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

Documentation: Update boolean arity #1400

Merged
merged 1 commit into from
Aug 4, 2021
Merged

Documentation: Update boolean arity #1400

merged 1 commit into from
Aug 4, 2021

Conversation

joca-bt
Copy link
Contributor

@joca-bt joca-bt commented Aug 1, 2021

The default arity for boolean options is 0..1 and no longer 0.

Closes #1398.

@joca-bt
Copy link
Contributor Author

joca-bt commented Aug 1, 2021

There might be some doc missing. @remkop

I am assuming the html is somehow autogenerated? Or do I need to update it as well?

@remkop
Copy link
Owner

remkop commented Aug 2, 2021

Some background on how this is implemented:
OptionSpec (and PositionalParamSpec) have a field called arity of type Range.

If a positional parameter is defined without explicitly setting the arity, picocli automatically assigns arity="1".
If a named option is defined without explicitly setting the arity, picocli automatically assigns arity="0" for booleans, and "1" for any other type. (Even though, as you found, effectively the arity for booleans is "0..1". Keep reading...)
However, in both cases, the arity Range is marked as "unspecified=true".

During parsing, picocli creates a "derived arity" for each option it recognizes: named options with a value attached to the option name will have derived arity="1". For example, --file=/tmp/abc, or -fMyFile.

Picocli uses this "derived arity" if the option was defined without an explicit arity (so when "unspecified=true").

Effectively, this means that from a validation point of view, booleans have arity="0..1".
(It kind of grew like this over time... Perhaps this can be simplied, but would not want to impact behaviour...)

Anyway, that is why the docs say that arity=0 for booleans, when arity=0..1 is more accurate. (It does not tell the whole story, but I guess that is okay.)
And for positional parameters, arity must be "1", regardless of type.

@remkop remkop added this to the 4.6.2 milestone Aug 2, 2021
Copy link
Owner

@remkop remkop left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we are almost there.
I have a few suggested changes. Can you take a look?

src/main/java/picocli/CommandLine.java Show resolved Hide resolved
@@ -3746,22 +3746,12 @@ private static String format(String formatString, Object... params) {
* </p>
* <b>A note on boolean options</b>
* <p>
* By default picocli does not expect boolean options (also called "flags" or "switches") to have a parameter.
* You can make a boolean option take a required parameter by annotating your field with {@code arity="1"}.
* By default picocli allows boolean options (also called "flags" or "switches") to have an optional parameter.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small addition:

(...) an optional parameter, which must be either true or false.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this one is very redundant, it's a boolean option.

Copy link
Owner

@remkop remkop Aug 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you are saying, but I think it makes sense to be very specific about what parameters are allowed. For example, the following parameters would be rejected: TRUE, FALSE, True, False, 1, 0, YES, NO, Yes, No.

Contrast this to some languages like Groovy and Python where Strings with this wider range of values are translated to true/false boolean values.

src/main/java/picocli/CommandLine.java Show resolved Hide resolved
The default arity for boolean options is 0..1 and no longer 0.
@remkop remkop merged commit e27485a into remkop:master Aug 4, 2021
remkop added a commit that referenced this pull request Aug 4, 2021
remkop added a commit that referenced this pull request Aug 4, 2021
@remkop
Copy link
Owner

remkop commented Aug 4, 2021

Merged.
Thank you for the pull request and thank you for your patience!

MarkoMackic pushed a commit to MarkoMackic/picocli that referenced this pull request Oct 17, 2021
The default arity for boolean options is 0..1 and no longer 0.
MarkoMackic pushed a commit to MarkoMackic/picocli that referenced this pull request Oct 17, 2021
MarkoMackic pushed a commit to MarkoMackic/picocli that referenced this pull request Oct 17, 2021
MarkoMackic added a commit to MarkoMackic/picocli that referenced this pull request Oct 17, 2021
MarkoMackic added a commit to MarkoMackic/picocli that referenced this pull request Oct 17, 2021
MarkoMackic added a commit to MarkoMackic/picocli that referenced this pull request Oct 17, 2021
MarkoMackic added a commit to MarkoMackic/picocli that referenced this pull request Oct 17, 2021
MarkoMackic added a commit to MarkoMackic/picocli that referenced this pull request Oct 17, 2021
MarkoMackic added a commit to MarkoMackic/picocli that referenced this pull request Oct 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Behaviour doesn't match documentation for boolean Option arity
2 participants