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:
Cam Hutchison
2024-09-10 21:02:37 +10:00
committed by GitHub
parent 7d84b95294
commit 4ecb53599b
8 changed files with 128 additions and 49 deletions
+3
View File
@@ -71,6 +71,7 @@ func TestHelp(t *testing.T) {
Map map[string]int `help:"A map of strings to ints."`
Required bool `required help:"A required flag."`
Sort bool `negatable short:"s" help:"Is sortable or not."`
Approve bool `negatable:"deny" help:"Approve or deny message."`
One struct {
Flag string `help:"Nested flag."`
@@ -118,6 +119,7 @@ Flags:
--map=KEY=VALUE;... A map of strings to ints.
--required A required flag.
-s, --[no-]sort Is sortable or not.
--approve/deny Approve or deny message.
Commands:
one --required [flags]
@@ -159,6 +161,7 @@ Flags:
--map=KEY=VALUE;... A map of strings to ints.
--required A required flag.
-s, --[no-]sort Is sortable or not.
--approve/deny Approve or deny message.
--flag=STRING Nested flag under two.
--required-two