Skip to content
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
7 changes: 5 additions & 2 deletions flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 30 additions & 0 deletions help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down