java – 如何使用apache commons cli指定多个选项?

我想要的东西:
java programName  -jobs1 -C 10 -W 20
java programName  -job2
java programName  -job3

内容

Option o1 = new Option("job2","some desc");
Option o2 = new Option("job3","(some desc")

Option o3 = OptionBuilder.hasArgs(2).withArgName( "W" ).withArgName("C").withDescription(  "Some desc" ).create("job1")
Option o4 = new Option("help");

Options os = new Options().addOption(o1).addOption(o2).addOption(o3).addOption(o4).

HelpFormatter formatter = new HelpFormatter();
formatter.printHelp( "ProgramName",options );

输出的位置是:

Usage ProgramName
 -job1 <c>  Some Desc
 -job2      Some desc
 -job3      Some desc
 -help      Print this message

我希望-job1它应该打印-job1 -C<> -W<>

我错过了什么吗?它不适用于多个参数.顺便说一下,我使用了commons-cli 1.2.

解决方法

您不能拥有上下文相关的参数.您可以拥有以下参数:job1,job2,job3,C& W,但你不能说(通过图书馆)C& W仅对job1有效.

如果job1 / 2/3是互斥的,则创建一个OptionGroup.然后在代码中,确保C& W仅用于job1.

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.OptionGroup;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.HelpFormatter;

public class StackOverflowExample
{
    public static final String JOB1 = "job1";
    public static final Option job1 =
        OptionBuilder.hasArg(false)
            .isrequired(false)
            .withDescription("description of job1")
            .create(JOB1);

    public static final String JOB2 = "job2";
    public static final Option job2 =
        OptionBuilder.hasArg(false)
            .isrequired(false)
            .withDescription("description of job2")
            .create(JOB2);

    public static final String JOB3 = "job3";
    public static final Option job3 =
        OptionBuilder.hasArg(false)
            .isrequired(false)
            .withDescription("description of job3")
            .create(JOB3);

    public static final String MY_C = "C";
    public static final Option my_c =
        OptionBuilder.hasArg(true)
            .withArgName("count")
            .isrequired(false)
            .withDescription("description of C")
            .create(MY_C);

    public static final String MY_W = "W";
    public static final Option my_w =
        OptionBuilder.hasArg(true)
            .withArgName("width")
            .isrequired(false)
            .withDescription("description of W")
            .create(MY_W);


    public static void main(String[] args)
    {
        Options options = new Options();

        OptionGroup optgrp = new OptionGroup();
        optgrp.addOption(job1);
        optgrp.addOption(job2);
        optgrp.addOption(job3);

        options.addOptionGroup(optgrp);
        options.addOption(my_c);
        options.addOption(my_w);

        try {
            CommandLineParser parser = new GnuParser();
            CommandLine cmdline = parser.parse(options,args);

            if (((cmdline.hasOption(MY_C)) || (cmdline.hasOption(MY_W))) &&
                (! cmdline.hasOption(JOB1))) {
                HelpFormatter help = new HelpFormatter();
                help.printHelp("cmdname",options);
                System.exit(-1);
            }

            System.out.println("OK");
        }
        catch (Exception ex) {
            ex.printStackTrace();
            System.exit(-1);
        }
    }
}

其中产生以下输出

<~/sandBox/CoreUtils/scratch> $javac -d . -cp ~/sandBox/CoreUtils/lib/commons-cli-1.2.jar:. StackOverflowExample.java
<~/sandBox/CoreUtils/scratch> $java -cp ~/sandBox/CoreUtils/lib/commons-cli-1.2.jar:. StackOverflowExample -C foo -job1
OK

<~/sandBox/CoreUtils/scratch> $java -cp ~/sandBox/CoreUtils/lib/commons-cli-1.2.jar:. StackOverflowExample -C foo -job2
usage: cmdname
 -C <count>   description of C
 -job1        description of job1
 -job2        description of job2
 -job3        description of job3
 -W <width>   description of W

相关文章

ArrayList简介:ArrayList 的底层是数组队列,相当于动态数组。与 Java 中的数组相比,它的容量能动态增...
一、进程与线程 进程:是代码在数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位。 线程...
本文为博客园作者所写:&#160;一寸HUI,个人博客地址:https://www.cnblogs.com/zsql/ 简单的一个类...
#############java面向对象详解#############1、面向对象基本概念2、类与对象3、类和对象的定义格式4、...
一、什么是异常? 异常就是有异于常态,和正常情况不一样,有错误出错。在java中,阻止当前方法或作用域...
Collection接口 Collection接口 Collection接口 Collection是最基本的集合接口,一个Collection代表一组...