Skip to content

fix(cli/clibase): don't error on required flags with --help #12181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
fix(cli/clibase): don't error on required flags with --help
  • Loading branch information
coadler committed Feb 15, 2024
commit 2a6b8019f3296d9924fa74e99e70abf69f2ef2b4
3 changes: 2 additions & 1 deletion cli/clibase/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,8 @@ func (inv *Invocation) run(state *runState) error {
missing = append(missing, opt.Flag)
}
}
if len(missing) > 0 {
// Don't error for missing flags if `--help` was supplied.
if len(missing) > 0 && !errors.Is(state.flagParseErr, pflag.ErrHelp) {
return xerrors.Errorf("Missing values for the required flags: %s", strings.Join(missing, ", "))
}

Expand Down
16 changes: 16 additions & 0 deletions cli/clibase/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ func TestCommand(t *testing.T) {
Required: true,
},
},
HelpHandler: func(i *clibase.Invocation) error {
_, _ = i.Stdout.Write([]byte("help text.png"))
return nil
},
Handler: func(i *clibase.Invocation) error {
_, _ = i.Stdout.Write([]byte(fmt.Sprintf("%s-%t", reqStr, reqBool)))
return nil
Expand Down Expand Up @@ -255,6 +259,18 @@ func TestCommand(t *testing.T) {
require.ErrorContains(t, err, "Missing values")
})

t.Run("RequiredFlagsMissingWithHelp", func(t *testing.T) {
t.Parallel()
i := cmd().Invoke(
"required-flag",
"--help",
)
fio := fakeIO(i)
err := i.Run()
require.NoError(t, err)
require.Contains(t, fio.Stdout.String(), "help text.png")
})

t.Run("RequiredFlagsMissingBool", func(t *testing.T) {
t.Parallel()
i := cmd().Invoke(
Expand Down