Flag: Negatable
Negatable Flag with `--no-` prefix
Negatable Flag
Normal Style
A negatable flag like --warning has a companion buddy --no-warning.
The two flags are just the opposite.
While end user typed --no-warning, the app.cmd.no-warning key in the Store will be set to true, on the contrary, app.cmd.warning key will be set to false.
Internally, these two flags are in a same Toggleable Group.
A negatable Flag always holds a boolean Default value, in generally,
-W Style
This new feature starts since cmdr v2.1.16.
A -W-style negatable flag allows you provide a set of child items.
cmdr will create a pair of toggable group flags for these child items. For example, the above sample code will make these child flags: --warnings.unused-variable/-Wunused-variable and --warnings.no-unused-variable/-Wno-unused-variable, and so on.
This -W-style negatable flag is covered from gcc -Waddress -Wno-address of GNU GCC command line options.
End-user can pass command-line arguments with -Wunused-variable to enable warnings.unused-variable option (above), or disable it with -Wno-unused-variable. Another way is using short flag -Wunused-variable and -Wno-unused-variable.
In the latest version of cmdr.v2, we also improved the help screen of a negatable flag, just like the following sample:
You've seen -W flag has the extra lines to list all of its sub-items.
Tip
When displaying a help screen (--help), specifying some flags and you've get their updated default-values to show there.
For example like above, we specified -w in the command-line, so its default-value has been updated to true in the help screen.
BTW, since a negatable flag would be implemented as a togglable flag internally, its right-part description will be leading with [x] or [ ], which the first form means a true-state choiced, and the last form means a false-state kept.
Define
To define negatable flag by .Negatable(true).Default(false) call.
And by .Negatable(true, "a","b","c").Default(false) call, a -W-style flag(-set) can be defined.
Run
The example app has these following outputs:
额外的话题
How is this guide?
Last updated on