简单地说,我想知道是否有可能让参数接受选项或不接受任何选项。这对列表很有用。示例:-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
仍会列出它。有什么想法吗?
答案 0 :(得分:0)
创建选项时,请改用:
Option lookAndFeelOption = new Option("f", "look-and-feel", true, "This sets the look and feel.");
lookAndFeelOption.setOptionalArg(true);
options.addOption(lookAndFeelOption);