1
我不明白如何在Commons CLI中將Options
應用到DefaultParser
。命令行中的Commons CLI空選項
當創建對象CommandLine
時,分配的Options
始終爲空。
婁塊是如何解釋的共享文檔CLI:
public static void main(String[] args) {
Options options = new Options();
options.addOption("c", false, "why are you hidding from me");
CommandLineParser parser = new DefaultParser();
System.out.println(args[0]); // this prints -c
try {
CommandLine line = parser.parse(options, args);
System.out.println(line.getArgs()[0]); // prints -c
Option[] o = line.getOptions(); // this is empty for some reason
System.out.println(o.length); // prints 0
if (line.hasOption("c")) { // false
System.out.println(" flag c found");
}
}
catch(ParseException e) {
e.printStackTrace();
}
怎麼說,line.getOptions()
是空的,以及如何正確應用這些選項?
你傳遞給你的程序的命令行是什麼? – JJF
-c。我基本上打開了eclipse的運行配置,並將-c傳遞給參數列表。不過我不認爲問題在那裏,因爲line.getArgs()不是空的。 – John
初看,我看不出你做了什麼錯。您使用的是哪種版本的commons-cli? – dosyfier