diff --git a/flag.go b/flag.go index f5c24b0f37..8a87ac4553 100644 --- a/flag.go +++ b/flag.go @@ -347,8 +347,11 @@ func stringifyFlag(f Flag) string { defaultValueString := "" - if s := df.GetDefaultText(); s != "" { - defaultValueString = fmt.Sprintf(formatDefault("%s"), s) + // don't print default text for required flags + if rf, ok := f.(RequiredFlag); !ok || !rf.IsRequired() { + if s := df.GetDefaultText(); s != "" { + defaultValueString = fmt.Sprintf(formatDefault("%s"), s) + } } usageWithDefault := strings.TrimSpace(usage + defaultValueString) diff --git a/help_test.go b/help_test.go index b45e0d2191..f5d8b82985 100644 --- a/help_test.go +++ b/help_test.go @@ -65,6 +65,36 @@ func Test_ShowAppHelp_MultiLineDescription(t *testing.T) { } } +func Test_Help_RequiredFlagsNoDefault(t *testing.T) { + output := new(bytes.Buffer) + + cmd := &Command{ + Flags: []Flag{ + &IntFlag{Name: "foo", Aliases: []string{"f"}, Required: true}, + }, + Writer: output, + } + + _ = cmd.Run(buildTestContext(t), []string{"test", "-h"}) + + expected := `NAME: + test - A new cli application + +USAGE: + test [global options] [command [command options]] [arguments...] + +COMMANDS: + help, h Shows a list of commands or help for one command + +GLOBAL OPTIONS: + --foo value, -f value + --help, -h show help (default: false) +` + + assert.Contains(t, output.String(), expected, + "expected output to include usage text") +} + func Test_Help_Custom_Flags(t *testing.T) { oldFlag := HelpFlag defer func() {