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

Default arity should be 1, not *, for array and collection options #192

Closed
remkop opened this issue Sep 28, 2017 · 1 comment
Closed

Default arity should be 1, not *, for array and collection options #192

remkop opened this issue Sep 28, 2017 · 1 comment

Comments

@remkop
Copy link
Owner

remkop commented Sep 28, 2017

Currently if an option for a list or array field doesn't have an explicit arity attribute, the default is to be greedy: consume as many arguments as possible until the next option or until the -- end-of-options delimiter is encountered.

This causes a number of problems:

Example

To illustrate, consider the following application:

class Args {
    @Option(names="-o") List<String> options;
    @Parameters         List<String> positionalParams;

    public static void main(String... args) {
        System.out.printf("Options:    %s%n", options);
        System.out.printf("Positional: %s%n", positionalParams);
    }
}

Current (incorrect)

Currently (as of v1.0.1) this input gives the following output:

<command> -o val1 val2 val3

# current (bad) behavior
Options:    [val1, val2, val3]
Positional: []

This default behavior is counterintuitive for applications that also have positional parameters. It becomes impossible to see where the options stop and the positional parameters begin unless the user specifies the -- end-of-options delimiter.

Correct

Instead, we want the parser to behave like this:

<command> -o val1 val2 val3

# expected behavior
Options:    [val1]
Positional: [val2, val3]

That would be consistent with how most Unix tools work. Most Unix tools would expect the option to consume only one argument. The option can then be specified multiple times in order to collect multiple values. E.g.,

<command> -o val1 -o val2 -o val3

Solution

This ticket proposes to change the default behavior to consume only one argument for list/array options that don't have an explicit arity attribute defined.

Users will be able get the current greedy behavior by explicitly specifying arity="1..*".

This will solve the above problems but may cause disruption for existing users/applications who rely on the current behaviour. I am not happy about that, but I believe this change is necessary to keep moving picocli forward. I hope that bumping the version number to 2.0 to signal an incompatible change will help reduce the disruption.

@remkop remkop added this to the 2.0.0 milestone Sep 28, 2017
@remkop remkop changed the title Default arity should be 1 for array and collection @Options Default arity should be 1 for array and collection options Sep 28, 2017
@remkop remkop changed the title Default arity should be 1 for array and collection options Default arity should be 1, not *, for array and collection options Sep 28, 2017
remkop added a commit that referenced this issue Sep 30, 2017
…reedily consume command line arguments

- #192 Default arity should be 1, not *, for array and collection options
- Improved help message format with regards to expected parameter count
- Closes #186 (Confusing usage message for collection options)
@remkop
Copy link
Owner Author

remkop commented Sep 30, 2017

This has been implemented.
TODO: update user manual.

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

1 participant