开发者

Regarding Java Command Line Arguments

I have the below command line arguments set for the program. argument proc is mandatory argument chgval is optional and argument inputfile is optional.

./test.sh -proc mode1 -chval a -inputfile b.csv

I need to modify the below function so that either one of the optional argument should exists in command line arguments along with the mandatory argument proc . If i have the two optional arguments chval , inputfile in the command line along with the mandatory argument proc it's allowing now. I dont want it to happen it should throw an error.

Listed the valid values below and the rest should be an error

./test.sh -proc mode1 -chval a

./test.sh -proc mode1 -inputfile b.csv

./test.sh -proc mode1


 public static Options usage() {

      Option proc = OptionBuilder.withArgName("proc")
                                      .hasArg()
                                      .isRequired()
                                      .withDescription("Process Mode for testing:")

                                      .create("proc");
      Option chgval = OptionBuilder.withArgName("chgval")
                                      .hasArg()
                                      .withDescription("chg eeds to be Processed")
                                      .create("chgval");
      Option inputFile = OptionBuilder.withArgName("inputfile")
                                      .hasArg()
               开发者_Go百科                       .withDescription("Name of the input file")
                                      .create("inputfile");

      Options options = new Options();
      options.addOption(proc);
      options.addOption(chgval);
      options.addOption(inputFile);

      return options;
  }

What needs to be modified?


Dancrumb was correct, for mutually exclusive options you use OptionGroup. Here's how to use it for your case:

Options options = new Options();

OptionGroup group1 = new OptionGroup();
group1.addOption(chgval);
group1.addOption(inputFile);

options.addOption(proc);
options.addOptionGroup(group1);

So now, chgval and inputFile are mutually exclusive options. On an argument like "-proc mode1 -chgval a -inputfile b.csv", it will throw an AlreadySelectedException: "The option inputfile was specified but an option from this group has already been selected: chgval".

By the way, I also noticed the inconsistency in your post between chval and chgval.


If you mean that inputfile and chgval should be mutually exclusive, then you should use OptionGroup

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜