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

Exclude blank args from subcommand parsing #94

Merged
merged 1 commit into from May 13, 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
5 changes: 3 additions & 2 deletions cli.go
Expand Up @@ -682,10 +682,11 @@ func (c *CLI) processArgs() {
// Determine the argument we look to to end subcommands.
// 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.
// argument with a space is always an argument. A blank
// argument is always an argument.
j := 0
for k, v := range c.Args[i:] {
if strings.ContainsRune(v, ' ') || v[0] == '-' {
if strings.ContainsRune(v, ' ') || v == "" || v[0] == '-' {
break
}

Expand Down
32 changes: 32 additions & 0 deletions cli_test.go
Expand Up @@ -427,6 +427,38 @@ func TestCLIRun_nestedNoArgs(t *testing.T) {
}
}

func TestCLIRun_nestedBlankArg(t *testing.T) {
command := new(MockCommand)
cli := &CLI{
Args: []string{"foo", "", "bar", "-baz"},
Commands: map[string]CommandFactory{
"foo": func() (Command, error) {
return command, nil
},
"foo bar": func() (Command, error) {
return new(MockCommand), nil
},
},
}

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

if exitCode != command.RunResult {
t.Fatalf("bad: %d", exitCode)
}

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

if !reflect.DeepEqual(command.RunArgs, []string{"", "bar", "-baz"}) {
t.Fatalf("bad args: %#v", command.RunArgs)
}
}

func TestCLIRun_nestedQuotedCommand(t *testing.T) {
command := new(MockCommand)
cli := &CLI{
Expand Down