Skip to content

Support optional arguments for options #188

Closed

Description

@sphynx

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

Activity

  1. sphynx commented on May 27, 2019

    @sphynx
    ContributorAuthor

    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.

  2. TeXitoi commented on May 27, 2019

    @TeXitoi
    Owner

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

  3. added
    enhancementWe would love to have this feature! Feel free to supply a PR
    on May 27, 2019
  4. sphynx commented on May 27, 2019

    @sphynx
    ContributorAuthor

    OK, I am working on it.

  5. ErichDonGubler commented on May 30, 2019

    @ErichDonGubler

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

  6. TeXitoi commented on May 30, 2019

    @TeXitoi
    Owner

    Feel free to open a PR.

  7. TeXitoi commented on May 30, 2019

    @TeXitoi
    Owner

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

  8. sphynx commented on May 30, 2019

    @sphynx
    ContributorAuthor

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

  9. ErichDonGubler commented on May 31, 2019

    @ErichDonGubler

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

  10. sphynx commented on Jun 1, 2019

    @sphynx
    ContributorAuthor

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementWe would love to have this feature! Feel free to supply a PR

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions