Make negatable flag name customisable (#439)
* fix: Check if negatable duplicates another flag
Add a check for flags with the `negatable` option if the negative flag
conflicts with another tag, such as:
Flag bool `negatable:""`
NoFlag bool
The flag `--no-flag` is ambiguous in this scenario.
* feat: Make negatable flag name customisable
Allow a value on the `negatable` tag to specify a flag name to use for
negation instead of using `--no-<flag-name>` as the flag.
e.g.
Approve bool `default:"true",negatable:"deny"`
This example will allow `--deny` to set the `Approve` field to false.
This commit is contained in:
@@ -491,27 +491,22 @@ func formatFlag(haveShort bool, flag *Flag) string {
|
||||
name := flag.Name
|
||||
isBool := flag.IsBool()
|
||||
isCounter := flag.IsCounter()
|
||||
|
||||
short := ""
|
||||
if flag.Short != 0 {
|
||||
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 isBool && flag.Tag.Negatable {
|
||||
if haveShort {
|
||||
flagString = fmt.Sprintf(" --[no-]%s", name)
|
||||
} else {
|
||||
flagString = fmt.Sprintf("--[no-]%s", name)
|
||||
}
|
||||
} else {
|
||||
if haveShort {
|
||||
flagString += fmt.Sprintf(" --%s", name)
|
||||
} else {
|
||||
flagString += fmt.Sprintf("--%s", name)
|
||||
}
|
||||
}
|
||||
short = "-" + string(flag.Short) + ", "
|
||||
} else if haveShort {
|
||||
short = " "
|
||||
}
|
||||
|
||||
if isBool && flag.Tag.Negatable == negatableDefault {
|
||||
name = "[no-]" + name
|
||||
} else if isBool && flag.Tag.Negatable != "" {
|
||||
name += "/" + flag.Tag.Negatable
|
||||
}
|
||||
|
||||
flagString += fmt.Sprintf("%s--%s", short, name)
|
||||
|
||||
if !isBool && !isCounter {
|
||||
flagString += fmt.Sprintf("=%s", flag.FormatPlaceHolder())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user