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

Invalid value for option '--xyz': '' is not an int #626

Closed
sebthom opened this issue Feb 8, 2019 · 5 comments
Closed

Invalid value for option '--xyz': '' is not an int #626

sebthom opened this issue Feb 8, 2019 · 5 comments
Labels
theme: parser An issue or change related to the parser

Comments

@sebthom
Copy link

sebthom commented Feb 8, 2019

For the following code:

@Command(name = "foo", defaultValueProvider = FooCommandDefaultValueProvider.class)
public class FooCommand implements Runnable {

    public static class FooCommandDefaultValueProvider implements IDefaultValueProvider {
        public String defaultValue(ArgSpec argSpec) throws Exception {
            if ("DURATION".equals(argSpec.paramLabel())) {
                return "1200";
            }
            return null;
        }
    }

    @Option(names = "--delay", paramLabel = "DURATION", //
        description = "The delay time in seconds.", //
        arity = "0..1" //
    )
    protected Integer delayDuration;

    // ... more code
}

I would expect:

  1. delayDuration is null if option "--delay" is not specified at all. (this works)
  2. delayDuration is 4000 if option "--delay 4000" is specified. (this works)
  3. delayDuration is 1200 if option "--delay" is specified without parameter where 1200 comes from the default value provider. However in that case I get the error:
    Invalid value for option '--duration': '' is not an int
@remkop
Copy link
Owner

remkop commented Feb 9, 2019

Actually, what happens during a call to parse is this:

  1. First all options and positional parameters are reset to their initial values (field value in the declaration)
  2. Next, default values are applied to all options and positional parameters
  3. Finally, the input is parsed which may result in option and positional parameter values being overwritten with values matched from the command line

So, if the option is not specified on the command line, it should get the default value. I’m surprised you are getting null, that should not happen. Can you run with -Dpicocli.trace=DEBUG and post the output?

When the option is specified without a value, it is assigned an empty string. You can use a custom type converter for that option to convert this empty string to the desired value. See also the discussion in #280 about adding some API to make this more convenient.

@sebthom
Copy link
Author

sebthom commented Feb 9, 2019

Regarding: "When the option is specified without a value, it is assigned an empty string."
When I specify artiy='0..1" I am declaring that the parameter of the option is optional, so why would picocli then try to assign an empty string to as parameter value to the option of type integer?

@remkop
Copy link
Owner

remkop commented Feb 9, 2019

The idea (see #279 and #280 for background) is that some applications want to distinguish between the option not being specified at all, and the option being specified without a value.

In the latter case, picocli assigns an empty string. This is documented in the user manual: https://picocli.info/#_optional_values

With the current version of picocli, you need to specify a custom converter to replace the empty string with some legal value. This is not the most convenient way, so we’re looking for a better alternative in #280.

By the way, were you able to verify that the default value is assigned when the option isn’t specified altogether?

@remkop
Copy link
Owner

remkop commented Feb 11, 2019

By the way, any feedback, comments or suggestions related to syntax on #280 are welcome!

@sebthom
Copy link
Author

sebthom commented Feb 11, 2019

I just checked again, you are right, when I don't specify the option, the default value is applied. I must have mixed this up during all the testing.

I am closing this in favor of #280

@sebthom sebthom closed this as completed Feb 11, 2019
@remkop remkop added the theme: parser An issue or change related to the parser label Apr 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
theme: parser An issue or change related to the parser
Projects
None yet
Development

No branches or pull requests

2 participants