【问题标题】:Required Option with and without arguments in Apache Common CLIApache Common CLI 中带和不带参数的必需选项
【发布时间】:2019-11-28 00:01:21
【问题描述】:
我需要有和没有参数的相同选项。例如,
CLIParser -d 2 abc.txt
以上情况d=2
CLIParser -d abc.txt
以上情况d=1
我尝试使用.optionalArg(true) 和.numberOfArgs(1),但似乎没有任何效果。这可行吗?
【问题讨论】:
标签:
java
command-line-interface
options
apache-commons-cli
【解决方案1】:
这对我来说很好用。
Option.builder("d").hasArg().optionalArg(true).build();
两者都需要:
hasArg = true and optionalArg = true
如果没有参数则:
CommandLine.getOptionValue("d");
将返回null。您可以根据需要将其更改为 1。