Skip to content

Commit

Permalink
Merge branch 'master' into pass-through-regression
Browse files Browse the repository at this point in the history
  • Loading branch information
lynn (they) committed Oct 17, 2019
2 parents 8be7bc4 + a221e66 commit c30b1cf
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -691,7 +691,7 @@ The code above will use the "load" string as a flag name to get the file name of
a yaml file from the cli.Context. It will then use that file name to initialize
the yaml input source for any flags that are defined on that command. As a note
the "load" flag used would also have to be defined on the command flags in order
for this code snipped to work.
for this code snippet to work.

Currently only YAML, JSON, and TOML files are supported but developers can add support
for other input sources by implementing the altsrc.InputSourceContext for their
Expand Down
2 changes: 2 additions & 0 deletions autocomplete/zsh_autocomplete
@@ -1,3 +1,5 @@
#compdef $PROG

_cli_zsh_autocomplete() {

local -a opts
Expand Down
5 changes: 5 additions & 0 deletions command_test.go
Expand Up @@ -70,8 +70,13 @@ func TestParseAndRunShortOpts(t *testing.T) {
{[]string{"foo", "test", "-af"}, nil, []string{}},
{[]string{"foo", "test", "-cf"}, nil, []string{}},
{[]string{"foo", "test", "-acf"}, nil, []string{}},
{[]string{"foo", "test", "--acf"}, errors.New("flag provided but not defined: -acf"), nil},
{[]string{"foo", "test", "-invalid"}, errors.New("flag provided but not defined: -invalid"), nil},
{[]string{"foo", "test", "-acf", "-invalid"}, errors.New("flag provided but not defined: -invalid"), nil},
{[]string{"foo", "test", "--invalid"}, errors.New("flag provided but not defined: -invalid"), nil},
{[]string{"foo", "test", "-acf", "--invalid"}, errors.New("flag provided but not defined: -invalid"), nil},
{[]string{"foo", "test", "-acf", "arg1", "-invalid"}, nil, []string{"arg1", "-invalid"}},
{[]string{"foo", "test", "-acf", "arg1", "--invalid"}, nil, []string{"arg1", "--invalid"}},
{[]string{"foo", "test", "-acfi", "not-arg", "arg1", "-invalid"}, nil, []string{"arg1", "-invalid"}},
{[]string{"foo", "test", "-i", "ivalue"}, nil, []string{}},
{[]string{"foo", "test", "-i", "ivalue", "arg1"}, nil, []string{"arg1"}},
Expand Down
25 changes: 16 additions & 9 deletions parse.go
Expand Up @@ -22,28 +22,35 @@ 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{}
argsWereSplit := false
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:]...)...)
argsWereSplit = true
break
}

// This should be an impossible to reach code path, but in case the arg
// splitting failed to happen, this will prevent infinite loops
if !argsWereSplit {
return err
}

// Since custom parsing failed, replace the flag set before retrying
Expand Down

0 comments on commit c30b1cf

Please sign in to comment.