From 4a044b3cb6864825cb61764a6eec91f6586146e0 Mon Sep 17 00:00:00 2001 From: Naveen Gogineni Date: Thu, 27 Jun 2024 21:55:16 -0400 Subject: [PATCH 1/3] Fix:(issue_1925) Dont print default text for required flags --- examples_test.go | 4 +++- flag.go | 7 +++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/examples_test.go b/examples_test.go index b7050b801d..93d5bb1cbb 100644 --- a/examples_test.go +++ b/examples_test.go @@ -97,6 +97,7 @@ func ExampleCommand_Run_appHelp() { }, Flags: []cli.Flag{ &cli.StringFlag{Name: "name", Value: "bob", Usage: "a name to say"}, + &cli.StringFlag{Name: "id", Value: "abcd-dndndnd", Usage: "an id", Required: true}, }, Commands: []*cli.Command{ { @@ -117,7 +118,7 @@ func ExampleCommand_Run_appHelp() { defer cancel() // Simulate the command line arguments - os.Args = []string{"greet", "help"} + os.Args = []string{"greet", "--id", "foo-x", "help"} _ = cmd.Run(ctx, os.Args) // Output: @@ -143,6 +144,7 @@ func ExampleCommand_Run_appHelp() { // // GLOBAL OPTIONS: // --name value a name to say (default: "bob") + // --id value an id // --help, -h show help (default: false) // --version, -v print the version (default: false) } diff --git a/flag.go b/flag.go index f5c24b0f37..6ceb54a399 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) + // dont 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) From 31ae48d614ecc5b7a48361c45376b819c9330074 Mon Sep 17 00:00:00 2001 From: dearchap Date: Fri, 28 Jun 2024 07:26:37 -0400 Subject: [PATCH 2/3] Update flag.go Co-authored-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com> --- flag.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flag.go b/flag.go index 6ceb54a399..8a87ac4553 100644 --- a/flag.go +++ b/flag.go @@ -347,7 +347,7 @@ func stringifyFlag(f Flag) string { defaultValueString := "" - // dont print default text for required flags + // 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) From 1c7a91e68e0c5cc1918b006449e45fb5468d312e Mon Sep 17 00:00:00 2001 From: Naveen Gogineni Date: Fri, 28 Jun 2024 09:57:22 -0400 Subject: [PATCH 3/3] Add new test for required flags --- examples_test.go | 4 +--- help_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/examples_test.go b/examples_test.go index 93d5bb1cbb..b7050b801d 100644 --- a/examples_test.go +++ b/examples_test.go @@ -97,7 +97,6 @@ func ExampleCommand_Run_appHelp() { }, Flags: []cli.Flag{ &cli.StringFlag{Name: "name", Value: "bob", Usage: "a name to say"}, - &cli.StringFlag{Name: "id", Value: "abcd-dndndnd", Usage: "an id", Required: true}, }, Commands: []*cli.Command{ { @@ -118,7 +117,7 @@ func ExampleCommand_Run_appHelp() { defer cancel() // Simulate the command line arguments - os.Args = []string{"greet", "--id", "foo-x", "help"} + os.Args = []string{"greet", "help"} _ = cmd.Run(ctx, os.Args) // Output: @@ -144,7 +143,6 @@ func ExampleCommand_Run_appHelp() { // // GLOBAL OPTIONS: // --name value a name to say (default: "bob") - // --id value an id // --help, -h show help (default: false) // --version, -v print the version (default: false) } 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() {