我对这两种方法有点困惑。当我使用时,getOptionValue(optionName)
我确实得到了我在命令行中输入的参数,但是当我使用时我得到了 nulloptionName.getValue()
获取值()
返回此 Option 的指定值,如果没有值,则返回 null。
获取选项值(字符串)
检索此选项的第一个参数(如果有)。
正如您所看到getOptionValue(String)
的,明确表示参数,而另一个只表示值,但它们的方法名称中都有“值”。由于没有setValue(String)
方法或类似的东西,我只是假设它与参数相同。
public static void main(final String[] args ) {
CommandLine cmd = null;
Option program = Option.builder("p")
.hasArg()
.required(true)
.longOpt("program")
.build();
Options options = new Options();
options.addOption( program );
CommandLineParser parser = new DefaultParser();
try {
cmd = parser.parse( options, args );
}
catch( ParseException exp ) {
System.err.println( "Parsing failed. Reason: " + exp.getMessage() );
}
String[] list;
list = cmd.getArgs();
int argsN = program.getArgs();
String optionValue = cmd.getOptionValue("p");
String value = program.getValue();
System.out.println(argsN);
System.out.println(optionValue);
System.out.println(value);
}
输出是:
1
[参数]
无效的