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

Custom boolean type converters behave strangely #2270

Closed
remkop opened this issue May 1, 2024 · 1 comment
Closed

Custom boolean type converters behave strangely #2270

remkop opened this issue May 1, 2024 · 1 comment
Labels
theme: parser An issue or change related to the parser type: bug 🐛
Milestone

Comments

@remkop
Copy link
Owner

remkop commented May 1, 2024

As raised on the picocli mailing list by Sven K. (Many thanks!)

Problems

  1. Custom type converters for primitive boolean.class (Boolean.TYPE), registered with CommandLine.registerConverter, appear not to be invoked, resulting in errors that the custom value cannot be converted to boolean.

  2. Custom type converters for java.lang.Boolean, are invoked twice, once with the original value provided on the command line, and a 2nd time with the String value of the conversion result. (E.g. if 123 is converted to true, the converter is called once with 123 and again with true.)

Reproducer project: https://codeberg.org/sven.k/picocli-reproducer
Main class: https://codeberg.org/sven.k/picocli-reproducer/src/branch/master/app/src/main/java/org/example/ConverterDemo.java

Analysis

Problem 2:

The reason why the converter for java.lang.Boolean is invoked twice:
There is special logic for boolean values that determines the boolean value of the command line argument, changes it to its opposite if the option is negatable, converts that boolean value back to a String, and finally again converts that String value to a boolean.

Solution: only convert if necessary (if the option is actually negatable), and otherwise retain the original command line value.
(Note that the issue still exists - but seems unavoidable - for negatable options; this should be documented somewhere.)

Problem 1:

Again, the special logic for boolean values is the culprit.
When determining the boolean value of the command line argument, the converter used is that for java.lang.Boolean, regardless of the actual type of the option.

Solution: obtain the type of the option to get the converter, instead of hard-coding the converter to be for java.lang.Boolean.

@remkop remkop added type: bug 🐛 theme: parser An issue or change related to the parser labels May 1, 2024
@remkop remkop added this to the 4.7.6 milestone May 1, 2024
remkop added a commit that referenced this issue May 6, 2024
@remkop
Copy link
Owner Author

remkop commented May 6, 2024

Fixed in main. Thank you for raising this, Sven!

@remkop remkop closed this as completed May 6, 2024
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 type: bug 🐛
Projects
None yet
Development

No branches or pull requests

1 participant