Fix infinite loop flag parsing with short options
This commit is contained in:
17
parse.go
17
parse.go
@@ -22,28 +22,27 @@ func parseIter(set *flag.FlagSet, ip iterativeParser, args []string) error {
|
||||
}
|
||||
|
||||
errStr := err.Error()
|
||||
trimmed := strings.TrimPrefix(errStr, "flag provided but not defined: ")
|
||||
trimmed := strings.TrimPrefix(errStr, "flag provided but not defined: -")
|
||||
if errStr == trimmed {
|
||||
return err
|
||||
}
|
||||
|
||||
// regenerate the initial args with the split short opts
|
||||
newArgs := []string{}
|
||||
for i, arg := range args {
|
||||
if arg != trimmed {
|
||||
newArgs = append(newArgs, arg)
|
||||
// skip args that are not part of the error message
|
||||
if name := strings.TrimLeft(arg, "-"); name != trimmed {
|
||||
continue
|
||||
}
|
||||
|
||||
shortOpts := splitShortOptions(set, trimmed)
|
||||
// if we can't split, the error was accurate
|
||||
shortOpts := splitShortOptions(set, arg)
|
||||
if len(shortOpts) == 1 {
|
||||
return err
|
||||
}
|
||||
|
||||
// add each short option and all remaining arguments
|
||||
newArgs = append(newArgs, shortOpts...)
|
||||
newArgs = append(newArgs, args[i+1:]...)
|
||||
args = newArgs
|
||||
// swap current argument with the split version
|
||||
args = append(args[:i], append(shortOpts, args[i+1:]...)...)
|
||||
break
|
||||
}
|
||||
|
||||
// Since custom parsing failed, replace the flag set before retrying
|
||||
|
Reference in New Issue
Block a user