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
3 changes: 3 additions & 0 deletions command_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,9 @@ func (cmd *Command) run(ctx context.Context, osArgs []string) (_ context.Context
tracef("using default command args=%[1]q (cmd=%[2]q)", argsWithDefault, cmd.Name)
subCmd = cmd.Command(argsWithDefault.First())
cmd.parsedArgs = argsWithDefault
if !hasDefault && cmd.parsedArgs.Len() > 0 && cmd.parsedArgs.First() == "" {
cmd.parsedArgs = &stringSliceArgs{cmd.parsedArgs.Tail()}
}
}
}
} else if cmd.DefaultCommand != "" {
Expand Down
34 changes: 34 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5444,3 +5444,37 @@
})
}
}

func TestCommand_FlagNameNoDefaultCommand(t *testing.T) {
var gotArgs Args

app := &Command{HideHelpCommand: true, HideHelp: true}
app.Commands = []*Command{
&Command{

Check failure on line 5453 in command_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not properly formatted (gofumpt)
Name: "foo",
Flags: []Flag{
&BoolFlag{
Name: "list",
},
},
Action: func(ctx context.Context, c *Command) error {
gotArgs = c.Args()
return nil
},
},
}

// flag has to be set to execute code path
if err := app.Commands[0].Flags[0].Set("list", "true"); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe set the default value of list to true in flag ? Or set an env variable so that it triggers the Set rather than us having to manually do it

t.Fatalf("Unexpected error setting flag: got %v, want nil", err)
}

err := app.Run(context.Background(), []string{"list", "foo", "list"})
if err != nil {
t.Errorf("Unexpected error: got %v, want nil", err)
return
}

var wantArgs Args = &stringSliceArgs{v: []string{"list"}}
assert.Equal(t, wantArgs, gotArgs)
}
Loading