mirror of
https://github.com/coder/coder.git
synced 2026-09-22 05:05:20 +08:00
fix(cli/clibase): don't error on required flags with --help (#12181)
This commit is contained in:
+2
-1
@@ -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, ", "))
|
||||
}
|
||||
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user