Skip to content

Commit

Permalink
Merge pull request #1966 from mahadzaryab1/fix-inverse-flag-usage
Browse files Browse the repository at this point in the history
fix(bool-with-inverse-flag): fix string printing of bool with inverse flag to properly show inverse prefix
  • Loading branch information
dearchap authored Aug 17, 2024
2 parents fc07a8c + eaa3c9a commit 3110c0e
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 11 deletions.
22 changes: 18 additions & 4 deletions flag_bool_with_inverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ func (parent *BoolWithInverseFlag) inverseName() string {
return parent.InversePrefix + parent.BoolFlag.Name
}

func (parent *BoolWithInverseFlag) inversePrefix() string {
if parent.InversePrefix == "" {
return DefaultInverseBoolPrefix
}

return parent.InversePrefix
}

func (parent *BoolWithInverseFlag) inverseAliases() (aliases []string) {
if len(parent.BoolFlag.Aliases) > 0 {
aliases = make([]string, len(parent.BoolFlag.Aliases))
Expand Down Expand Up @@ -170,11 +178,17 @@ func (parent *BoolWithInverseFlag) Names() []string {
// String implements the standard Stringer interface.
//
// Example for BoolFlag{Name: "env"}
// --env (default: false) || --no-env (default: false)
// --[no-]env (default: false)
func (parent *BoolWithInverseFlag) String() string {
if parent.positiveFlag == nil {
return fmt.Sprintf("%s || --%s", parent.BoolFlag.String(), parent.inverseName())
out := FlagStringer(parent)
i := strings.Index(out, "\t")

prefix := "--"

// single character flags are prefixed with `-` instead of `--`
if len(parent.Name) == 1 {
prefix = "-"
}

return fmt.Sprintf("%s || %s", parent.positiveFlag.String(), parent.negativeFlag.String())
return fmt.Sprintf("%s[%s]%s%s", prefix, parent.inversePrefix(), parent.Name, out[i:])
}
80 changes: 77 additions & 3 deletions flag_bool_with_inverse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,84 @@ func TestBoolWithInverseNames(t *testing.T) {
require.Len(t, names, 2)
require.Equal(t, "env", names[0], "expected first name to be `env`")
require.Equal(t, "no-env", names[1], "expected first name to be `no-env`")
}

func TestBoolWithInverseString(t *testing.T) {
tcs := []struct {
testName string
flagName string
required bool
usage string
inversePrefix string
expected string
}{
{
testName: "empty inverse prefix",
flagName: "",
required: true,
expected: "--[no-]\t",
},
{
testName: "single-char flag name",
flagName: "e",
required: true,
expected: "-[no-]e\t",
},
{
testName: "multi-char flag name",
flagName: "env",
required: true,
expected: "--[no-]env\t",
},
{
testName: "required with usage",
flagName: "env",
required: true,
usage: "env usage",
expected: "--[no-]env\tenv usage",
},
{
testName: "required without usage",
flagName: "env",
required: true,
expected: "--[no-]env\t",
},
{
testName: "not required with default usage",
flagName: "env",
required: false,
expected: "--[no-]env\t(default: false)",
},
{
testName: "custom inverse prefix",
flagName: "env",
required: true,
inversePrefix: "nope-",
expected: "--[nope-]env\t",
},
{
testName: "empty inverse prefix",
flagName: "env",
required: true,
inversePrefix: "",
expected: "--[no-]env\t",
},
}

for _, tc := range tcs {
t.Run(tc.testName, func(t *testing.T) {
flag := &BoolWithInverseFlag{
BoolFlag: &BoolFlag{
Name: tc.flagName,
Usage: tc.usage,
Required: tc.required,
},
InversePrefix: tc.inversePrefix,
}

flagString := flag.String()
require.Contains(t, flagString, "--env")
require.Contains(t, flagString, "--no-env")
require.Equal(t, tc.expected, flag.String())
})
}
}

func TestBoolWithInverseDestination(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions godoc-current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,7 @@ func (parent *BoolWithInverseFlag) RunAction(ctx context.Context, cmd *Command)
func (parent *BoolWithInverseFlag) String() string
String implements the standard Stringer interface.

Example for BoolFlag{Name: "env"} --env (default: false) || --no-env
(default: false)
Example for BoolFlag{Name: "env"} --[no-]env (default: false)

func (parent *BoolWithInverseFlag) Value() bool

Expand Down
3 changes: 1 addition & 2 deletions testdata/godoc-v3.x.txt
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,7 @@ func (parent *BoolWithInverseFlag) RunAction(ctx context.Context, cmd *Command)
func (parent *BoolWithInverseFlag) String() string
String implements the standard Stringer interface.

Example for BoolFlag{Name: "env"} --env (default: false) || --no-env
(default: false)
Example for BoolFlag{Name: "env"} --[no-]env (default: false)

func (parent *BoolWithInverseFlag) Value() bool

Expand Down

0 comments on commit 3110c0e

Please sign in to comment.