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

This commit is contained in:
Colin Adler
2024-02-15 23:41:46 +00:00
committed by GitHub
parent fbd436cc2c
commit 97e4d51953
2 changed files with 18 additions and 1 deletions
+2 -1
View File
@@ -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, ", "))
}
+16
View File
@@ -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
@@ -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(