question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Variable Arity parameters don't work with setAcceptUnknownOption(true)

See original GitHub issue

Using 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:open
  • Created 6 years ago
  • Reactions:5
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
paplorinccommented, Oct 11, 2019

Are there plans to fix this?

0reactions
buzzlawlesscommented, Jun 17, 2020

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found