Skip to content

Commit

Permalink
Merge pull request #1633 from dearchap/issue_1277
Browse files Browse the repository at this point in the history
Fix:(issue_1277) Remove default text for version/help flags
  • Loading branch information
dearchap authored Jan 6, 2023
2 parents a6194b9 + ba2a124 commit c891d79
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 31 deletions.
10 changes: 5 additions & 5 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand All @@ -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() {
Expand All @@ -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

}

Expand Down
2 changes: 2 additions & 0 deletions flag-spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ flag_types:
- name: Count
type: int
pointer: true
- name: DisableDefaultText
type: bool
- name: Action
type: "func(*Context, bool) error"
float64:
Expand Down
23 changes: 15 additions & 8 deletions flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 display default text if 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)
Expand Down
16 changes: 10 additions & 6 deletions godoc-current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,8 @@ type BoolFlag struct {

Count *int

DisableDefaultText bool

Action func(*Context, bool) error
// Has unexported fields.
}
Expand Down Expand Up @@ -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

Expand Down
9 changes: 3 additions & 6 deletions help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1366,8 +1366,7 @@ DESCRIPTION:
case
OPTIONS:
--help, -h show help
(default: false)
--help, -h show help
`

if output.String() != expected {
Expand Down Expand Up @@ -1436,8 +1435,7 @@ USAGE:
even more
OPTIONS:
--help, -h show help
(default: false)
--help, -h show help
`

if output.String() != expected {
Expand Down Expand Up @@ -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 {
Expand Down
16 changes: 10 additions & 6 deletions testdata/godoc-v2.x.txt
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,8 @@ type BoolFlag struct {

Count *int

DisableDefaultText bool

Action func(*Context, bool) error
// Has unexported fields.
}
Expand Down Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions zz_generated.flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,8 @@ type BoolFlag struct {

Count *int

DisableDefaultText bool

Action func(*Context, bool) error
}

Expand Down

0 comments on commit c891d79

Please sign in to comment.