简体   繁体   English

如何定义帮助选项和其他必需选项?

[英]How to define help option with other required option?

I need to have help option (-h) and two other required options (-u, -p) coexist together.. 我需要帮助选项(-h)和其他两个必需的选项(-u,-p)并存。

private static Options buildOptions() {
    Options options = new Options();

    Option help = new Option("h", "help", false, "print this message");     

    Option user = OptionBuilder.hasArg()
                .isRequired(true)
                .withArgName("username")                   
                .withDescription("use given user name for login")
                .withLongOpt("user")
                .create("u");

    Option pass = OptionBuilder.hasArg()
            .isRequired(true)
            .withArgName("password")                   
            .withDescription("use given password for login")
            .withLongOpt("pass")
            .create("p");

    options.addOption(help);
    options.addOption(user);
    options.addOption(pass);
    return options;
}

But when I try to parse args without providing required parameters, I receive MissingOptionException. 但是,当我尝试在不提供必需参数的情况下解析args时,会收到MissingOptionException。 I am looking for a way to be able to pass in only a -h and be able to print out only the help. 我正在寻找一种只能传递-h并仅打印出帮助的方法。

I pre-parse the command line looking for help: 我预先准备了命令行以寻求帮助:

if ((args.length > 0) && (args[0].matches("^\\s*[-/]{0,2}[Hh](?i:elp)\\s*$"))) {
    usage();
}

I also look for -help anywhere in the command line so that I can use just tack it onto the end of a partial command line if I forget whether something is -debug or -dbg . 我还会在命令行中的任何地方寻找-help ,这样,如果我忘记了-debug-dbg就可以在部分命令行的末尾使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM