mirror of
https://github.com/spf13/cobra
synced 2024-11-25 07:07:15 +00:00
test if short flag is a bool setting inFlag
This commit is contained in:
parent
0c34d16c31
commit
92aeee7d6c
1 changed files with 18 additions and 11 deletions
23
command.go
23
command.go
|
@ -444,16 +444,23 @@ func hasNoOptDefVal(name string, fs *flag.FlagSet) bool {
|
||||||
return flag.NoOptDefVal != ""
|
return flag.NoOptDefVal != ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func shortHasNoOptDefVal(name string, fs *flag.FlagSet) bool {
|
func shortWithSpace(arg string, c *Command) bool {
|
||||||
if len(name) == 0 {
|
if strings.HasPrefix(arg, "-") {
|
||||||
return false
|
if !strings.Contains(arg, "=") {
|
||||||
}
|
if len(arg) == 2 {
|
||||||
|
flag := c.Flags().ShorthandLookup(arg[1:])
|
||||||
flag := fs.ShorthandLookup(name[:1])
|
|
||||||
if flag == nil {
|
if flag == nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
if flag.Value.Type() == "bool" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
//shortHasNoOptDefVal
|
||||||
return flag.NoOptDefVal != ""
|
return flag.NoOptDefVal != ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func stripFlags(args []string, c *Command) []string {
|
func stripFlags(args []string, c *Command) []string {
|
||||||
|
@ -474,7 +481,7 @@ Loop:
|
||||||
// If '--flag arg' then
|
// If '--flag arg' then
|
||||||
// delete arg from args.
|
// delete arg from args.
|
||||||
fallthrough // (do the same as below)
|
fallthrough // (do the same as below)
|
||||||
case strings.HasPrefix(s, "-") && !strings.Contains(s, "=") && len(s) == 2 && !shortHasNoOptDefVal(s[1:], flags):
|
case shortWithSpace(s, c):
|
||||||
// If '-f arg' then
|
// If '-f arg' then
|
||||||
// delete 'arg' from args or break the loop if len(args) <= 1.
|
// delete 'arg' from args or break the loop if len(args) <= 1.
|
||||||
if len(args) <= 1 {
|
if len(args) <= 1 {
|
||||||
|
@ -585,7 +592,7 @@ func (c *Command) Traverse(args []string) (*Command, []string, error) {
|
||||||
flags = append(flags, arg)
|
flags = append(flags, arg)
|
||||||
continue
|
continue
|
||||||
// A short flag with a space separated value
|
// A short flag with a space separated value
|
||||||
case strings.HasPrefix(arg, "-") && !strings.Contains(arg, "=") && len(arg) == 2 && !shortHasNoOptDefVal(arg[1:], c.Flags()):
|
case shortWithSpace(arg, c):
|
||||||
inFlag = true
|
inFlag = true
|
||||||
flags = append(flags, arg)
|
flags = append(flags, arg)
|
||||||
continue
|
continue
|
||||||
|
|
Loading…
Reference in a new issue