I'm using the structopt
crate and I have the following struct:
#[derive(Clone, StructOpt, Debug)]
#[structopt(name = "test")]
pub struct CommandlineOptions {
#[structopt(
long = "length",
help = "The length of the the string to generate",
default_value = "50",
index = 1
)]
pub length: usize,
}
let options = CommandlineOptions::from_args();
If options.length
is 50
, how can I know it comes from the default value 50, or the user provided a value of 50?
I don't think it is possible to do this with
structopt
. The idiomatic way to solve this problem is to instead use anOption<usize>
instead ofusize
(as documented here):If this does not work in your case somehow, you could also directly work with the
clap::ArgMatches
struct (structopt
is little more than macro magic aroundclap
) to check the number of occurrences oflength
withArgMatches::occurrences_of
. However, this wouldn't be very idiomatic.