-
Notifications
You must be signed in to change notification settings - Fork 0
Add support for automatic negation flags #69
Comments
Comment by jacobsvante Great proposal @kbknapp. Having a Perhaps this could even be the default, considering that v3 is still in beta? The user would then have to call |
Comment by pksunkara Not all single flags are need overridable flags. From looking at the clis, this is a minority use case. Which is why this won't be a default behaviour. |
Comment by Emerentius It's actually quite useful if all flags have negation flags because you can effectively switch the default of a flag via aliases or wrappers without hardcoding the behavior, because the user can still override the choice. For some prior art, the very popular python command line argument library |
Comment by Mange I usually also always add a |
Comment by epage Random thoughts:
|
Comment by mkayaalp There are also instances of complementary flags using
|
Comment by epage Though that is a whole different argument scheme and is more related to #1210, especially with my comment. However, that also points out the general challenge with trying to support automatic negation flags: there are a lot of different styles. I think the most important thing is we don't get in the way of people writing their CLI, even if it requires more boiler palte. After that is us making the core workflows and common / best practices easy to take advantage of. I think we should define the scope of what kind of automatic flags we support and limit ourselves to that or else I fear it'll either be too complicated by supporting all types or won't be present for anyone. |
Issue by kbknapp
Saturday Jan 14, 2017 at 02:17 GMT
Originally opened as clap-rs/clap#815
Add a way to automatically generate flags that override (or negate) other flags. This can be done manually already, but doing so for an entire CLI can be tedious, painful, and error prone. Manually doing so will also pollute the
--help
output.This proposal offers a way to automatically have these negation flags generated on a case by case basis, or across all flags in the command. This proposal also offers a way to have these negation flags listed in the
--help
message or hidden.Design
A negation flag would simply take the long version of the regular flag and pre-pend
no
; for exmaple--follow
would get a--no-follow
. If a flag only specifies a short version, theno
would be prepended to the short such as-L
gets--no-L
.When parsing occurs, if a negation flag is found, and the negated argument was used, it functions exactly like a override that is already supported.
Functionally the following two examples are equivilant:
New proposal:
Concerns
There are two primary concerns with this approach.
Flags that already contian "no"
A flag which already starts with
no
such as--no-ignore
would end up getting a doubleno
in the form of--no-no-ignore
. This actually makes sense and is consistent, but looks strange at first glance. An alternative would be to check if a flag starts withno
and simply remove theno
, i.e.--no-ignore
becomes--ignore
but this has the downside of additional processing at runtime, becomes slightly more confusing, and has a higher chance of a conflict.Conflicting names
If a user has selected to auto-generate a negation flag, and the negating flag long conflicts with a flag already in use, a
panic!
will occur. Example,--ignore
and--no-ignore
is already defined elsewhere, and the user has selected to automaticlly generate negation flags, this will cause--ignore
to generate a--no-ignore
flag which already exists causing apanic!
. The fix is to either not use a sweeping setting that applies ot all flags indescriminantly, or to change/remove the already defined--no-ignore
flag.Progress
AppSettings::GenerateNegationFlags
which does the above, but automatically for all flags.AppSettings:GenerateHiddenNegationFlags
which hides all these negation flags.See the discussion in BurntSushi/ripgrep#196
Relates to #748
Edit: Removed
Arg::overridable(bool)
because this can already be done manually by making another flag.The text was updated successfully, but these errors were encountered: