29

For Command Line parsing in Java, I typically use Apache Commons CLI. Can anybody recommend any alternative libraries?

Trevor Hickey
  • 34,154
  • 27
  • 144
  • 256
Jan
  • 8,959
  • 13
  • 46
  • 52
  • 8
    If 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
  • 1
    To 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
  • 6
    possible 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 Answers8

22

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);
Samuel Liew
  • 72,637
  • 105
  • 156
  • 238
matt b
  • 135,500
  • 64
  • 278
  • 339
6

Does this answer help? How to parse command line arguments in Java?

Community
  • 1
  • 1
Chris
  • 1,323
  • 3
  • 17
  • 31
  • 1
    I 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
5

I personally use kohsuke's args4J at https://github.com/kohsuke/args4j

gliptak
  • 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
2

I see a few listed here command line interpreters

Aravind Yarram
  • 76,625
  • 45
  • 224
  • 313
2

At java-source.net you can find at least 4 other alternative libraries to Apache Commons CLI.

Adrian A.
  • 1,044
  • 8
  • 6
0

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.

xeno
  • 372
  • 2
  • 5
0

getopt-1.0.11 is a simple easy to use lightweight library I used once for command line arguements parsing

Gopi
  • 9,915
  • 4
  • 29
  • 45
0

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.

fishtoprecords
  • 2,336
  • 7
  • 26
  • 38