2013-07-03 60 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