Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions command_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,23 @@ func (cmd *Command) parseFlags(args Args) (Args, error) {
posArgs = append(posArgs, rargs...)
return &stringSliceArgs{posArgs}, nil
}
// When DefaultCommand is set, pass unknown flags through as positional args
// so the default command can handle them (fixes #2249)
if cmd.DefaultCommand != "" {
posArgs = append(posArgs, rargs...)
return &stringSliceArgs{posArgs}, nil
}
return &stringSliceArgs{posArgs}, fmt.Errorf("%s%s", providedButNotDefinedErrMsg, flagName)
}

// try to split the flags
for index, c := range flagName {
tracef("processing flag (fName=%[1]q)", string(c))
if sf := cmd.lookupFlag(string(c)); sf == nil {
if index == 0 && cmd.DefaultCommand != "" {
posArgs = append(posArgs, rargs...)
return &stringSliceArgs{posArgs}, nil
}
return &stringSliceArgs{posArgs}, fmt.Errorf("%s%s", providedButNotDefinedErrMsg, flagName)
} else if fb, ok := sf.(boolFlag); ok && fb.IsBoolFlag() {
fv := flagVal
Expand Down
106 changes: 106 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5772,6 +5772,7 @@
})
}


Check failure on line 5775 in command_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not properly formatted (gofumpt)
// TestCommand_NoDefaultCmdArgMatchingFlag tests the argument set
// of a command which has no default command, and has a flag with
// the name of the next argument
Expand Down Expand Up @@ -5801,3 +5802,108 @@
require.NoError(t, err)
require.Equal(t, &expectedArgs, actualArgs)
}

func TestDefaultCommandWithSubcommandFlags(t *testing.T) {
// Regression test for https://github.com/urfave/cli/issues/2249
// When DefaultCommand is set, flags defined on the default subcommand
// should work even when the subcommand name is omitted.
actionExecuted := false
cmd := &Command{
DefaultCommand: "run1",
Commands: []*Command{
{
Name: "run1",
Usage: "run the main application",
Action: func(ctx context.Context, cmd *Command) error {
actionExecuted = true
return nil
},
Flags: []Flag{
&StringFlag{
Name: "foo",
Required: true,
},
},
},
{
Name: "run2",
Action: func(ctx context.Context, cmd *Command) error {
return nil
},
Flags: []Flag{
&StringFlag{
Name: "value",
Required: true,
},
},
},
},
}

// Using flag without subcommand name should route to default command
err := cmd.Run(buildTestContext(t), []string{"c", "--foo", "bar"})
assert.NoError(t, err)
assert.True(t, actionExecuted, "expected run1 action to be executed")
}

func TestDefaultCommandWithShortFlag(t *testing.T) {
// Covers the short-flag splitting path in parseFlags when DefaultCommand is set
actionRun := false
cmd := &Command{
DefaultCommand: "run",
Commands: []*Command{
{
Name: "run",
Usage: "run the app",
Action: func(ctx context.Context, cmd *Command) error {
actionRun = true
if cmd.String("foo") != "baz" {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the action isnt run for some reason (due to fault args or something) then this check wont run. Add the actionRun flag as above

return fmt.Errorf("expected foo=baz, got %s", cmd.String("foo"))
}
return nil
},
Flags: []Flag{
&StringFlag{
Name: "foo",
Aliases: []string{"f"},
},
},
},
},
}

err := cmd.Run(buildTestContext(t), []string{"c", "-f", "baz"})
assert.NoError(t, err)
assert.True(t, actionRun, "expected run action to be executed")
}

func TestDefaultCommandWithShortFlagHandling(t *testing.T) {
// Covers the shortOptionHandling for-loop path when DefaultCommand is set
actionRun := false
cmd := &Command{
UseShortOptionHandling: true,
DefaultCommand: "run",
Commands: []*Command{
{
Name: "run",
Usage: "run the app",
Action: func(ctx context.Context, cmd *Command) error {
actionRun = true
if cmd.String("foo") != "baz" {
return fmt.Errorf("expected foo=baz, got %s", cmd.String("foo"))
}
return nil
},
Flags: []Flag{
&StringFlag{
Name: "foo",
Aliases: []string{"f"},
},
},
},
},
}
err := cmd.Run(buildTestContext(t), []string{"c", "-f", "baz"})
assert.NoError(t, err)
assert.True(t, actionRun, "expected run action to be executed")
}
Loading