CommonsCLI,相互包容的论点?

时间:2017-06-14 17:11:54

标签: java apache-commons apache-commons-cli

有没有办法使用CommonsCLI来创建一对组,当且仅当使用来自另一组的项目时才可以使用它们? (即两者或两者都没有)

例如,给出选项选项......

-foo       Enable foo. Must enable either bar or baz, cannot (for obvious reasons) be combined with moo.
-moo       Enable moo. Must enable either bar or baz, cannot (for obvious reasons) be combined with foo.
-bar       Set foo or moo mode to foobar or moobar. Cannot be used without foo or moo modes. Cannot be combined with baz.
-baz       Set foo or moo mode to foobaz or moobaz. Cannot be used without foo or moo modes. Cannot be combined with bar.

这给出了......的有效模式。

-foo -bar
-foo -baz
-moo -bar
-moo -baz
<None>

和......的无效模式。

-foo -moo [-bar / -baz / None]
-bar -baz [-foo / -moo / None]
-bar
-baz
-foo
-moo

1 个答案:

答案 0 :(得分:0)

我不认为commons-cli可以做到这一点,你需要在通过cli解析之后自己做一些检查,例如。

CommandLine cmd = parser.parse( options, args);
if(cmd.hasOption("foo") && cmd.hasOption("moo")) {
    throw new IOException("invalidoptions");
}
...