Java – how to specify multiple options using Apache commons cli?

What I want:

java programName  -jobs1 -C 10 -W 20
java programName  -job2
java programName  -job3

Content:

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 );

... the location of the output is:

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

I hope - job1 it should print - job1 - C < > - W < >

Did I miss anything? It does not apply to multiple parameters By the way, I used commons cli 1.2

Solution

You cannot have context sensitive parameters You can have the following parameters: job1, job2, job3, C & W, but you can't say (through the Library) that C & W is only valid for job1

If job1 / 2 / 3 is mutually exclusive, create an OptionGroup Then, in the code, make sure that C & W is only used for 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);
        }
    }
}

The following outputs are generated:

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

<~/sand@R_139_2419@/CoreUtils/scratch> $java -cp ~/sand@R_139_2419@/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
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>