如何解析 Java 中的命令行参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/367706/
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
How do I parse command line arguments in Java?
提问by lindelof
What is a good way of parsing command line arguments in Java?
在 Java 中解析命令行参数的好方法是什么?
采纳答案by Vinko Vrsalovic
Check these out:
检查这些:
Or roll your own:
或者自己动手:
For instance,this is how you use commons-cli
to parse 2 string arguments:
例如,这是您commons-cli
用来解析 2 个字符串参数的方式:
import org.apache.commons.cli.*;
public class Main {
public static void main(String[] args) throws Exception {
Options options = new Options();
Option input = new Option("i", "input", true, "input file path");
input.setRequired(true);
options.addOption(input);
Option output = new Option("o", "output", true, "output file");
output.setRequired(true);
options.addOption(output);
CommandLineParser parser = new DefaultParser();
HelpFormatter formatter = new HelpFormatter();
CommandLine cmd;
try {
cmd = parser.parse(options, args);
} catch (ParseException e) {
System.out.println(e.getMessage());
formatter.printHelp("utility-name", options);
System.exit(1);
}
String inputFilePath = cmd.getOptionValue("input");
String outputFilePath = cmd.getOptionValue("output");
System.out.println(inputFilePath);
System.out.println(outputFilePath);
}
}
usage from command line:
从命令行使用:
$> java -jar target/my-utility.jar -i asd
Missing required option: o
usage: utility-name
-i,--input <arg> input file path
-o,--output <arg> output file
回答by Marc Novakowski
Take a look at the Commons CLIproject, lots of good stuff in there.
看看Commons CLI项目,里面有很多好东西。
回答by mepcotterell
Maybe these
也许这些
JArgs command line option parsing suite for Java- this tiny project provides a convenient, compact, pre-packaged and comprehensively documented suite of command line option parsers for the use of Java programmers. Initially, parsing compatible with GNU-style 'getopt' is provided.
ritopt, The Ultimate Options Parser for Java- Although, several command line option standards have been preposed, ritopt follows the conventions prescribed in the opt package.
Java 的 JArgs 命令行选项解析套件- 这个小项目为 Java 程序员提供了一个方便、紧凑、预先打包和全面记录的命令行选项解析器套件。最初,提供了与 GNU 风格的“getopt”兼容的解析。
ritopt,Java 的终极选项解析器- 尽管已经提出了几个命令行选项标准,ritopt 遵循 opt 包中规定的约定。
回答by GaryF
I've used JOpt and found it quite handy: http://jopt-simple.sourceforge.net/
我使用过 JOpt 并发现它非常方便:http://jopt-simple.sourceforge.net/
The front page also provides a list of about 8 alternative libraries, check them out and pick the one that most suits your needs.
首页还提供了大约 8 个替代库的列表,请查看它们并选择最适合您需求的库。
回答by OscarRyz
Yeap.
是的。
I think you're looking for something like this: http://commons.apache.org/cli
我想你正在寻找这样的东西:http: //commons.apache.org/cli
The Apache Commons CLI library provides an API for processing command line interfaces.
Apache Commons CLI 库提供了一个用于处理命令行界面的 API。
回答by Ray Tayek
If you are familiar with gnu getopt, there is a Java port at: http://www.urbanophile.com/arenn/hacking/download.htm.
如果您熟悉 gnu getopt,有一个 Java 端口:http: //www.urbanophile.com/arenn/hacking/download.htm。
There appears to be a some classes that do this:
似乎有一些类可以做到这一点:
回答by Alex Miller
You might find this meta-article of unhappiness interesting as a jumping off point:
你可能会发现这篇关于不快乐的元文章很有趣,作为一个起点:
http://furiouspurpose.blogspot.com/2008/07/command-line-parsing-libraries-for-java.html
http://furious purpose.blogspot.com/2008/07/command-line-parsing-libraries-for-java.html
回答by André
回答by Cedric Beust
Take a look at the more recent JCommander.
看看最近的JCommander。
I created it. I'm happy to receive questions or feature requests.
我创造了它。我很高兴收到问题或功能请求。
回答by lexicalscope
I have been trying to maintain a list of Java CLI parsers.
我一直在努力维护一个Java CLI 解析器列表。
- Airline
- Active Fork: https://github.com/rvesse/airline
- argparse4j
- argparser
- args4j
- clajr
- cli-parser
- CmdLn
- Commandline
- DocOpt.java
- dolphin getopt
- DPML CLI (Jakarta Commons CLI2 fork)
- Dr. Matthias Laux
- Jakarta Commons CLI
- jargo
- jargp
- jargs
- java-getopt
- jbock
- JCLAP
- jcmdline
- jcommander
- jcommando
- jewelcli (written by me)
- JOpt simple
- jsap
- naturalcli
- Object Mentor CLI article (more about refactoring and TDD)
- parse-cmd
- ritopt
- Rop
- TE-Code Command
- picoclihas ANSI colorized usage help and autocomplete