From 91df5063ba5a79d4659a301a4647853fef377283 Mon Sep 17 00:00:00 2001 From: yxshee <84263676+yxshee@users.noreply.github.com> Date: Sun, 15 Nov 2026 06:27:36 +0530 Subject: [PATCH 1/2] fix: preserve flag parsing when an argument matches a command --- command_parse.go | 6 +++--- command_test.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/command_parse.go b/command_parse.go index d1e09735a8..d4419fddb6 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 { + // Only the first positional argument can select a child command. + // Let that command handle the rest of the parsing. + if len(posArgs) == 0 && cmd.Command(firstArg) != nil { posArgs = append(posArgs, rargs...) return &stringSliceArgs{posArgs}, nil } diff --git a/command_test.go b/command_test.go index 263ad82cde..5cea23ede4 100644 --- a/command_test.go +++ b/command_test.go @@ -8333,3 +8333,38 @@ 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: "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) + }) + } +} From 1b1912ca1d83a8b776e5b731d02776637bc68b2c Mon Sep 17 00:00:00 2001 From: yxshee <84263676+yxshee@users.noreply.github.com> Date: Sun, 15 Nov 2026 06:38:28 +0530 Subject: [PATCH 2/2] fix: match child command names without trimming arguments --- command_parse.go | 6 +++--- command_test.go | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/command_parse.go b/command_parse.go index d4419fddb6..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) - // Only the first positional argument can select a child command. - // Let that command handle the rest of the parsing. - if len(posArgs) == 0 && 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 5cea23ede4..28b6ca1a1d 100644 --- a/command_test.go +++ b/command_test.go @@ -8345,6 +8345,7 @@ func TestCommand_ChildNameAfterPositionalArgument(t *testing.T) { {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}, } {