2013-07-03 42 views
3

我尝试使用Common CLI解析简单参数,但收到ParseException。Apache Common CLI无法识别选项

这里是我的代码:

​​

结果是:

命令行参数的

解析失败。原因:无法识别的选项:-source /home/file/myfile.txt

如果我使用args来没有一个破折号,比没有抛出异常,但cmd.hasOption("source")返回false。

P.S>使用示例建议使用DefaultParser,但它只会出现在CLI 1.3中(根据1.3 JavaDoc)。

回答

6

变化

args = new String[]{ 
      "-source /home/file/myfile.txt", 
      "-url http://localhost/state", 
      "-result result.txt"}; 

args = new String[]{ 
      "-source", "/home/file/myfile.txt", 
      "-url", "http://localhost/state", 
      "-result", "result.txt"}; 

第二是JVM将如何包装/传递命令行参数传递给你的主要方法,因此是什么公地CLI期待。

+0

噢,谢谢,这对我的工作如我所料。没有关于如何将参数传递给commons-cli的信息,并且有一个像'String [] args = new String [] {“--block-size = 10”};'这使我困惑:) – Dragon