Agrs4j を
調べた
条件付き<wbr>チェック
と
B は A に 依存していて B を 指定する 場合は、 A が 必須
Option
アノテーションに、
以下では、-filePath
オプションに、-format
オプションを
@Option(name = "-format", usage = "Format of output ", metaVar = "[console or json]") private Format format = Format.CONSOLE; @Option(name = "-filePath", usage = "File name of json ", metaVar = "<filename>", depends = {"-format"}) private String filePath = null;
上記指定で、-filepath
のみ
java -jar build/libs/xyz.monotalk.google.webmaster.cli-0.0.1.jar -filePath aaaa.json
下記の
option "-filePath" requires the option(s) [-format]
A と B の 両方は 指定できない
検索した
java - args4j: How to make an argument required if another argument is/isn’t given? - Stack Overflow
forbids
には
以下のように、-siteUrl
の、forbids
には-format
を-format
のforbids
には-siteUrl
を
@Option(name = "-siteUrl", usage = "Url of site", metaVar = "<siteUrl>", handler = URLOptionHandler.class, forbids = {"-format"}) private URL siteUrl = null; @Option(name = "-format", usage = "Format of output ", metaVar = "[console or json]", forbids = {"-siteUrl"}) private Format format = null;
上記指定で、
java -jar build/libs/xyz.monotalk.google.webmaster.cli-0.0.1.jar webmasters.sitemaps.list -format CONSOLE -siteUrl https://kemsakurai.github.io/
以下の
option "-siteUrl" cannot be used with the option(s) [-format]
StackOverFlow のA と<wbr> B の<wbr>両方は<wbr>指定できず、<wbr>いずれか<wbr>必須
を
if (siteUrl == null && format == null) { throw new CmdLineException(); }
A の 値 が 「あ」だったら、 B が 必須
これは
Enum Format が
@Option(name = "-format", usage = "Format of output ", metaVar = "[console or json]") private Format format = Format.CONSOLE; @Option(name = "-filePath", usage = "File name of json ", metaVar = "<filename>", depends = {"-format"}) private String filePath = null;
以下、
Enum 値 が
switch (format) { // for format console case CONSOLE: System.out.println(response.toPrettyString()); break; // for format json case JSON: if (StringUtils.isEmpty(filePath)) { throw new CmdLineArgmentException("For JSON format, filepath is mandatory."); } ..... break; }
条件付き
以上です。
コメント