From 09042535c77b2b482b178960f162aff4779aedc6 Mon Sep 17 00:00:00 2001 From: Naveen Gogineni Date: Fri, 30 Dec 2022 11:06:47 -0500 Subject: [PATCH 1/2] Fix:(issue_1277) Remove default text for version/help flags --- app_test.go | 10 +++++----- flag-spec.yaml | 2 ++ flag.go | 23 +++++++++++++++-------- godoc-current.txt | 16 ++++++++++------ help_test.go | 9 +++------ testdata/godoc-v2.x.txt | 16 ++++++++++------ zz_generated.flags.go | 2 ++ 7 files changed, 47 insertions(+), 31 deletions(-) diff --git a/app_test.go b/app_test.go index 7091c40ef9..7f2a2144e5 100644 --- a/app_test.go +++ b/app_test.go @@ -145,8 +145,8 @@ func ExampleApp_Run_appHelp() { // // GLOBAL OPTIONS: // --name value a name to say (default: "bob") - // --help, -h show help (default: false) - // --version, -v print the version (default: false) + // --help, -h show help + // --version, -v print the version } func ExampleApp_Run_commandHelp() { @@ -183,7 +183,7 @@ func ExampleApp_Run_commandHelp() { // This is how we describe describeit the function // // OPTIONS: - // --help, -h show help (default: false) + // --help, -h show help } func ExampleApp_Run_noAction() { @@ -201,7 +201,7 @@ func ExampleApp_Run_noAction() { // help, h Shows a list of commands or help for one command // // GLOBAL OPTIONS: - // --help, -h show help (default: false) + // --help, -h show help } func ExampleApp_Run_subcommandNoAction() { @@ -228,7 +228,7 @@ func ExampleApp_Run_subcommandNoAction() { // This is how we describe describeit the function // // OPTIONS: - // --help, -h show help (default: false) + // --help, -h show help } diff --git a/flag-spec.yaml b/flag-spec.yaml index 76480ba47c..cfe47df06d 100644 --- a/flag-spec.yaml +++ b/flag-spec.yaml @@ -7,6 +7,8 @@ flag_types: - name: Count type: int pointer: true + - name: DisableDefaultText + type: bool - name: Action type: "func(*Context, bool) error" float64: diff --git a/flag.go b/flag.go index 5c0a8b7328..6d4dbffb38 100644 --- a/flag.go +++ b/flag.go @@ -34,18 +34,20 @@ var BashCompletionFlag Flag = &BoolFlag{ // VersionFlag prints the version for the application var VersionFlag Flag = &BoolFlag{ - Name: "version", - Aliases: []string{"v"}, - Usage: "print the version", + Name: "version", + Aliases: []string{"v"}, + Usage: "print the version", + DisableDefaultText: true, } // HelpFlag prints the help for all commands and subcommands. // Set to nil to disable the flag. The subcommand // will still be added unless HideHelp or HideHelpCommand is set to true. var HelpFlag Flag = &BoolFlag{ - Name: "help", - Aliases: []string{"h"}, - Usage: "show help", + Name: "help", + Aliases: []string{"h"}, + Usage: "show help", + DisableDefaultText: true, } // FlagStringer converts a flag definition to a string. This is used by help @@ -337,8 +339,13 @@ func stringifyFlag(f Flag) string { defaultValueString := "" - if s := df.GetDefaultText(); s != "" { - defaultValueString = fmt.Sprintf(formatDefault("%s"), s) + // set default text for all flags except bool flags + // for bool flags text is set only is DisableDefaultText is not + // set + if bf, ok := f.(*BoolFlag); !ok || !bf.DisableDefaultText { + if s := df.GetDefaultText(); s != "" { + defaultValueString = fmt.Sprintf(formatDefault("%s"), s) + } } usageWithDefault := strings.TrimSpace(usage + defaultValueString) diff --git a/godoc-current.txt b/godoc-current.txt index 6afd244f25..1b0bef3112 100644 --- a/godoc-current.txt +++ b/godoc-current.txt @@ -455,6 +455,8 @@ type BoolFlag struct { Count *int + DisableDefaultText bool + Action func(*Context, bool) error // Has unexported fields. } @@ -881,18 +883,20 @@ var BashCompletionFlag Flag = &BoolFlag{ BashCompletionFlag enables bash-completion for all commands and subcommands var HelpFlag Flag = &BoolFlag{ - Name: "help", - Aliases: []string{"h"}, - Usage: "show help", + Name: "help", + Aliases: []string{"h"}, + Usage: "show help", + DisableDefaultText: true, } HelpFlag prints the help for all commands and subcommands. Set to nil to disable the flag. The subcommand will still be added unless HideHelp or HideHelpCommand is set to true. var VersionFlag Flag = &BoolFlag{ - Name: "version", - Aliases: []string{"v"}, - Usage: "print the version", + Name: "version", + Aliases: []string{"v"}, + Usage: "print the version", + DisableDefaultText: true, } VersionFlag prints the version for the application diff --git a/help_test.go b/help_test.go index b1b456c8ae..0a2d9f97fc 100644 --- a/help_test.go +++ b/help_test.go @@ -1366,8 +1366,7 @@ DESCRIPTION: case OPTIONS: - --help, -h show help - (default: false) + --help, -h show help ` if output.String() != expected { @@ -1436,8 +1435,7 @@ USAGE: even more OPTIONS: - --help, -h show help - (default: false) + --help, -h show help ` if output.String() != expected { @@ -1514,8 +1512,7 @@ USAGE: OPTIONS: --test-f value my test usage - --help, -h show help - (default: false) + --help, -h show help ` if output.String() != expected { diff --git a/testdata/godoc-v2.x.txt b/testdata/godoc-v2.x.txt index 6afd244f25..1b0bef3112 100644 --- a/testdata/godoc-v2.x.txt +++ b/testdata/godoc-v2.x.txt @@ -455,6 +455,8 @@ type BoolFlag struct { Count *int + DisableDefaultText bool + Action func(*Context, bool) error // Has unexported fields. } @@ -881,18 +883,20 @@ var BashCompletionFlag Flag = &BoolFlag{ BashCompletionFlag enables bash-completion for all commands and subcommands var HelpFlag Flag = &BoolFlag{ - Name: "help", - Aliases: []string{"h"}, - Usage: "show help", + Name: "help", + Aliases: []string{"h"}, + Usage: "show help", + DisableDefaultText: true, } HelpFlag prints the help for all commands and subcommands. Set to nil to disable the flag. The subcommand will still be added unless HideHelp or HideHelpCommand is set to true. var VersionFlag Flag = &BoolFlag{ - Name: "version", - Aliases: []string{"v"}, - Usage: "print the version", + Name: "version", + Aliases: []string{"v"}, + Usage: "print the version", + DisableDefaultText: true, } VersionFlag prints the version for the application diff --git a/zz_generated.flags.go b/zz_generated.flags.go index 7d0a267f78..016951a3f0 100644 --- a/zz_generated.flags.go +++ b/zz_generated.flags.go @@ -448,6 +448,8 @@ type BoolFlag struct { Count *int + DisableDefaultText bool + Action func(*Context, bool) error } From ba2a1244f3cc2ce81c3926d0b754d497118c1a15 Mon Sep 17 00:00:00 2001 From: Naveen Gogineni Date: Fri, 30 Dec 2022 16:23:13 -0500 Subject: [PATCH 2/2] Fix wording --- flag.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flag.go b/flag.go index 6d4dbffb38..5260f7f9e2 100644 --- a/flag.go +++ b/flag.go @@ -340,7 +340,7 @@ func stringifyFlag(f Flag) string { defaultValueString := "" // set default text for all flags except bool flags - // for bool flags text is set only is DisableDefaultText is not + // for bool flags display default text if DisableDefaultText is not // set if bf, ok := f.(*BoolFlag); !ok || !bf.DisableDefaultText { if s := df.GetDefaultText(); s != "" {