Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
exclude flags from subcommand parsing
Browse files Browse the repository at this point in the history
When determining the string to use when querying the radix tree for the
longest subcommand, exclude any argument beginning with a "-", since
this is a flag and not a subcommand.

This fixes a bug in which "fooasdf -o=foo" is matched to the subcommand
"foo" due to logic further down on line 704.
  • Loading branch information
kmoe committed Apr 21, 2022
1 parent 3bf635d commit 4bb9ba2
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 7 deletions.
8 changes: 4 additions & 4 deletions cli.go
Original file line number Diff line number Diff line change
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
3 changes: 0 additions & 3 deletions cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,6 @@ func TestCLIRun_prefix(t *testing.T) {
}
}

// NOTE: This failing test is intended to demonstrate incorrect behaviour
// in which a subcommand with an arbitrary suffix is still parsed as that
// subcommand, if a supplied argument is also suffixed by that subcommand.
func TestCLIRun_subcommandSuffix(t *testing.T) {
buf := new(bytes.Buffer)
command := new(MockCommand)
Expand Down

0 comments on commit 4bb9ba2

Please sign in to comment.