Skip to content

Commit 97e4d51

Browse files
authored
fix(cli/clibase): don't error on required flags with --help (#12181)
1 parent fbd436c commit 97e4d51

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

cli/clibase/cmd.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,8 @@ func (inv *Invocation) run(state *runState) error {
383383
missing = append(missing, opt.Flag)
384384
}
385385
}
386-
if len(missing) > 0 {
386+
// Don't error for missing flags if `--help` was supplied.
387+
if len(missing) > 0 && !errors.Is(state.flagParseErr, pflag.ErrHelp) {
387388
return xerrors.Errorf("Missing values for the required flags: %s", strings.Join(missing, ", "))
388389
}
389390

cli/clibase/cmd_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ func TestCommand(t *testing.T) {
7979
Required: true,
8080
},
8181
},
82+
HelpHandler: func(i *clibase.Invocation) error {
83+
_, _ = i.Stdout.Write([]byte("help text.png"))
84+
return nil
85+
},
8286
Handler: func(i *clibase.Invocation) error {
8387
_, _ = i.Stdout.Write([]byte(fmt.Sprintf("%s-%t", reqStr, reqBool)))
8488
return nil
@@ -255,6 +259,18 @@ func TestCommand(t *testing.T) {
255259
require.ErrorContains(t, err, "Missing values")
256260
})
257261

262+
t.Run("RequiredFlagsMissingWithHelp", func(t *testing.T) {
263+
t.Parallel()
264+
i := cmd().Invoke(
265+
"required-flag",
266+
"--help",
267+
)
268+
fio := fakeIO(i)
269+
err := i.Run()
270+
require.NoError(t, err)
271+
require.Contains(t, fio.Stdout.String(), "help text.png")
272+
})
273+
258274
t.Run("RequiredFlagsMissingBool", func(t *testing.T) {
259275
t.Parallel()
260276
i := cmd().Invoke(

0 commit comments

Comments
 (0)