Structopt: Support optional arguments for options

Created on 27 May 2019  路  9Comments  路  Source: TeXitoi/structopt

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: https://github.com/clap-rs/clap/issues/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>>

enhancement

All 9 comments

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.

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

OK, I am working on it.

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? :)

Feel free to open a PR.

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

@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.

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. :)

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SirWindfield picture SirWindfield  路  3Comments

mbrobbel picture mbrobbel  路  5Comments

wez picture wez  路  7Comments

gnzlbg picture gnzlbg  路  5Comments

liamdawson picture liamdawson  路  5Comments