Java Apache Common CLI:如何添加参数?

Java Apache Common CLI:如何添加参数?,java,apache-commons,Java,Apache Commons,我正在将Common CLI用于个人项目。从文档中我没有发现一件事,那就是如何强制执行某个论点 为了澄清我的问题,我是否可以定义参数和选项之间的区别,命令: mycommand file.txt -b 2 mycommand is the command, file.txt is the argument -b 2 is the option where 2 is the option value 使用Common CLI,我可以添加-b 2作为如下选项: options.

我正在将Common CLI用于个人项目。从文档中我没有发现一件事,那就是如何强制执行某个论点

为了澄清我的问题,我是否可以定义参数和选项之间的区别,命令:

    mycommand file.txt -b 2

mycommand is the command, 
file.txt is the argument
-b 2 is the option where 2 is the option value
使用Common CLI,我可以添加-b 2作为如下选项:

    options.addOption( "b", true, "Some message" );
并使用以下命令分析参数:

CommandLineParser commandParser = new GnuParser();
CommandLine result = commandParser.parse(options, args)
但是如何指定file.txt也是必需的


非常感谢编辑:我不知道你的意思是让目标(不是选项)成为必需的

如果使用完整解析方法
CommandLineParser.parse(Options,String[],boolean)
并将可选标志设置为false,则解析器将跳过未知参数

稍后可以通过返回字符串[]的方法
getArgs()
检索它们

然后,您可以检查这些字符串,以确保有一个名为file.txt的字符串

Options options = new Options();

options.addOption("b", true, "some message");

String[] myArgs = new String[]{"-b","2", "file.txt"};
CommandLineParser commandParser = new GnuParser();

CommandLine commandline = commandParser.parse(options, myArgs, false);

System.out.println(Arrays.toString(commandline.getArgs()));
将在屏幕上打印
[file.txt]

因此,您添加了一个额外的检查来搜索该数组以查找任何所需的目标:

boolean found=false;
for(String unparsedTargets : commandline.getArgs()){
    if("file.txt".equals(unparsedTargets)){
        found =true;
    }
}
if(!found){
    throw new IllegalArgumentException("must provide a file.txt");
}

我同意这很混乱,但我不认为CLI提供了一种干净的方法来做到这一点。

不,在当前的API中是不可能的,但我认为您可以使用自己的
Parser.parse()
实现来扩展GnuParser,如果强制参数名曾经是file.txt。
否则,如果文件名可以更改,您可以覆盖
Parser.processArgs()
(对于非选项args,我的意思是您的文件名)和
Parser.processOption()
(设置标志表示您找到了一个有效的选项):如果您在
Parser.processArgs()
中输入,当设置标志时,您会发现一个无效的未命名的参数)

p.filename
(或
cmdLine.getArgs[0]
)中,可以获取文件名

这并不直观,但对于CLI API,我不知道还有其他方法

public class MyGnuParser extends GnuParser {

    private int optionIndex;
    private String filename;

    public MyGnuParser() {
        this.optionIndex = 0;
        this.filename = null;
    }

public CommandLine parse(Options options, String[] arguments, Properties properties) throws ParseException {
       CommandLine cmdLine = super.parse(options, arguments, properties, false);
       if(this.filename == null) throw new ParseException(Missing mandatory filename argument);
    }

    @Override
    public void processArgs(Option opt, ListIterator iter) throws ParseException {
      super.processArgs(opt, item);
      ++this.optionIndex;
    }

    @Override
    protected void processOption(final String arg, final ListIterator iter) throws    ParseException {
      if(this.optionIndex > 0) {
        throw new ParseException(non-opt arg must be the first);
      }
      if(this.filename != null) {
        throw new ParseException(non-opt invalid argument);
      }
      this.filename = arg;
      ++this.optionIndex;
    }
}

MyGnuParser p = new MyGnuParser();
CommandLine cmdLine = p.parse(options, args, properties);