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

Pass None to default_value_if, or better adding default_value_if_not_set? #2748

Closed
2 tasks done
runapp opened this issue Aug 31, 2021 · 1 comment
Closed
2 tasks done

Comments

@runapp
Copy link

runapp commented Aug 31, 2021

Please complete the following tasks

  • I have searched the discussions
  • I have searched the existing issues

Clap Version

2.33.3

Describe your use case

I found that I can't make an option that only has default value if another option (doesn't takes value) is not set. The closest way is like this:

    let matches = App::new("test")
        .arg(Arg::with_name("A").short("A"))
        .arg(Arg::with_name("B").short("B").default_value_if("A", None, "").default_value("A not set"))
        .get_matches();
    println!("A={} B={}",
        matches.value_of("A").unwrap_or("###"),
        matches.value_of("B").unwrap_or("###"),
    );

I was thinking of passing None to default_value_if might help emulate as if -B is never set (is_present("B")==false). But default_value_if only accepts &str rather than Option.

Describe the solution you'd like

I don't know which would be better. Modifying current signature of default_value_if seems painful, and might bring endless Some to most common cases. So would add default_value_if_not_set be a choice?

Alternatives, if applicable

No response

Additional Context

No response

@pksunkara
Copy link
Member

pksunkara commented Sep 4, 2021

This is fixed on v3/master through a PR. What search terms did you use when searching for issues like this as requested?

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