Variable Arity parameters don't work with setAcceptUnknownOption(true)
See original GitHub issueUsing variable arity parameters together with unknown options fails with
ParameterException: Expected a value after parameter -values
JCommander#isOption simply returns true when acceptUnknownOption is true, so DefaultVariableArity#processVariableArity always returns 0, since it thinks the arguments to the variable arity option are options.
This used to work in 1.60, where isOption checked if the option has a prefix.
Example code:
import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import java.util.List;
public class VarArity {
@Parameter (names = "-values", variableArity = true)
private List<String> values;
@Parameter (names = "-foo")
private boolean foo;
@Parameter
private List<String> files;
public static void main(String[] args) {
VarArity app = new VarArity();
JCommander jCommander = new JCommander(app);
jCommander.setAcceptUnknownOptions(true);
jCommander.parse("-values", "one", "two", "-foo", "-bar", "foobar.txt");
}
}
Issue Analytics
- State:
- Created 6 years ago
- Reactions:5
- Comments:7 (1 by maintainers)
Top Results From Across the Web
3.6 Variable Arity Methods | Declarations - InformIT
A fixed arity method must be called with the same number of actual parameters (also called arguments) as the number of formal parameters...
Read more >What are the 3 dots in parameters?/What is a variable arity ...
Its called Variable arguments or in short var-args, introduced in Java 1.5. The advantage is you can pass any number of arguments while ......
Read more >DCL57-J. Avoid ambiguous overloading of variable arity
The variable arity (varargs) feature was introduced in JDK v1.5.0 to support methods that accept a variable numbers of arguments. According to the...
Read more >An Introduction to Scheme and its Implementation
Variable Arity : Procedures that Take a Variable Number of Arguments ... This tells Scheme that the procedure's actual arguments should be packaged...
Read more >Overloading Variable Arity Method in Java - GeeksforGeeks
1.5 Using arrays to pass the variable number of arguments is an old approach to varargs, which is not suitable. So let us...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Are there plans to fix this?
What does it mean to accept unknown options? I don’t see anything in the documentation about it.
Edit: Leaving an explanation here in case others see this and didn’t know: if accept unknown options is true, then passing an unknown parameter (e.g. “-bar” in the example that started this thread) will simply be ignored. If it is false, then an exception is thrown here