Skip to content

Is it possible to get the default value from the value of another flag? #1914

Answered by kbknapp
bjeanes asked this question in Q&A
Discussion options

You must be logged in to vote

There isn't a current way to do that in clap code, however doing so in consumer code is fairly easy:

    let matches = App::new("test")
        .arg(Arg::with_name("foo").long("foo").default_value("99"))
        .arg(Arg::with_name("bar").long("bar").takes_value(true))
        .get_matches();

    let foo = matches.value_of("foo").unwrap();
    let bar = matches.value_of("bar").unwrap_or(foo);

Edit: simplified

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
1 reply
@CreepySkeleton
Comment options

Comment options

You must be logged in to vote
3 replies
@bjeanes
Comment options

@kbknapp
Comment options

@bjeanes
Comment options

Answer selected by pksunkara
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants