Tad*_*Tad 2 apache-commons-cli
我正在尝试编写一个程序,执行时java -jar -cf file.txt将检索cf参数的值.我到目前为止的代码是:
Options options = new Options();
final Option configFileOption = Option.builder("cf")
.longOpt("configfile")
.desc("Config file for Genome Store").argName("cf")
.build();
options.addOption(configFileOption);
CommandLineParser cmdLineParser = new DefaultParser();
CommandLine commandLineGlobal= cmdLineParser.parse(options, commandLineArguments);
if(commandLineGlobal.hasOption("cf")) {
System.out.println(commandLineGlobal.getOptionValue("cf"));
}
Run Code Online (Sandbox Code Playgroud)
我面临的问题是正在打印的值为null.谁能告诉我我错过了什么?
找出它不起作用的有用方法是打印出commons-cli的帮助信息
// automatically generate the help statement
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp( "sample", options );
Run Code Online (Sandbox Code Playgroud)
这打印
usage: sample
-cf,--configfile Config file for Genome Store
Run Code Online (Sandbox Code Playgroud)
这表示使用longOpt()指定选项的别名,而不是arg-value.做你想要的正确代码是:
final Option configFileOption = Option.builder("cf")
.argName("configfile")
.hasArg()
.desc("Config file for Genome Store")
.build();
Run Code Online (Sandbox Code Playgroud)
哪个正确打印
usage: sample
-cf <configfile> Config file for Genome Store
Run Code Online (Sandbox Code Playgroud)
并正确地将传递的参数报告给-cf.
| 归档时间: |
|
| 查看次数: |
3917 次 |
| 最近记录: |