Apache Commons CLI允许参数选项或无参数选项

时间:2014-11-05 17:43:45

标签: java apache-commons-cli

简单地说,我想知道是否有可能让参数接受选项或不接受任何选项。这对列表很有用。示例:-a将列出可能的选项,而-a stuff将使用这些内容来执行操作。

这是我目前的代码:

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仍会列出它。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

创建选项时,请改用:

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