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

Processing of clustered short options seems to be broken on HEAD (846ace0). #226

Closed
RobertZenz opened this issue Nov 1, 2017 · 2 comments
Milestone

Comments

@RobertZenz
Copy link
Contributor

RobertZenz commented Nov 1, 2017

With the latest HEAD (846ace0 as of writing this) processing clustered short options seems to be broken. Assume that I have a Command class with three flags, like this:

private static class Options {
    @Option(names = "-b")
    private boolean buffered = false;

    @Option(names = "-o")
    private boolean overwriteOutput = true;

    @Option(names = "-v")
    private boolean verbose = false;
}

With the input of -bov the following exception is thrown:

Exception in thread "main" picocli.CommandLine$ParameterException: EmptyStackException: null while processing argument at or before arg[0] '-bov' in [-bov]: java.util.EmptyStackException
	at picocli.CommandLine$ParameterException.create(CommandLine.java:4613)
	at picocli.CommandLine$ParameterException.access$0(CommandLine.java:4610)
	at picocli.CommandLine$Interpreter.parse(CommandLine.java:1978)
	at picocli.CommandLine$Interpreter.parse(CommandLine.java:1955)
	at picocli.CommandLine.parse(CommandLine.java:346)
	at picocli.CommandLine.populateCommand(CommandLine.java:327)
	at picocli.FlagsMain.main(FlagsMain.java:72)
Caused by: java.util.EmptyStackException
	at java.util.Stack.peek(Stack.java:102)
	at java.util.Stack.pop(Stack.java:84)
	at picocli.CommandLine$Interpreter.processClusteredShortOptions(CommandLine.java:2172)
	at picocli.CommandLine$Interpreter.processArguments(CommandLine.java:2059)
	at picocli.CommandLine$Interpreter.parse(CommandLine.java:1972)
	... 4 more

Form what I can gather, that seems to be because the processing of clustered options is not exiting when there are no more clustered options. I fixed it locally by using the following patch:

diff --git a/src/main/java/picocli/CommandLine.java b/src/main/java/picocli/CommandLine.java
index 72c6ad9..782e9bd 100644
--- a/src/main/java/picocli/CommandLine.java
+++ b/src/main/java/picocli/CommandLine.java
@@ -2166,7 +2166,7 @@ public class CommandLine {
                     }
                     int consumed = applyOption(field, Option.class, arity, paramAttachedToOption, args, initialized, argDescription);
                     // only return if cluster (and maybe more) was consumed, otherwise continue do-while loop
-                    if (consumed > 0) {
+                    if (consumed > 0 || args.isEmpty()) {
                         return;
                     }
                     cluster = args.pop();

But I'm unsure if that is the best option.

@remkop
Copy link
Owner

remkop commented Nov 1, 2017

Nice catch! Your bugfix looks fine to me.
I'll probably do a 2.0.2 release for this fix. Many thanks!

@remkop remkop added this to the 2.1.0 milestone Nov 1, 2017
@remkop remkop closed this as completed in b0d7084 Nov 1, 2017
remkop added a commit that referenced this issue Nov 1, 2017
@remkop remkop modified the milestones: 2.1.0, 2.0.2 Nov 5, 2017
@remkop
Copy link
Owner

remkop commented Nov 5, 2017

FYI, 2.0.2 is available from Maven Central and JCenter now. Thanks again!

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

2 participants