是否存在将Java命令行选项解析为关联数组的库? [英] Is there a library to parse java command line options into an associative array?
本文介绍了是否存在将Java命令行选项解析为关联数组的库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一个可以采用java -jar --aaa=a --bbb=b ---ccc=c
形式的命令行选项并返回其值可以通过argsArray['aaa'], argsArray['bbb']
等访问的数组的库.
I need a library which can take command line options of the form java -jar --aaa=a --bbb=b ---ccc=c
and return an array whose values can be accessed as argsArray['aaa'], argsArray['bbb']
etc.
是否有一些带有示例的图书馆来做到这一点?
Is there some library with examples to do this?
推荐答案
选项可以带有或不带有参数,可以是可选的或必需的,并且您可以为每个设置设置说明以获取使用帮助.一个简单的用法示例:
Options can have arguments or not, can be optional or required, and you can set up descriptions for each for usage help. A brief example usage:
public static void main(String[] args) {
Options options = new Options();
try {
options.addOption(OptionBuilder.withArgName("help").hasArgs(0).withDescription("Prints this help message.").isRequired(false).create("h"));
options.addOption(OptionBuilder.withArgName("debug logging").hasArgs(0).withDescription("Enable debug logging").isRequired(false).create("1"));
CommandLineParser parser = new PosixParser();
CommandLine cmd = parser.parse(options, args);
if (cmd.hasOption("h")) {
new HelpFormatter().printHelp(400, "load_page_spool.sh", "OPTIONS", options, "Loads crawl data from pages pool, updating FRONTIER, HISTORY and PageTable", true);
return;
}
....
} catch (MissingOptionException e) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("LoadPageSpool", options);
}
}
这篇关于是否存在将Java命令行选项解析为关联数组的库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文