Reusing JCommander object fails
See original GitHub issueI’m trying to reuse a JCommander instance, because I’m trying to create a simple Command Prompt-like Java front-end. I instantiate it and add a test command as such:
JCommander jc = new JCommander();
JCommanderTest jct = new JCommanderTest();
jc.addCommand(jct);
I then attempt to parse the command twice, as such:
String[] argv = {"example", "-log", "2", "-groups", "unit"};
parseAndExecute(argv);
String[] argv2 = {example", "-log", "3", "-groups", "unit2", "unit3"};
parseAndExecute(argv2);
Where parseAndExecute is defined as such:
private void parseAndExecute(String[] args)
{
jc.parse(args);
String parsedCommand = jc.getParsedCommand();
JCommander parsedJCommander = jc.getCommands().get(parsedCommand);
IJCommand commandObject = (IJCommand) parsedJCommander.getObjects().get(0);
commandObject.execute();
}
IJCommand is an interface that defines only the single method execute().
Now, the first time an array is parsed, it works as expected. However, the second time it gives the following exception:
Exception in thread “main” com.beust.jcommander.ParameterException: Can only specify option -log once
Does this mean it is not possible to reuse a JCommander object in this fashion? And if it is not possible, then what workaround is recommended?
Issue Analytics
- State:
- Created 7 years ago
- Comments:10 (1 by maintainers)
Top Results From Across the Web
JCommander
To address this problem, JCommander supports parameter delegates. When JCommander encounters an object annotated with @ParameterDelegate in one ...
Read more >Parsing Command-Line Parameters with JCommander
Now, let's use the JCommander class to parse the command-line arguments and assign the fields in our HelloWorldArgs object:
Read more >Java CLI Parser - command line interface - Stack Overflow
Edit: How to reuse commands. Solution 1, use inheritance: public class CommonArgs{ @Parameter(names="--common- ...
Read more >com.beust.jcommander.DynamicParameter.<init> java code ...
Best Java code snippets using com.beust.jcommander. ... Reuse -l for each property") private Map<String,String> commandLineProperties = new TreeMap<>();.
Read more >com.beust.jcommander.internal.Lists Java Examples
This page shows Java code examples of com.beust.jcommander.internal. ... new Callable() { @Override public Object call() throws Exception { command.run(); ...
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 FreeTop 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
Top GitHub Comments
You need to fork before you can submit a pull request.
Cédric
On Tue, Jul 5, 2016 at 6:42 AM, Jae Gangemi notifications@github.com wrote:
yes of course you do. sorry - i took forking to mean something different in this context.