Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command Line Parsing: Commons CLI alternatives? [closed]

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

like image 687
Jan Avatar asked Jul 22 '10 13:07

Jan


1 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); 
like image 119
matt b Avatar answered Oct 13 '22 23:10

matt b