2014-11-05 36 views
0

簡單地說,我想知道是否可以有參數接受選項或不接受選項。這對列表很有用。例如:-a將列出可能的選項,而-a stuff將使用這些東西來做事情。Apache Commons CLI允許參數選項或無參數選項

這裏是我當前的代碼:

options.addOption("f", "look-and-feel", false, "This sets the look and feel."); 
//Some stuff happens here 
if (cmd.hasOption('f')) { 
    String laf = cmd.getOptionValue('f'); 
    System.out.println(laf); 
    if (laf == null) { 
     for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) { 
      System.out.println(info.getClassName()); 
     } 
     return; 
    } else { 
     setLookAndFeel(laf); 
    } 
} else { 
    setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); 
} 

鍵入-f列表的外觀和感覺正確,同時指定其-f com.laf.LookAndFeel還列出了它。有任何想法嗎?

+0

我添加的代碼我使用預期不起作用。 – 2014-11-05 17:56:38

回答

0

當您創建選項,使用這個代替:

Option lookAndFeelOption = new Option("f", "look-and-feel", true, "This sets the look and feel."); 
lookAndFeelOption.setOptionalArg(true); 
options.addOption(lookAndFeelOption);