我需要一个可以采用以下形式的命令行选项的库java -jar --aaa=a --bbb=b ---ccc=c
并返回一个数组,其值可以通过以下方式访问argsArray['aaa'], argsArray['bbb']
etc.
有一些带有示例的库可以做到这一点吗?
Java 中命令行选项的一个很棒的解析器是Apache Commons CLI http://commons.apache.org/cli/.
选项可以有参数也可以没有,可以是可选的也可以是必需的,并且您可以为每个选项设置说明以获取使用帮助。一个简短的用法示例:
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);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)