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.

Reusing JCommander object fails

See original GitHub issue

I’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:closed
  • Created 7 years ago
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
cbeustcommented, Jul 5, 2016

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:

ugh - why fork the project instead of just submitting a pull request?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/cbeust/jcommander/issues/271#issuecomment-230481454, or mute the thread https://github.com/notifications/unsubscribe/AAFoosSxYB4TtSTYtnezb5DfQIeMvg_Bks5qSl81gaJpZM4Iudpj .

0reactions
jgangemicommented, Jul 7, 2016

yes of course you do. sorry - i took forking to mean something different in this context.

Read more comments on GitHub >

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

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