小编Moh*_*rab的帖子

Apache CLI:必需选项与help选项相矛盾.

如果我有2个选项定义为所需,例如:

 public static void main(String [] args){
      Options options= new Options();
      Option  inputFileOp=Option.builder("i").longOpt("input").hasArg().desc("Input file").argName("file").required().build();
        options.addOption(inputFileOp);

      Option outputFileOp=Option.builder("o").longOpt("output").hasArg().desc("Output file").argName("file").required().build();
        options.addOption(outputFileOp);
Run Code Online (Sandbox Code Playgroud)

和帮助选项

    Option helpOp =new Option("h",false,"Show Help");
    helpOp.setLongOpt("help");
    helpOptions.addOption(helpOp);
Run Code Online (Sandbox Code Playgroud)

和解析器

DefaultParser parser = new DefaultParser();
CommandLine cmd=parser.parse(options,args);

if(cmd.hasOption(helpOp.getOpt())){
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp( "MyApp.sh", options );
        System.exit(0);
    }

}
Run Code Online (Sandbox Code Playgroud)

当用户输入例如myApp -h ..在解析步骤中引发了一个异常,即缺少必需的选项,而我想打印帮助数据.

如何在保持按要求声明这些选项的同时调用帮助?

java apache-commons apache-commons-cli

12
推荐指数
3
解决办法
6627
查看次数

标签 统计

apache-commons ×1

apache-commons-cli ×1

java ×1