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

How to detect when String option was specified without value #279

Closed
pditommaso opened this issue Feb 3, 2018 · 12 comments
Closed

How to detect when String option was specified without value #279

pditommaso opened this issue Feb 3, 2018 · 12 comments

Comments

@pditommaso
Copy link
Contributor

pditommaso commented Feb 3, 2018

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:

@Command(name='sample')
class Sample {
  @Option(names="--foo", aritiy="0..1")
  String foo
}

Now, the use case requires that when the user specifies the --foo parameter w/o a value an implicit value is assigned to foo. 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 ?

@remkop
Copy link
Owner

remkop commented Feb 4, 2018

Can you explain a bit more about your use case? I have trouble understanding when it would be useful to distinguish between

# --foo option not specified:
<command> --bar

and

# --foo option specified without value 
<command> --foo --bar

Won’t the application use the default value in both cases?

@pditommaso
Copy link
Contributor Author

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:

# --foo option not specified:
<command> --bar

Attribute foo is null therefore the associated feature disabled.

<command> --foo --bar

The attribute foo is set with the Option implicit value. Finally:

<command> --foo hello --bar

This allows the user to provide a specific value for foo eg hello.

In a nutshell this allow to allows to implement a flag attribute that by default has no value ie. null, it can be enabled with a implicit (ie. default) value when the user specifies only the parameters name and finally, allows the user to provide a specific value as an optional value.

I haven't found a way to implement this requirement with picocli.

@remkop
Copy link
Owner

remkop commented Feb 4, 2018

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 ParseResult object into your command with the planned @Inject annotation (#259).

@pditommaso
Copy link
Contributor Author

I was think about having an implicit attribute on the Option annotation eg.

class Sample {
  @Option(names="--foo", aritiy="0..1", implicit="hello")
  String foo
}

And use it when the user does not provide a specific value. Would not that be possible?

@remkop
Copy link
Owner

remkop commented Feb 4, 2018

There is some work in progress (#261) to add a defaultValue = "..." attribute to @Option and @Parameters. I think it would be confusing to have an additional implicit attribute that is very close in meaning but subtly different.

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:

  1. --foo option not specified: foo field is set to the default value (null in the above example)
  2. --foo option specified without value: foo field is set to the empty String
  3. --foo option is specified with value: foo field is set to the specified value

I need to think about this some more, but would this work for you?

@pditommaso
Copy link
Contributor Author

This would provide a workaround, but I found it a suboptimal solution.

@remkop
Copy link
Owner

remkop commented Feb 4, 2018

All right, how about this? Let’s split this ticket into parts:

  • How to detect when String option was specified without value
  • Customize value assigned when option specified without value

When I finish with the first part I will close this ticket. Can you raise a separate ticket for the second feature?

@remkop remkop changed the title Implicit parameter value How to detect when String option was specified without value Feb 8, 2018
remkop added a commit that referenced this issue Feb 8, 2018
…ment was consumed for optional String parameter
@remkop remkop closed this as completed in 498d36d Feb 9, 2018
@remkop
Copy link
Owner

remkop commented Feb 9, 2018

Pushed to master. Please verify.

@remkop remkop added this to the 3.0 milestone Feb 9, 2018
@pditommaso
Copy link
Contributor Author

pditommaso commented Feb 10, 2018

Is there a snapshot to which I can give a try ?

@remkop
Copy link
Owner

remkop commented Feb 10, 2018

Nothing published. Can you check out master and do gradle build?

@pditommaso
Copy link
Contributor Author

pditommaso commented Feb 10, 2018

Just done! Nice, it work as expected.

So the plan is to be able to customise the implicit value with #280, right? Also, my understanding is that this works only with String fields, the #280 would allow the use on fields of any type ?

remkop added a commit that referenced this issue Feb 11, 2018
@remkop
Copy link
Owner

remkop commented Feb 11, 2018

Merged into the 2.x branch. This will be included in the upcoming 2.3 release.

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