Skip to content

Commit

Permalink
adjust tests for HideDefault
Browse files Browse the repository at this point in the history
  • Loading branch information
Skeeve committed Jun 28, 2024
1 parent c4cd0a5 commit b3d3fac
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 19 deletions.
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
18 changes: 11 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 @@ -155,6 +157,7 @@ type Countable interface {
type VisibleFlag interface {
// IsVisible returns true if the flag is not hidden, otherwise false
IsVisible() bool
IsDefaultVisible() bool
}

// CategorizableFlag is an interface that allows us to potentially
Expand Down Expand Up @@ -346,8 +349,9 @@ func stringifyFlag(f Flag) string {
}

defaultValueString := ""
isVisible := f.(VisibleFlag).IsDefaultVisible()

if s := df.GetDefaultText(); s != "" {
if s := df.GetDefaultText(); isVisible && s != "" {
defaultValueString = fmt.Sprintf(formatDefault("%s"), s)
}

Expand Down
6 changes: 6 additions & 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 Expand Up @@ -216,6 +217,11 @@ func (f *FlagBase[T, C, V]) IsVisible() bool {
return !f.Hidden
}

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

// GetCategory returns the category of the flag
func (f *FlagBase[T, C, V]) GetCategory() string {
return f.Category
Expand Down
11 changes: 4 additions & 7 deletions help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1398,8 +1398,7 @@ COMMANDS:
for one command
OPTIONS:
--help, -h show help
(default: false)
--help, -h show help
`,
output.String(),
)
Expand Down Expand Up @@ -1464,8 +1463,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 @@ -1545,8 +1543,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 @@ -1624,7 +1621,7 @@ COMMANDS:
for one command
GLOBAL OPTIONS:
--help, -h show help (default: false)
--help, -h show help
--m2 value
--strd value
Expand Down

0 comments on commit b3d3fac

Please sign in to comment.