Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in which "fooasdf -o=foo" is incorrectly identified as the subcommand "foo" #93

Merged
merged 2 commits into from Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions cli.go
Expand Up @@ -680,12 +680,12 @@ func (c *CLI) processArgs() {
}

// Determine the argument we look to to end subcommands.
// We look at all arguments until one has a space. This
// disallows commands like: ./cli foo "bar baz". An argument
// with a space is always an argument.
// We look at all arguments until one is a flag or has a space.
// This disallows commands like: ./cli foo "bar baz". An
// argument with a space is always an argument.
j := 0
for k, v := range c.Args[i:] {
if strings.ContainsRune(v, ' ') {
if strings.ContainsRune(v, ' ') || v[0] == '-' {
break
}

Expand Down
31 changes: 31 additions & 0 deletions cli_test.go
Expand Up @@ -171,6 +171,37 @@ func TestCLIRun_prefix(t *testing.T) {
}
}

func TestCLIRun_subcommandSuffix(t *testing.T) {
buf := new(bytes.Buffer)
command := new(MockCommand)
cli := &CLI{
Args: []string{"fooasdf", "-o=foo"},
Commands: map[string]CommandFactory{
"foo": func() (Command, error) {
return command, nil
},

"foo bar": func() (Command, error) {
return command, nil
},
},
ErrorWriter: buf,
}

exitCode, err := cli.Run()
if err != nil {
t.Fatalf("err: %s", err)
}

if exitCode != 127 {
t.Fatalf("expected to get exit code 127, but got %d", exitCode)
}

if command.RunCalled {
t.Fatalf("run should not be called")
}
}

func TestCLIRun_default(t *testing.T) {
commandBar := new(MockCommand)
commandBar.RunResult = 42
Expand Down