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

Mechanism which combines 'adding' and 'positionalList' behavior #10

Closed
JakeWharton opened this issue Apr 2, 2017 · 4 comments
Closed

Comments

@JakeWharton
Copy link

I want to parse a command which takes a series of key/value pairs. From my experiments, adding gives me the ability to accumulate multiple arguments and positionalList gives me the ability to consume the next two arguments after a flag. There doesn't appear to be a way to combine these so that I can accomplish something like the following:

./whatever --kv hey there --kv these are --kv multiple values

Which would somehow allow combining the positionalList and adding behavior into a List<List<String>>.

@xenomachina
Copy link
Owner

Yeah, positionalList is not intended to be associated with an option, so it won't do what you're trying to do here.

There is a way to do what you want using the internal option method. I almost made this method public, but ended up making it internal before the initial release, as its API is kind of ugly and error-prone, and I didn't want to paint myself into a corner by making it public.

I've also been considering cranking down its power a bit before making it public. Most arg parsing APIs have essentially 3 kinds of options: 0-arg, 1-arg, and 0/1-args (aka non-greedy 1-arg -- see #8 ). Right now options actually lets an option consume an arbitrary number of arguments, and decide to stop based on the argument values (with a look ahead of 1), even though none of the public helpers actually use this ability beyond consuming a single argument.

One idea I've had for changing it to be less error-prone, while still exposing more power than is currently available, is to make the user (of options) specify a fixed argument count. Then for your case you'd state that it takes 2-arguments. (I'd also have to special-case the 0/1 non-greedy case.)

In the meantime, would something like a "putting" method that works like adding but consumes 2 (instead of 1) args and puts the key/value pairs into a Map be good enough for your immediate needs? That would be pretty easy to add, and I think would probably be generally useful.

@xenomachina
Copy link
Owner

I'm thinking that I'm going to change option to take an argument count (or possibly an argument name list), and make it public. I'm not going to have a chance to work on this for the next week or so, though.

@JakeWharton
Copy link
Author

Sorry I've been busy and haven't had a chance to consider either putting or take a look at optional. I'm currently doing key=value and adding which works, so anything that lets me do the two argument things after a flag is excellent.

@xenomachina
Copy link
Owner

Version 2.0.0 (just released) makes this possible. I've even added an example in the README (it's also in the tests). Search for "putting" in the README.

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

No branches or pull requests

2 participants