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

Add examples for Default values for arguments #418

Closed
Fiedzia opened this issue Feb 5, 2016 · 10 comments
Closed

Add examples for Default values for arguments #418

Fiedzia opened this issue Feb 5, 2016 · 10 comments
Labels
A-docs Area: documentation, including docs.rs, readme, examples, etc... C-enhancement Category: Raise on the bar on expectations
Milestone

Comments

@Fiedzia
Copy link

Fiedzia commented Feb 5, 2016

Its really excellent library, but I am missing ability to define default value for arguments. Could this be added?

@sru
Copy link
Contributor

sru commented Feb 5, 2016

Since value_of from ArgMatches struct returns Option, you can use unwrap_or function. i.e.

let s = matches.value_of("something").unwrap_or("default");

@Fiedzia
Copy link
Author

Fiedzia commented Feb 5, 2016

Sure, but if I'm accessing this in 5 places (say every time I'm checking verbosity), I'll have to define default value in those 5 places, and in documentation, and then make sure all those places are in sync. Having it defined once would be nicer.

@sru
Copy link
Contributor

sru commented Feb 5, 2016

You don't have to define those default value in 5 different places, you'd just define one constant and use it over those five different places. I believe having default value has been brought up previously (#367 briefly). I think adding default value would be simple enough addition though I am skeptical if it is actually needed. Need @kbknapp to chime in.

@kbknapp
Copy link
Member

kbknapp commented Feb 5, 2016

I'm not opposed to adding it, as with everything else in clap it'd be opt in and there's really no overhead to using it or it being there if you don't use it. Short of a quick cycle through the defaults if they're not already provided.

Like @sru said I personally think there are ways get by without it, but it does seem to be something that I'm asked about from time to time.

I'll add this to issue tracker for implementing.

Thanks for taking the time to file this ;)

@kbknapp kbknapp added T: new feature A-parsing Area: Parser's logic and needs it changed somehow. E-medium Call for participation: Experience needed to fix: Medium / intermediate and removed T: RFC / question labels Feb 5, 2016
@kbknapp
Copy link
Member

kbknapp commented Feb 9, 2016

I have a local branch with this implemented - once #422 merges I'll put in the PR. Just waiting because this feature bumps the minor version, but #422 does not.

@jpastuszek
Copy link

I am also looking for default value support.
Ideally you just define it with .default("") on Arg.
Also it should be displayed in help message in square brackets (?) after help string using Display trait or alternatively if .default_label("") is used it should display provided string instead.

This is what I do now:

    -c, --data-processor-url <URL>     Nanomsg URL to data processor [ipc:///tmp/rdms_data_store.ipc]
    let url = value_t!(args, "data-processor-url", Url).unwrap_or_else(|err|
        match err.kind {
            clap::ErrorKind::ArgumentNotFound => FromStr::from_str("ipc:///tmp/rdms_data_store.ipc").unwrap(),
            _ => err.exit()
        }
    );

Is there a better way?

@kbknapp
Copy link
Member

kbknapp commented Feb 14, 2016

@jpastuszek The just merged soon to merge #423 includes this. It contains a new Arg::default_value method for setting the value when the argument is not used at runtime.

The next PR commit will add the adding to help text automatically, good idea.

Edit: after the merge I'll upload v2.1.0 to crates.io

@homu homu closed this as completed in 7321195 Feb 14, 2016
@davidmyersdev
Copy link

@kbknapp is there any way we can get this added to the docs or the examples? I feel like this is a pretty useful feature, and it took me a while to find it. Is there an API reference available for the args commands? I'm loving this project so far. Thanks!

@kbknapp
Copy link
Member

kbknapp commented Sep 17, 2016

@Drm2 absolutely!

@kbknapp kbknapp reopened this Sep 17, 2016
@kbknapp kbknapp changed the title Default values for arguments Add examples for Default values for arguments Sep 17, 2016
@kbknapp kbknapp added C-enhancement Category: Raise on the bar on expectations A-docs Area: documentation, including docs.rs, readme, examples, etc... and removed C: args A-parsing Area: Parser's logic and needs it changed somehow. E-medium Call for participation: Experience needed to fix: Medium / intermediate labels Sep 17, 2016
@kbknapp kbknapp added this to the 2.12.2 milestone Sep 17, 2016
@davidmyersdev
Copy link

Wow. Incredible response time. Thank you!

@homu homu closed this as completed in 6b9bd21 Sep 18, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: documentation, including docs.rs, readme, examples, etc... C-enhancement Category: Raise on the bar on expectations
Projects
None yet
Development

No branches or pull requests

5 participants