Noticed this behavior on an internal tool, essentially, if you have flags defined for a command, then execute a subcommand of that command with a flag before the subcommand, then it identifies the command as the subcommand.
For example, if we take the test cases @ https://github.com/mitchellh/cli/blob/master/cli_test.go#L1459-L1489 and change it to:
func TestCLISubcommand_nested(t *testing.T) {
testCases := []struct {
args []string
subcommand string
}{
{[]string{"bar"}, "bar"},
{[]string{"foo", "-h"}, "foo"},
{[]string{"-h", "bar"}, "bar"},
{[]string{"foo", "bar", "-h"}, "foo bar"},
{[]string{"foo", "bar", "baz", "-h"}, "foo bar baz"},
{[]string{"foo", "bar", "-h", "baz"}, "foo bar"}, // expected match would be `foo bar baz`
{[]string{"-h", "foo", "bar"}, "foo bar"},
}
for _, testCase := range testCases {
cli := &CLI{
Args: testCase.args,
Commands: map[string]CommandFactory{
"foo bar": func() (Command, error) {
return new(MockCommand), nil
},
"foo bar baz": func() (Command, error) {
return new(MockCommand), nil
},
},
}
result := cli.Subcommand()
if result != testCase.subcommand {
t.Errorf("Expected %#v, got %#v. Args: %#v",
testCase.subcommand, result, testCase.args)
}
}
}
I guess the question is, with other tools like cobra which handle flags anywhere within the arguments, does it make sense to change this library's behavior?