Skip to content

Commit

Permalink
Add negatable output when using short flag names
Browse files Browse the repository at this point in the history
  • Loading branch information
josephschmitt authored and alecthomas committed Mar 1, 2021
1 parent ed24960 commit d48f4e5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
16 changes: 10 additions & 6 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,17 +469,21 @@ func formatFlag(haveShort bool, flag *Flag) string {
name := flag.Name
isBool := flag.IsBool()
if flag.Short != 0 {
flagString += fmt.Sprintf("-%c, --%s", flag.Short, name)
if isBool && flag.Tag.Negatable {
flagString += fmt.Sprintf("-%c, --[no-]%s", flag.Short, name)
} else {
flagString += fmt.Sprintf("-%c, --%s", flag.Short, name)
}
} else {
if haveShort {
if isBool && flag.Tag.Negatable {
if isBool && flag.Tag.Negatable {
if haveShort {
flagString = fmt.Sprintf(" --[no-]%s", name)
} else {
flagString += fmt.Sprintf(" --%s", name)
flagString = fmt.Sprintf("--[no-]%s", name)
}
} else {
if isBool && flag.Tag.Negatable {
flagString = fmt.Sprintf("--[no-]%s", name)
if haveShort {
flagString += fmt.Sprintf(" --%s", name)
} else {
flagString += fmt.Sprintf("--%s", name)
}
Expand Down
6 changes: 3 additions & 3 deletions help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestHelp(t *testing.T) {
Slice []string `help:"A slice of strings." placeholder:"STR"`
Map map[string]int `help:"A map of strings to ints."`
Required bool `required help:"A required flag."`
Sort bool `negatable help:"Is sortable or not."`
Sort bool `negatable short:"s" help:"Is sortable or not."`

One struct {
Flag string `help:"Nested flag."`
Expand Down Expand Up @@ -76,7 +76,7 @@ Flags:
--slice=STR,... A slice of strings.
--map=KEY=VALUE;... A map of strings to ints.
--required A required flag.
--[no-]sort Is sortable or not.
-s, --[no-]sort Is sortable or not.
Commands:
one --required
Expand Down Expand Up @@ -117,7 +117,7 @@ Flags:
--slice=STR,... A slice of strings.
--map=KEY=VALUE;... A map of strings to ints.
--required A required flag.
--[no-]sort Is sortable or not.
-s, --[no-]sort Is sortable or not.
--flag=STRING Nested flag under two.
--required-two
Expand Down

0 comments on commit d48f4e5

Please sign in to comment.