Skip to content
This repository was archived by the owner on Jan 1, 2022. It is now read-only.

Multi valued flags and required argument do not work together very well #83

Open
epage opened this issue Dec 6, 2021 · 3 comments
Open

Comments

@epage
Copy link
Owner

epage commented Dec 6, 2021

Issue by rsertelon
Friday Dec 08, 2017 at 23:13 GMT
Originally opened as clap-rs/clap#1125


Rust Version

  • 1.22.1

Affected Version of clap

  • 2.29.0

The habitat project uses clap for its command line parsing. There are currently two issues opened related to how clap generates help message and/or how it parses the command line arguments.

Issues are:

In the source code of habitat, here's the interesting bit.

The subcommand load has both a required @arg PKG_IDENT_OR_ARTIFACT + required + takes_value and a multivalued @arg BIND: --bind +takes_value +multiple

The problem is that the help message prints:

USAGE:
    hab-sup load [FLAGS] [OPTIONS] <PKG_IDENT>

But if you try to actually follow the help message while using the --bind flag and specify the <PKG_IDENT> as the last argument, it won't work as clap thinks it's still a --bind value. However, if --bind argument is placed as the last one, it'll work as expected.

So there's either a discrepancy in the help message, or/and a multivalue parsing that may be to greedy for when there's an expected required argument.

I understand this problem is not really trivial, I've seen an issue that might address this, but it feels weird to have to add a terminator to the --bind flag from a user perspective.

Hope the problem has been made clearly, don't hesitate if you need further information!

@epage
Copy link
Owner Author

epage commented Dec 6, 2021

Comment by kbknapp
Tuesday Jan 09, 2018 at 16:33 GMT


@rsertelon sorry for the long delay! Holidays and travel have had me gone for a while.

Although this isn't a trivial problem (how to tell clap when --bind is done and <PKG_IDENT> starts), there is a trivial fix if it works with your use case.

  • Limit --bind to a single value per occurrence. So for example, --bind value1 --bind value2 is legal, but --bind value1 value2 is not. This is the easiest fix and my recommended solution. To do this, declare --bind like so: @arg BIND: --bind +takes_value +multiple number_of_values(1)

There are some other things you could try like Arg::last(true) (or +last for the macro version). Which will change your usage string to hab-sup load [FLAGS] [OPTIONS] [--] <PKG_IDENT> the downside it that it requires -- to be used to access <PKG_IDENT>.

Finally, you could also just set a custom usage string for that one subcommand, to make it hab-sup load [FLAGS] <PKG_IDENT> [OPTIONS].

@epage
Copy link
Owner Author

epage commented Dec 6, 2021

Comment by kbknapp
Tuesday Jan 09, 2018 at 16:35 GMT


I have some ideas on things that could fix this...but I'm far from implementing them yet and they wouldn't probably happen until v3. I'm thinking of things like filters, where one could say, "This particular arg only accepts values that meet some criteria...if they don't fit, look at other args."

@epage
Copy link
Owner Author

epage commented Dec 6, 2021

Comment by rsertelon
Tuesday Jan 09, 2018 at 21:09 GMT


Hey @kbknapp no problem! Thanks for your answer. I'll discuss this with the dev team at habitat. The workaround might be a good solution, filters might be nice too! :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant