java 命令行解析:Commons CLI 替代方案?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3309250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Command Line Parsing: Commons CLI alternatives?
提问by Jan
For Command Line parsing in Java, I typically use Apache Commons CLI. Can anybody recommend any alternative libraries?
对于 Java 中的命令行解析,我通常使用Apache Commons CLI。有人可以推荐任何替代库吗?
采纳答案by matt b
JCommandersounds like a very simple and interesting approach to parsing command line arguments with annotations (from the creator of TestNG):
JCommander听起来像是一种非常简单有趣的方法来解析带有注释的命令行参数(来自 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:
然后你只需让 JCommander 解析:
JCommanderTest jct = new JCommanderTest();
String[] argv = { "-log", "2", "-groups", "unit", "a", "b", "c" };
new JCommander(jct, argv);
Assert.assertEquals(jct.verbose.intValue(), 2);
回答by Chris
Does this answer help? How to parse command line arguments in Java?
这个答案有帮助吗? 如何解析 Java 中的命令行参数?
回答by gliptak
I personally use kohsuke's args4J at https://github.com/kohsuke/args4j
我个人在https://github.com/kohsuke/args4j使用 kohsuke 的 args4J
回答by Aravind Yarram
I see a few listed here command line interpreters
我看到这里列出了一些命令行解释器
回答by Adrian A.
At java-source.netyou can find at least 4 other alternative libraries to Apache Commons CLI.
在java-source.net 上,您至少可以找到 4 个其他替代 Apache Commons CLI 的库。
回答by Gopi
getopt-1.0.11 is a simple easy to use lightweight library I used once for command line arguements parsing
getopt-1.0.11 是一个简单易用的轻量级库,我曾经用于命令行参数解析
回答by xeno
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.
虽然它允许您将命令行参数直接映射到域模型上,但您应该考虑为命令行参数使用单独的类。
回答by fishtoprecords
I use and like
我使用并喜欢
http://martiansoftware.com/jsap/
http://martiansoftware.com/jsap/
its open source, simple. Its not active, I think the author has other irons in the fire.
它的开源,简单。它不活跃,我认为作者在火上有其他熨斗。

