Merge pull request #4321 from vieux/docker_run_-it

Add support for docker run -it or docker images -qa
This commit is contained in:
unclejack 2014-03-03 20:52:10 +02:00
commit 6e7ffe094b
2 changed files with 37 additions and 12 deletions

View file

@ -27,4 +27,5 @@ func main() {
fmt.Printf("b: %b\n", b) fmt.Printf("b: %b\n", b)
fmt.Printf("-bool: %b\n", b2) fmt.Printf("-bool: %b\n", b2)
fmt.Printf("s/#hidden/-string(via lookup): %s\n", flag.Lookup("s").Value.String()) fmt.Printf("s/#hidden/-string(via lookup): %s\n", flag.Lookup("s").Value.String())
fmt.Printf("ARGS: %v\n", flag.Args())
} }

View file

@ -77,6 +77,9 @@ import (
// ErrHelp is the error returned if the flag -help is invoked but no such flag is defined. // ErrHelp is the error returned if the flag -help is invoked but no such flag is defined.
var ErrHelp = errors.New("flag: help requested") var ErrHelp = errors.New("flag: help requested")
// ErrRetry is the error returned if you need to try letter by letter
var ErrRetry = errors.New("flag: retry")
// -- bool Value // -- bool Value
type boolValue bool type boolValue bool
@ -733,21 +736,21 @@ func (f *FlagSet) usage() {
} }
// parseOne parses one flag. It reports whether a flag was seen. // parseOne parses one flag. It reports whether a flag was seen.
func (f *FlagSet) parseOne() (bool, error) { func (f *FlagSet) parseOne() (bool, string, error) {
if len(f.args) == 0 { if len(f.args) == 0 {
return false, nil return false, "", nil
} }
s := f.args[0] s := f.args[0]
if len(s) == 0 || s[0] != '-' || len(s) == 1 { if len(s) == 0 || s[0] != '-' || len(s) == 1 {
return false, nil return false, "", nil
} }
if s[1] == '-' && len(s) == 2 { // "--" terminates the flags if s[1] == '-' && len(s) == 2 { // "--" terminates the flags
f.args = f.args[1:] f.args = f.args[1:]
return false, nil return false, "", nil
} }
name := s[1:] name := s[1:]
if len(name) == 0 || name[0] == '=' { if len(name) == 0 || name[0] == '=' {
return false, f.failf("bad flag syntax: %s", s) return false, "", f.failf("bad flag syntax: %s", s)
} }
// it's a flag. does it have an argument? // it's a flag. does it have an argument?
@ -767,14 +770,14 @@ func (f *FlagSet) parseOne() (bool, error) {
if !alreadythere { if !alreadythere {
if name == "-help" || name == "help" || name == "h" { // special case for nice help message. if name == "-help" || name == "help" || name == "h" { // special case for nice help message.
f.usage() f.usage()
return false, ErrHelp return false, "", ErrHelp
} }
return false, f.failf("flag provided but not defined: -%s", name) return false, name, ErrRetry
} }
if fv, ok := flag.Value.(boolFlag); ok && fv.IsBoolFlag() { // special case: doesn't need an arg if fv, ok := flag.Value.(boolFlag); ok && fv.IsBoolFlag() { // special case: doesn't need an arg
if has_value { if has_value {
if err := fv.Set(value); err != nil { if err := fv.Set(value); err != nil {
return false, f.failf("invalid boolean value %q for -%s: %v", value, name, err) return false, "", f.failf("invalid boolean value %q for -%s: %v", value, name, err)
} }
} else { } else {
fv.Set("true") fv.Set("true")
@ -787,17 +790,17 @@ func (f *FlagSet) parseOne() (bool, error) {
value, f.args = f.args[0], f.args[1:] value, f.args = f.args[0], f.args[1:]
} }
if !has_value { if !has_value {
return false, f.failf("flag needs an argument: -%s", name) return false, "", f.failf("flag needs an argument: -%s", name)
} }
if err := flag.Value.Set(value); err != nil { if err := flag.Value.Set(value); err != nil {
return false, f.failf("invalid value %q for flag -%s: %v", value, name, err) return false, "", f.failf("invalid value %q for flag -%s: %v", value, name, err)
} }
} }
if f.actual == nil { if f.actual == nil {
f.actual = make(map[string]*Flag) f.actual = make(map[string]*Flag)
} }
f.actual[name] = flag f.actual[name] = flag
return true, nil return true, "", nil
} }
// Parse parses flag definitions from the argument list, which should not // Parse parses flag definitions from the argument list, which should not
@ -808,13 +811,34 @@ func (f *FlagSet) Parse(arguments []string) error {
f.parsed = true f.parsed = true
f.args = arguments f.args = arguments
for { for {
seen, err := f.parseOne() seen, name, err := f.parseOne()
if seen { if seen {
continue continue
} }
if err == nil { if err == nil {
break break
} }
if err == ErrRetry {
if len(name) > 1 {
err = nil
for _, letter := range strings.Split(name, "") {
f.args = append([]string{"-" + letter}, f.args...)
seen2, _, err2 := f.parseOne()
if seen2 {
continue
}
if err2 != nil {
err = f.failf("flag provided but not defined: -%s", name)
break
}
}
if err == nil {
continue
}
} else {
err = f.failf("flag provided but not defined: -%s", name)
}
}
switch f.errorHandling { switch f.errorHandling {
case ContinueOnError: case ContinueOnError:
return err return err