Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"--no-" variants of flags #84

Closed
blyxxyz opened this issue Mar 3, 2021 · 1 comment
Closed

"--no-" variants of flags #84

blyxxyz opened this issue Mar 3, 2021 · 1 comment

Comments

@blyxxyz
Copy link
Collaborator

blyxxyz commented Mar 3, 2021

HTTPie lets you negate flags, e.g. --no-check-status cancels out --check-status. While argparse has optional support for that, HTTPie does it by manually processing arguments argparse didn't recognize (plus a few hardcoded cases).

clap doesn't support this, but there's an issue for it (clap-rs/clap#815). It also has an escape hatch for looking at unknown arguments, but I don't know exactly how powerful it is, and I'm worried it would degrade the usual error message.

A downside of HTTPie's current approach (and probably of the escape hatch approach for clap) is that the order of the flags doesn't matter. Normally the last flag counts, so --form --json works like --json and --json --form like --form, but --no-check-status always overrides --check-status, even if it comes first. But that's acceptable, since the typical usage would be to add --no-check-status after an alias that includes --check-status, and the opposite wouldn't come up because there's little reason to use --no-check-status in an alias.

If this is added to clap it might take a long time before it's usable in xh. It'd land in version 3.0.0, and then after that we might have to wait for structopt to support it (or maybe switch to clap_derive).

@ducaale
Copy link
Owner

ducaale commented Mar 3, 2021

I think HTTPie current behavior could be emulated (although a bit tedious) with something like this TeXitoi/structopt#320 (comment)

#[derive(StructOpt)]
struct Opt {
    #[structopt(long, help="enabled debug (disable with --no-debug)")]
    pub debug: bool,
    #[structopt(long, overrides_with="debug", hidden=true)]
    pub _no_debug: bool
}

Edit: I think you would also need to parse(from_flag = std::ops::Not::not) to the _no_debug property.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants