Java Commons CLI - 在使用help参数时覆盖required()参数

时间:2018-01-11 11:07:03

标签: java command-line-interface apache-commons apache-commons-cli

我以下列方式为Parser添加选项:

options = new Options()
                .addOption(Option.builder(CONFIG_PARAM)
                        .required()
                        .hasArg(true)
                        .argName(CONFIG_PARAM_NAME + "_path")
                        .desc(CONFIG_PARAM_DESC)
                        .longOpt(CONFIG_PARAM_NAME)
                        .build())
                (...)
                .addOption(Option.builder(HELP_PARAM)
                        .hasArg(false)
                        .longOpt(HELP_PARAM_NAME)
                        .desc(HELP_PARAM_DESC)
                        .build());

现在,我想允许用户仅使用help命令,例如

mypreciousapp --help

通过上述解决方案,我无法获得有关缺少必需参数的信息

Missing required options: c

有没有办法标记help参数,以便它可以覆盖所需的参数,并允许单独使用它?我可以手动执行此操作,但首先我想知道CLI lib中是否有这样的选项。

2 个答案:

答案 0 :(得分:1)

似乎commons-cli目前不支持,所以我会创建第二个Options对象,其中不需要param,并在完成整个解析之前先解析/检查,如下所示:

public static void main(String[] args) {
    // define the options with required arguments as needed
    Options opts = new Options()
            .addOption(Option.builder("p")
                    .required()
                    .hasArg(true)
                    .argName("arg")
                    .desc("description  ")
                    .longOpt("param")
                    .build())
            .addOption(Option.builder("h")
                    .hasArg(false)
                    .longOpt("help")
                    .desc("help description")
                    .build());

    // first check if usage-output was requested
    if (handleHelp(args, opts)) {
        return;
    }

    // now handle the full options
    CommandLineParser parser = new DefaultParser();
    final CommandLine cmdLine;
    try {
        cmdLine = parser.parse(opts, args);
    } catch (ParseException ex) {
        System.out.println("Syntax error: " + ex.getMessage());

        printHelp(opts);

        return;
    }

    // now handle options and do your work
}

private boolean handleHelp(String[] args, Options opts) {
    Options helpOpts = new Options()
            .addOption(Option.builder("p")
                    //.required()
                    .hasArg(true)
                    .argName("arg")
                    .desc("description  ")
                    .longOpt("param")
                    .build())
            .addOption(Option.builder("h")
                    .hasArg(false)
                    .longOpt("help")
                    .desc("help description")
                    .build());

    CommandLineParser helpParser = new DefaultParser();
    final CommandLine cmdLine;
    try {
        cmdLine = helpParser.parse(helpOpts, args);
    } catch (ParseException ex) {
        System.out.println("Syntax error: " + ex.getMessage());

        printHelp(opts);

        return true;
    }

    if(cmdLine.hasOption("h")) {
        printHelp(opts);

        return true;
    }

    return false;
}

private void printHelp(Options opts) {
    try (PrintWriter pw = new PrintWriter(System.out)) {
        HelpFormatter formatter = new HelpFormatter();

        formatter.printHelp(pw, 80, "myapp", "test-header", opts,
                formatter.getLeftPadding(), formatter.getDescPadding(), "test-footer", true);
    }
}

答案 1 :(得分:0)

commons-cli库有一个流畅的包装器:https://github.com/bogdanovmn/java-cmdline-app

-h选项已内置。您无需使用自己的代码进行管理。