命令行解析:Commons CLI备用? [英] Command Line Parsing: Commons CLI alternatives?

查看:217
本文介绍了命令行解析:Commons CLI备用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Java中的命令行解析,我通常使用 Apache Commons CLI
任何人都可以推荐任何替代库?

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

推荐答案

JCommander 听起来像一个非常简单而有趣的解析命令行参数与注释(来自TestNG的创建者)的方法:

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;
}




然后你只需要让JCommander解析:

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);

这篇关于命令行解析:Commons CLI备用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆