update vars and args

This commit is contained in:
Lynn Cyrin 2019-08-25 14:12:13 -07:00
parent 0f9d8a9abd
commit 406a1c31b0
No known key found for this signature in database
GPG Key ID: EE9CCB427DFEC897

View File

@ -190,7 +190,7 @@ func (c *Command) parseFlags(args Args) (*flag.FlagSet, error) {
} }
if !c.SkipArgReorder { if !c.SkipArgReorder {
args = reorderArgs(args) args = reorderArgs(c.Flags, args)
} }
set, err := parseIter(c, args) set, err := parseIter(c, args)
@ -214,34 +214,36 @@ func (c *Command) useShortOptionHandling() bool {
return c.UseShortOptionHandling return c.UseShortOptionHandling
} }
// reorderArgs moves all flags before arguments as this is what flag expects // reorderArgs moves all flags (reorderedFlags) before arguments (remainingArgs)
func reorderArgs(args []string) []string { // as this is what flag expects.
var nonflags, flags []string func reorderArgs(commandFlags []Flag, args []string) []string {
var remainingArgs []string
var reorderedFlags []string
readFlagValue := false readFlagValue := false
for i, arg := range args { for i, arg := range args {
if arg == "--" { if arg == "--" {
nonflags = append(nonflags, args[i:]...) remainingArgs = append(remainingArgs, args[i:]...)
break break
} }
if readFlagValue && !strings.HasPrefix(arg, "-") && !strings.HasPrefix(arg, "--") { if readFlagValue && !strings.HasPrefix(arg, "-") && !strings.HasPrefix(arg, "--") {
readFlagValue = false readFlagValue = false
flags = append(flags, arg) reorderedFlags = append(reorderedFlags, arg)
continue continue
} }
readFlagValue = false readFlagValue = false
if arg != "-" && strings.HasPrefix(arg, "-") { if arg != "-" && strings.HasPrefix(arg, "-") {
flags = append(flags, arg) reorderedFlags = append(reorderedFlags, arg)
readFlagValue = !strings.Contains(arg, "=") readFlagValue = !strings.Contains(arg, "=")
} else { } else {
nonflags = append(nonflags, arg) remainingArgs = append(remainingArgs, arg)
} }
} }
return append(flags, nonflags...) return append(reorderedFlags, remainingArgs...)
} }
// Names returns the names including short names and aliases. // Names returns the names including short names and aliases.