Skip to content

Commit

Permalink
Merge pull request urfave#1935 from dearchap/issue_1925
Browse files Browse the repository at this point in the history
Fix:(issue_1925) Dont print default text for required flags
  • Loading branch information
dearchap authored Jun 29, 2024
2 parents c4cd0a5 + 1c7a91e commit df46df5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
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

0 comments on commit df46df5

Please sign in to comment.