Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix:(issue_1925) Dont print default text for required flags #1935

Merged
merged 3 commits into from
Jun 29, 2024
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() {
dearchap marked this conversation as resolved.
Show resolved Hide resolved
if s := df.GetDefaultText(); s != "" {
dearchap marked this conversation as resolved.
Show resolved Hide resolved
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
Loading