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

adjust tests for HideDefault #1937

Merged
merged 11 commits into from
Jun 29, 2024
11 changes: 11 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3919,6 +3919,7 @@ func TestJSONExportCommand(t *testing.T) {
"usage": "",
"required": false,
"hidden": false,
"hideDefault": false,
"persistent": false,
"defaultValue": "",
"aliases": [
Expand All @@ -3938,6 +3939,7 @@ func TestJSONExportCommand(t *testing.T) {
"usage": "some usage text",
"required": false,
"hidden": false,
"hideDefault": false,
"persistent": false,
"defaultValue": false,
"aliases": [
Expand Down Expand Up @@ -3977,6 +3979,7 @@ func TestJSONExportCommand(t *testing.T) {
"usage": "",
"required": false,
"hidden": false,
"hideDefault": false,
"persistent": false,
"defaultValue": "",
"aliases": [
Expand All @@ -3996,6 +3999,7 @@ func TestJSONExportCommand(t *testing.T) {
"usage": "another usage text",
"required": false,
"hidden": false,
"hideDefault": false,
"persistent": false,
"defaultValue": false,
"aliases": [
Expand Down Expand Up @@ -4153,6 +4157,7 @@ func TestJSONExportCommand(t *testing.T) {
"usage": "some usage text",
"required": false,
"hidden": false,
"hideDefault": false,
"persistent": false,
"defaultValue": false,
"aliases": [
Expand Down Expand Up @@ -4192,6 +4197,7 @@ func TestJSONExportCommand(t *testing.T) {
"usage": "",
"required": false,
"hidden": false,
"hideDefault": false,
"persistent": false,
"defaultValue": "",
"aliases": [
Expand All @@ -4211,6 +4217,7 @@ func TestJSONExportCommand(t *testing.T) {
"usage": "another usage text",
"required": false,
"hidden": false,
"hideDefault": false,
"persistent": false,
"defaultValue": false,
"aliases": [
Expand Down Expand Up @@ -4250,6 +4257,7 @@ func TestJSONExportCommand(t *testing.T) {
"usage": "some 'usage' text",
"required": false,
"hidden": false,
"hideDefault": false,
"persistent": false,
"defaultValue": "value",
"aliases": [
Expand All @@ -4268,6 +4276,7 @@ func TestJSONExportCommand(t *testing.T) {
"usage": "",
"required": false,
"hidden": false,
"hideDefault": false,
"persistent": false,
"defaultValue": "",
"aliases": [
Expand All @@ -4287,6 +4296,7 @@ func TestJSONExportCommand(t *testing.T) {
"usage": "another usage text",
"required": false,
"hidden": false,
"hideDefault": false,
"persistent": false,
"defaultValue": false,
"aliases": [
Expand All @@ -4305,6 +4315,7 @@ func TestJSONExportCommand(t *testing.T) {
"usage": "",
"required": false,
"hidden": true,
"hideDefault": false,
"persistent": false,
"defaultValue": false,
"aliases": null,
Expand Down
10 changes: 5 additions & 5 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ func ExampleCommand_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 ExampleCommand_Run_commandHelp() {
Expand Down Expand Up @@ -190,7 +190,7 @@ func ExampleCommand_Run_commandHelp() {
// help, h Shows a list of commands or help for one command
//
// OPTIONS:
// --help, -h show help (default: false)
// --help, -h show help
}

func ExampleCommand_Run_noAction() {
Expand All @@ -211,7 +211,7 @@ func ExampleCommand_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 ExampleCommand_Run_subcommandNoAction() {
Expand Down Expand Up @@ -243,7 +243,7 @@ func ExampleCommand_Run_subcommandNoAction() {
// This is how we describe describeit the function
//
// OPTIONS:
// --help, -h show help (default: false)
// --help, -h show help
}

func ExampleCommand_Run_shellComplete_bash_withShortFlag() {
Expand Down
26 changes: 19 additions & 7 deletions flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,20 @@ var GenerateShellCompletionFlag 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",
HideDefault: 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",
HideDefault: true,
}

// FlagStringer converts a flag definition to a string. This is used by help
Expand Down Expand Up @@ -135,6 +137,10 @@ type DocGenerationFlag interface {

// GetEnvVars returns the env vars for this flag
GetEnvVars() []string

// IsDefaultVisible returns whether the default value should be shown in
// help text
IsDefaultVisible() bool
}

// DocGenerationMultiValueFlag extends DocGenerationFlag for slice/map based flags.
Expand Down Expand Up @@ -173,6 +179,11 @@ type PersistentFlag interface {
IsPersistent() bool
}

// IsDefaultVisible returns true if the flag is not hidden, otherwise false
func (f *FlagBase[T, C, V]) IsDefaultVisible() bool {
return !f.HideDefault
}

func newFlagSet(name string, flags []Flag) (*flag.FlagSet, error) {
set := flag.NewFlagSet(name, flag.ContinueOnError)

Expand Down Expand Up @@ -349,7 +360,8 @@ func stringifyFlag(f Flag) string {

// don't print default text for required flags
if rf, ok := f.(RequiredFlag); !ok || !rf.IsRequired() {
if s := df.GetDefaultText(); s != "" {
isVisible := df.IsDefaultVisible()
if s := df.GetDefaultText(); isVisible && s != "" {
defaultValueString = fmt.Sprintf(formatDefault("%s"), s)
}
}
Expand Down
1 change: 1 addition & 0 deletions flag_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type FlagBase[T any, C any, VC ValueCreator[T, C]] struct {
Name string `json:"name"` // name of the flag
Category string `json:"category"` // category of the flag, if any
DefaultText string `json:"defaultText"` // default text of the flag for usage purposes
HideDefault bool `json:"hideDefault"` // whether to hide the default value in output
Usage string `json:"usage"` // usage string for help output
Sources ValueSourceChain `json:"-"` // sources to load flag value from
Required bool `json:"required"` // whether the flag is required or not
Expand Down
22 changes: 16 additions & 6 deletions godoc-current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,10 @@ type DocGenerationFlag interface {

// GetEnvVars returns the env vars for this flag
GetEnvVars() []string

// IsDefaultVisible returns whether the default value should be shown in
// help text
IsDefaultVisible() bool
}
DocGenerationFlag is an interface that allows documentation generation for
the flag
Expand Down Expand Up @@ -620,25 +624,28 @@ var GenerateShellCompletionFlag Flag = &BoolFlag{
GenerateShellCompletionFlag enables shell completion

var HelpFlag Flag = &BoolFlag{
Name: "help",
Aliases: []string{"h"},
Usage: "show help",
Name: "help",
Aliases: []string{"h"},
Usage: "show help",
HideDefault: 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",
HideDefault: true,
}
VersionFlag prints the version for the application

type FlagBase[T any, C any, VC ValueCreator[T, C]] struct {
Name string `json:"name"` // name of the flag
Category string `json:"category"` // category of the flag, if any
DefaultText string `json:"defaultText"` // default text of the flag for usage purposes
HideDefault bool `json:"hideDefault"` // whether to hide the default value in output
Usage string `json:"usage"` // usage string for help output
Sources ValueSourceChain `json:"-"` // sources to load flag value from
Required bool `json:"required"` // whether the flag is required or not
Expand Down Expand Up @@ -684,6 +691,9 @@ func (f *FlagBase[T, C, V]) GetValue() string
GetValue returns the flags value as string representation and an empty
string if the flag takes no value at all.

func (f *FlagBase[T, C, V]) IsDefaultVisible() bool
IsDefaultVisible returns true if the flag is not hidden, otherwise false

func (f *FlagBase[T, C, VC]) IsMultiValueFlag() bool
IsMultiValueFlag returns true if the value type T can take multiple values
from cmd line. This is true for slice and map type flags
Expand Down
13 changes: 5 additions & 8 deletions help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ COMMANDS:

GLOBAL OPTIONS:
--foo value, -f value
--help, -h show help (default: false)
--help, -h show help
`

assert.Contains(t, output.String(), expected,
Expand Down Expand Up @@ -1428,8 +1428,7 @@ COMMANDS:
for one command

OPTIONS:
--help, -h show help
(default: false)
--help, -h show help
`,
output.String(),
)
Expand Down Expand Up @@ -1494,8 +1493,7 @@ USAGE:
even more

OPTIONS:
--help, -h show help
(default: false)
--help, -h show help
`

assert.Equal(t, expected, output.String(), "Unexpected wrapping")
Expand Down Expand Up @@ -1575,8 +1573,7 @@ COMMANDS:
OPTIONS:
--test-f value my test
usage
--help, -h show help
(default: false)
--help, -h show help
`,
output.String(),
)
Expand Down Expand Up @@ -1654,7 +1651,7 @@ COMMANDS:
for one command

GLOBAL OPTIONS:
--help, -h show help (default: false)
--help, -h show help
--m2 value
--strd value

Expand Down
22 changes: 16 additions & 6 deletions testdata/godoc-v3.x.txt
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,10 @@ type DocGenerationFlag interface {

// GetEnvVars returns the env vars for this flag
GetEnvVars() []string

// IsDefaultVisible returns whether the default value should be shown in
// help text
IsDefaultVisible() bool
}
DocGenerationFlag is an interface that allows documentation generation for
the flag
Expand Down Expand Up @@ -620,25 +624,28 @@ var GenerateShellCompletionFlag Flag = &BoolFlag{
GenerateShellCompletionFlag enables shell completion

var HelpFlag Flag = &BoolFlag{
Name: "help",
Aliases: []string{"h"},
Usage: "show help",
Name: "help",
Aliases: []string{"h"},
Usage: "show help",
HideDefault: 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",
HideDefault: true,
}
VersionFlag prints the version for the application

type FlagBase[T any, C any, VC ValueCreator[T, C]] struct {
Name string `json:"name"` // name of the flag
Category string `json:"category"` // category of the flag, if any
DefaultText string `json:"defaultText"` // default text of the flag for usage purposes
HideDefault bool `json:"hideDefault"` // whether to hide the default value in output
Usage string `json:"usage"` // usage string for help output
Sources ValueSourceChain `json:"-"` // sources to load flag value from
Required bool `json:"required"` // whether the flag is required or not
Expand Down Expand Up @@ -684,6 +691,9 @@ func (f *FlagBase[T, C, V]) GetValue() string
GetValue returns the flags value as string representation and an empty
string if the flag takes no value at all.

func (f *FlagBase[T, C, V]) IsDefaultVisible() bool
IsDefaultVisible returns true if the flag is not hidden, otherwise false

func (f *FlagBase[T, C, VC]) IsMultiValueFlag() bool
IsMultiValueFlag returns true if the value type T can take multiple values
from cmd line. This is true for slice and map type flags
Expand Down
Loading