For Command Line parsing in Java, I typically use Apache Commons CLI. Can anybody recommend any alternative libraries?
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With