diff --git a/command_parse.go b/command_parse.go index d1e09735a8..cb2765b898 100644 --- a/command_parse.go +++ b/command_parse.go @@ -110,9 +110,9 @@ func (cmd *Command) parseFlags(args Args) (Args, error) { // positional argument probably tracef("rearrange-3 (cmd=%[1]q) check %[2]q", cmd.Name, firstArg) - // if there is a command by that name let the command handle the - // rest of the parsing - if cmd.Command(firstArg) != nil { + // Dispatch matches the first positional argument without trimming it. + // Only hand off parsing when that argument selects a child command. + if len(posArgs) == 0 && cmd.Command(rargs[0]) != nil { posArgs = append(posArgs, rargs...) return &stringSliceArgs{posArgs}, nil } diff --git a/command_test.go b/command_test.go index 263ad82cde..28b6ca1a1d 100644 --- a/command_test.go +++ b/command_test.go @@ -8333,3 +8333,39 @@ func TestCommand_RunAgainChecksRequiredFlagsAndSources(t *testing.T) { r.NoError(cmd.Run(buildTestContext(t), []string{"app"})) r.Equal("from-env", cmd.String("name")) } + +func TestCommand_ChildNameAfterPositionalArgument(t *testing.T) { + for _, test := range []struct { + name string + args []string + wantArgs []string + wantChild bool + }{ + {name: "command name", args: []string{"file", "deploy", "--verbose"}, wantArgs: []string{"file", "deploy"}}, + {name: "command alias", args: []string{"file", "d", "-v"}, wantArgs: []string{"file", "d"}}, + {name: "empty first argument", args: []string{"", "deploy", "--verbose"}, wantArgs: []string{"", "deploy"}}, + {name: "ordinary argument", args: []string{"file", "other", "--verbose"}, wantArgs: []string{"file", "other"}}, + {name: "padded first argument", args: []string{" deploy ", "--verbose"}, wantArgs: []string{" deploy "}}, + {name: "first command name", args: []string{"deploy", "--verbose"}, wantArgs: []string{}, wantChild: true}, + {name: "first command alias", args: []string{"d", "--verbose"}, wantArgs: []string{}, wantChild: true}, + } { + t.Run(test.name, func(t *testing.T) { + var ran bool + action := func(_ context.Context, cmd *Command) error { + ran = true + require.Equal(t, test.wantChild, cmd.Name == "deploy") + require.True(t, cmd.Bool("verbose")) + require.Equal(t, test.wantArgs, cmd.Args().Slice()) + return nil + } + cmd := &Command{ + Name: "app", + Flags: []Flag{&BoolFlag{Name: "verbose", Aliases: []string{"v"}}}, + Action: action, + Commands: []*Command{{Name: "deploy", Aliases: []string{"d"}, Action: action}}, + } + require.NoError(t, cmd.Run(t.Context(), append([]string{"app"}, test.args...))) + require.True(t, ran) + }) + } +}