For Command Line parsing in Java, I typically use Apache Commons CLI. Can anybody recommend any alternative libraries?
-
8If you're going to ask for an alternative to what you normally use, it would be useful if you'd say what you're looking for that your normal option doesn't provide. – Jon Skeet Jul 22 '10 at 13:22
-
1To be honest, I didn't have a particular missing feature in mind. True, Apache Commons CLI is 'tried and tested', but it is also pretty verbose. I was just wondering if anybody introduced any new ideas in this area. In this category, I think I'm going to have a closer look at the JCommander suggestion below. – Jan Jul 22 '10 at 13:58
-
6possible duplicate of [Is there a good command line argument parser for Java?](http://stackoverflow.com/questions/367706/is-there-a-good-command-line-argument-parser-for-java) – Guido Aug 13 '10 at 11:38
-
At least for me, the Commons CLI has a number of architectural issues. Its OptionBuilder isn't really a Builder, tree-like commands aren't really supported, and there isn't support for user-defined types. – David Ehrmann Jan 27 '14 at 18:01
8 Answers
JCommander sounds like a very simple and interesting approach to parsing command line arguments with annotations (from the creator of TestNG):
You annotate fields with descriptions of your options:
import com.beust.jcommander.Parameter;
public class JCommanderTest {
@Parameter
public List<String> parameters = Lists.newArrayList();
@Parameter(names = { "-log", "-verbose" }, description = "Level of verbosity")
public Integer verbose = 1;
@Parameter(names = "-groups", description = "Comma-separated list of group names to be run")
public String groups;
@Parameter(names = "-debug", description = "Debug mode")
public boolean debug = false;
}
and then you simply ask JCommander to parse:
JCommanderTest jct = new JCommanderTest();
String[] argv = { "-log", "2", "-groups", "unit", "a", "b", "c" };
new JCommander(jct, argv);
Assert.assertEquals(jct.verbose.intValue(), 2);
- 72,637
- 105
- 156
- 238
- 135,500
- 64
- 278
- 339
-
This is how Cederic introduced it: http://beust.com/weblog/2010/07/13/announcing-jcommander-1-0/ – Jan Jul 22 '10 at 14:17
-
Couldn't find an API to print the help. See related question: http://stackoverflow.com/questions/30046171/how-to-print-help-using-jcommander – AlikElzin-kilaka May 05 '15 at 07:04
-
Does this answer help? How to parse command line arguments in Java?
-
1I appreciate these links to alternative frameworks and I have to admit I didn't notice this very similar question. However, I was hoping to reach somebody who *actually tried* different alternatives and who is able to *recommend* one of those. – Jan Jul 22 '10 at 14:08
-
In the respect, this is the kind of article I was looking for: http://furiouspurpose.blogspot.com/2008/07/command-line-parsing-libraries-for-java.html Thank you! – Jan Jul 22 '10 at 14:21
I personally use kohsuke's args4J at https://github.com/kohsuke/args4j
- 3,396
- 1
- 27
- 57
-
It is also my personal preference. args4j is more flexible than JCommander. The principles are the same, but it is more intuitive to use and differentiate between arguments and options. – el.nicko Jul 25 '16 at 20:23
At java-source.net you can find at least 4 other alternative libraries to Apache Commons CLI.
- 1,044
- 8
- 6
You could try commandline (pretty anonymous name, I know )
It uses annotations to map command line arguments into an object model. A couple of parsing modes and a set of annotations that can be combined in various ways allows you to create pretty advanced command line option rules.
While it allows you to map the command line arguments directly onto your domain model, you should consider using separate classes for the command line arguments.
- 372
- 2
- 5
getopt-1.0.11 is a simple easy to use lightweight library I used once for command line arguements parsing
- 9,915
- 4
- 29
- 45
I use and like
http://martiansoftware.com/jsap/
its open source, simple. Its not active, I think the author has other irons in the fire.
- 2,336
- 7
- 26
- 38