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

Allow optional values (not simply optional args with value notation) #892

Closed
patcon opened this issue Mar 9, 2017 · 4 comments
Closed

Comments

@patcon
Copy link

patcon commented Mar 9, 2017

From my reading of the docs, this isn't possible, but feel free to correct me!

The distinction being that an optional argument using value notation is currently possible

-b --bump=[type] 'bump the version point automatically (options: patch, minor, major)'

Which would allow all of:

~$ mycommand --some-arg --bump=patch
~$ mycommand --some-arg --bump=major
~$ mycommand --some-arg # doesn't bump version

But I don't think there's a way to allow an optional value, like so:

~$ mycommand --some-arg --bump=patch
~$ mycommand --some-arg --bump # could be same result as above
~$ mycommand --some-arg # doesn't bump version

Any help or guidance is appreciated! If this is already possible, I'm happy to help update the docs to better convey this distinction :)

@kbknapp
Copy link
Member

kbknapp commented Mar 9, 2017

Try setting Arg::min_values(0). This should work, but I also can't remember if I special cased 0 values...

Let me know if that doesn't work 😉

@patcon
Copy link
Author

patcon commented Mar 10, 2017

Thanks so so much bunch, @kbknapp! :)

EDIT: haha "so so much bunch"...

@patcon patcon closed this as completed Mar 10, 2017
@nodakai
Copy link

nodakai commented Oct 15, 2017

It would be great if a brief note about this use case could be added to Arg::min_values().

Also, in this use case the difference between ArgMatches::value_of() returning Some and ArgMatches::is_present() returning true is important, and I think this part of the documentation of value_of() should be revised.

If the option wasn't present at runtime it returns None.

Because it seem to suggest that it returns None iff the option wasn't present.

if matches.is_present("bump") {
  if let Some(v) = matches.value_of("bump") {
    println!("bump == {:?}", v);
  } else {
    println!("bump"); // matters only under `min_values(0)`
  }
} else {
  println!("do not bump");
}

@kbknapp
Copy link
Member

kbknapp commented Oct 16, 2017

@nodakai I'd be good adding that blurb or updating the docs if you could open a new issue for it. Could you point out a link to the sections which should be made more clear? I have the problem of guilty knowledge and it's hard to for me to see which parts aren't exactly clear since I wrote the library and am intimately familiar with it 😜

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

3 participants