Jan Jan - 1 year ago 296
Java Question

Command Line Parsing: Commons CLI alternatives?

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

Thank you!

Answer Source

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 {
  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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download