These are chat archives for kbknapp/clap-rs

29th
Mar 2018
Mikhail Trishchenkov
@kriomant
Mar 29 2018 04:45

Nope, value_t!'s return type is not Option, it returns error when argument is not present (from docs: “There are two types of errors, parse failures and those where the argument wasn't present (such as a non-required argument)”), so I have to do:

let opt: Option<usize> = match value_t!(matches.value_of("opt"), usize) {
        Ok(v) => Some(v),
        Err(clap::Error {kind: clap::ErrorKind::ArgumentNotFound, ..}) => None,
        Err(e) => e.exit(),
    };

I would prefer:

let opt: Option<usize> = opt_value_t!(matches.value_of("opt"), usize).unwrap_or_else(|e| e.exit());

Of course, I can write such macro myself, but it seems it would be useful addition to clap.

Kevin K.
@kbknapp
Mar 29 2018 20:25
aah ok I was mistaken, sorry!
There isn't a particular reason this hasn't been added, other than it hasn't been asked for before
I'm not against adding a macro to do that, however I can't promise it'll happen super quick as I'm trying to feature freeze v2 (at least on a personal level) to get v3 completed
You could also wrap value_t! in a macro of your own as well, similar to how ripgrep wraps a lot of the clap args with custom structs to default the functionality, or other such customizations.
There is also the structopt crate (which uses clap internally so you're not losing any functionality), and I believe it basically does what you're looking for
structopt is even merging with clap v3, so so there shouldn't be any concerns about splitting into a "fourth" party crate or anything