Clap: [Question/Feature] Is There a Way to Make Args that Behave Like `--help`

Created on 12 Dec 2019  路  7Comments  路  Source: clap-rs/clap

I have a situation where I have created a custom --doc flag that works very similarly to the --help flag except it triggers a documentation pager like a manpage. The problem I have is that I have "required" arguments that should not be required when the --doc flag is present. I could usually use required_unless("doc"), but that doesn't work when the --doc flag is passed to a subcommand and the required argument is in a super-command. For example with this app:

App::new("test-app")
    .arg(Arg::with_name("testarg")
        .required_unless("doc")) // Doesn't work because `doc` is in a subcommand
    .subcommand(App::new("subcommand")
        .arg(Arg::with_name("doc")
            .long("doc"))

In the above app I have no way of triggering the --doc argument without passing testarg because testarg is required.

What I need is a way to "pre-check" for --doc flags and handle those first, only continuing like normal after I have verified that I don't need to handle the --doc flag. This is just like the --help flag works, but it is built-in and I can't make another argument follow that behavior.

options new feature

Most helpful comment

OK, it feels a lot like exclusive indeed, and yet it's a bit different. exclusive is about "this option conflicts with every other" but the question is about "this option terminates processing right away".

All 7 comments

I think this is solved by exclusive. I'll check later.

OK, it feels a lot like exclusive indeed, and yet it's a bit different. exclusive is about "this option conflicts with every other" but the question is about "this option terminates processing right away".

I think .global(true).exclusive(true) should handle this case.

Not really. exclusive will go boom if the arg is not the only one, but the expected behavior is to "ignore everything else if the arg is present".

I also want to do this in order to tap into the completions logic and allow users to do something like:

my-cli --completions bash >> ~/.bashrc

However, clap_generate is (understandably) underdocumented at the moment, so y'all might already have plans on standardising how shells get access to those completions.

Traditionally the only way to do something like this was to make --doc a global argument, use required_unless and then in consumer code check for --doc prior to actually doing any real work.

Since exclusive is a 3.x addition, we could downgrade it to override all other arguments instead of hard conflict and error. Reason being, we can't enforce process ending (minus error) without some kind of handler which is still undecided. cc @CreepySkeleton @pksunkara

Was this page helpful?
0 / 5 - 0 ratings

Related issues

casey picture casey  路  25Comments

smklein picture smklein  路  35Comments

pickfire picture pickfire  路  21Comments

CAD97 picture CAD97  路  21Comments

ruabmbua picture ruabmbua  路  17Comments