-
Notifications
You must be signed in to change notification settings - Fork 429
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
Flag to treat first positional parameter as end-of-options #284
Comments
Yes, that looks strange. I’ll take a look. |
To me, the first result is unexpected (and wrong). A command like this:
Should result in
It appears that the The second result looks correct. Command line arguments that look like an option should result in UnmatchedParameterException (unless |
It depends. I was looking exactly for this behaviour and the problem is that last example. Let me give some other details about by use case: I need to implement a command line which behave somehow similar to the one of docker ie.
Everything after the |
That sounds a bit like the use case described in #215. To implement that, I propose to add a Would this meet your requirements? About the first result, I do intend to fix that since the current behaviour does not follow the specification as described in the Mixing Options and Positional Parameters section of the user manual. |
Nearly. But the proposed |
In alternative it should be possible to disable the Mixing options and position parameters feature to allow the implementation of a positional parameter command line. |
Ok, that may be a good idea. By the way, I want to understand your use case better. Can you give a few examples of the application you are implementing with support for 3rd party commands? In addition, an example of how a user typo could give wrong parsing result would also be useful. |
Think about Docker, for example:
The In my understanding the |
(FYI messages crossed. I posted this before seeing your last message.) In your first example above, I assume that your intention was to capture the 3rd party command name and its arguments in the Instead, ensure that your command only has options and positional parameters that apply to your command. Avoid any potentially ambiguous options or positional parameters with variable arity ( Then the 3rd party command name and its arguments will be captured in the class Cmd {
@Option(names = "--alpha") String alpha;
@Parameters(index = "0", arity = "1") String foo;
public static void main(String... args) {
CommandLine commandLine = new CommandLine(new Cmd())
.setStopOnUnmatched(true);
commandLine.parse(args);
List<String> unmatched = commandLine.getUnmatchedArguments();
invokeThirdPartyCommand(unmatched);
}
} |
Thanks for the docker example. That clarifies the spelling mistake concern you mentioned. I see how you are better served with an option that disables the Mixing options and position parameters behaviour. Something like stopAtPositionalWhen this option is enabled, the parser interprets the first positional parameter as "end of options" so the remaining arguments are all treated as positional parameters. The command must have a multi-value (array, Collection or Map) field to store the positional parameters. If the command does not have a multi-value field, or the field's stopAtUnmatched
Unmatched command line arguments are stored in the |
The |
Fixed in master and 2.x branch. Please verify. |
Released in 2.3.0 |
Take in consideration a command as shown below:
It parses correctly a command line like:
resulting:
Instead the command line:
throws the exception:
Is this a feature or a bug?
I'm more inclined to the latter, also because I've noticed that when a command does not specify any double
-
option it is able to parse the above command line without reporting the unmatched argument exception.I know that I could bypass that exception using a
--
separator, however in any case it looks a bit incoherent behaviour.The text was updated successfully, but these errors were encountered: