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

Support optional arguments for options #188

Closed
sphynx opened this issue May 27, 2019 · 9 comments · Fixed by #190
Closed

Support optional arguments for options #188

sphynx opened this issue May 27, 2019 · 9 comments · Fixed by #190
Labels
enhancement We would love to have this feature! Feel free to supply a PR

Comments

@sphynx
Copy link
Contributor

sphynx commented May 27, 2019

Sometimes options have an optional argument, for example in rustfmt we can use either --help or --help=<topic>.

In order to achieve this we can use Arg::min_values(0) in clap (as described here: clap-rs/clap#892), but there is no way to directly specify it in structopt. Using Option<String> type for a field would fail to distinguish between no option provided at all and an option without an argument. It will return None for both.

It looks like a possible way to support it would be to use Option<Option<String>> field type which can lead to min_values(0) in generated clap code.

Note that this type of options is also supported by getopts library:
https://doc.rust-lang.org/1.1.0/getopts/fn.optflagopt.html

This issue seems to be related to #180, but it has Option<Vec<T>> instead of Option<Option<T>>

@sphynx
Copy link
Contributor Author

sphynx commented May 27, 2019

BTW, I can try to implement a fix for this issue, if you think it's not too hard and doesn't lead to any serious changes in the code.

@TeXitoi
Copy link
Owner

TeXitoi commented May 27, 2019

I'll accept a PR adding support of Option<Option<T>>

@TeXitoi TeXitoi added the enhancement We would love to have this feature! Feel free to supply a PR label May 27, 2019
@sphynx
Copy link
Contributor Author

sphynx commented May 27, 2019

OK, I am working on it.

@ErichDonGubler
Copy link

Just so people here are aware: I feel like the changelog notes don't do this nice feature justice! I had no idea why I should care about Option<Option<T>>, but it DOES make sense to see cmd [--opt[=value]]. Perhaps the CHANGELOG could be tweaked so this interesting feature could be telegraphed better? :)

@TeXitoi
Copy link
Owner

TeXitoi commented May 30, 2019

Feel free to open a PR.

@TeXitoi
Copy link
Owner

TeXitoi commented May 30, 2019

If you have ideas of improvement of the doc at the same time, any improvement on this side is very welcome.

@sphynx
Copy link
Contributor Author

sphynx commented May 30, 2019

@ErichDonGubler Thanks for your feedback! Indeed, I think mentioning Option<Option<T>> in the ChangeLog is more like a leak of implementation detail than a proper feature advertisement :) Your formulation is much better, I am happy to change that. Please let me know if you want to do this and send a PR, otherwise I can do it myself.

@ErichDonGubler
Copy link

I'll be able to get to this probably by Wednesday -- but if you want to take care of it before then, consider my interest relinquished. :)

@sphynx
Copy link
Contributor Author

sphynx commented Jun 1, 2019

No worries, I've prepared the pull request with a small update in ChangeLog. Thanks again for your suggestion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement We would love to have this feature! Feel free to supply a PR
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants