-
Notifications
You must be signed in to change notification settings - Fork 424
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
How to detect when String option was specified without value #279
Comments
Can you explain a bit more about your use case? I have trouble understanding when it would be useful to distinguish between
and
Won’t the application use the default value in both cases? |
Yes sure, the need for this is to allow the user to enable a feature and optionally to provide an extra parameter value. So for example:
Attribute
The attribute
This allows the user to provide a specific value for In a nutshell this allow to allows to implement a flag attribute that by default has no value ie. I haven't found a way to implement this requirement with picocli. |
Thanks for the clarification. To be honest, I don't see an easy way to accomplish this with annotations. Do you have any suggestions? This may be easier with the programmatic API, although the API for the parse results (#257) is still in a very early stage. I could imagine your use case would look something like this: String[] args = { "--foo" };
ParseResult parseResult = new CommandLine(new Sample()).parseArgs(args);
if (parseResult.hasOption("--foo")) {
enableFooFeature();
File fooFile = parseResult.optionValue("--foo");
setFooFile(fooFile);
} One idea is to inject the |
I was think about having an
And use it when the user does not provide a specific value. Would not that be possible? |
There is some work in progress (#261) to add a One thing I can think of is to set the annotated field to the empty string when the option is specified without a value. That would give:
I need to think about this some more, but would this work for you? |
This would provide a workaround, but I found it a suboptimal solution. |
All right, how about this? Let’s split this ticket into parts:
When I finish with the first part I will close this ticket. Can you raise a separate ticket for the second feature? |
…ment was consumed for optional String parameter
Pushed to master. Please verify. |
Is there a snapshot to which I can give a try ? |
Nothing published. Can you check out master and do |
Merged into the 2.x branch. This will be included in the upcoming 2.3 release. |
I'm wondering if picocli allows the support for implicit value parameter having an arity of
0..1
and not value is specified.To make it clear consider the following definition:
Now, the use case requires that when the user specifies the
--foo
parameter w/o a value an implicit value is assigned tofoo
. The property default is not a valid workaround because it would not allow to distinguish the absence of that parameter.How to handle this use case with picocli ?
The text was updated successfully, but these errors were encountered: