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:
+3
-3
@@ -710,13 +710,13 @@ func (c *Context) parseFlag(flags []*Flag, match string) (err error) {
|
||||
candidates = append(candidates, alias)
|
||||
}
|
||||
|
||||
neg := "--no-" + flag.Name
|
||||
if !matched && !(match == neg && flag.Tag.Negatable) {
|
||||
neg := negatableFlagName(flag.Name, flag.Tag.Negatable)
|
||||
if !matched && match != neg {
|
||||
continue
|
||||
}
|
||||
// Found a matching flag.
|
||||
c.scan.Pop()
|
||||
if match == neg && flag.Tag.Negatable {
|
||||
if match == neg && flag.Tag.Negatable != "" {
|
||||
flag.Negated = true
|
||||
}
|
||||
err := flag.Parse(c.scan, c.getValue(flag.Value))
|
||||
|
||||
Reference in New Issue
Block a user