commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Benedikt Ritter <brit...@apache.org>
Subject Re: [cli]
Date Mon, 25 Jan 2016 08:03:57 GMT
Hello Pooja,

2016-01-21 6:54 GMT+01:00 Chitnis, Pooja <Pooja_Chitnis@bmc.com>:

> Hi cli dev team,
>
> I recently upgraded to the commons-cli version 1.3.1 where the BasicParser
> is deprecated. So if I sue the DefaultParser instead, my following sample
> code would not work -
>
> public static void main(String[] args) throws Exception {
>         Options options = getOptions();
>         if (args != null && args.length == 1 && args[0].equals("-?"))
{
>             CommandLineUtils.printHelp("adfg", options);
>             System.exit(2);
>         }
>         CommandLineParser parser = new DefaultParser();
>         CommandLine cmd = parser.parse(options, args);
>         String name1 = cmd.getOptionValue("name1");
>         String name2 = cmd.getOptionValue("name2");
>         String name3 = cmd.getOptionValue("name3");
>
>         System.out.println("name1 : " + name1);
>         System.out.println("name2 : " + name2);
>         System.out.println("name3 : " + name3);
>     }
>
>     private static Options getOptions() {
>         Options options = new Options();
>         Option asOption = new Option("name1", true, "name1");
>         asOption.setRequired(true);
>         options.addOption(asOption);
>         Option dsOption = new Option("name2", true, "name2");
>         dsOption.setOptionalArg(true);
>         dsOption.setRequired(false);
>         options.addOption(dsOption);
>         Option dcOption = new Option("name3", true, "name3");
>         dcOption.setRequired(true);
>         options.addOption(dcOption);
>         return options;
>     }
> The input args passed to main method are: -name1 val1 -name2 -name3 val3
> Note that I have not passed val for the -name2. If I remove the -name2 it
> will work but its user driven. Previously with BasicParser this use to work.
>
> Here it throws following exception
> Exception in thread "main" org.apache.commons.cli.MissingOptionException:
> Missing required option: name3
>        at
> org.apache.commons.cli.DefaultParser.checkRequiredOptions(DefaultParser.java:199)
>        at
> org.apache.commons.cli.DefaultParser.parse(DefaultParser.java:130)
>        at org.apache.commons.cli.DefaultParser.parse(DefaultParser.java:76)
>        at org.apache.commons.cli.DefaultParser.parse(DefaultParser.java:60)
>        at test.DefaultParserTest.main(DefaultParserTest.java:26)
>
> It seems a bug in the DefaultParser. Can you please fix this?
>

You could file an issue in our bug tracker [1]. I haven't seen much
activity in [cli] since the last release, so patches welcome! :-)

Regards,
Benedikt

[1] https://issues.apache.org/jira/browse/cli


>
> Thanks,
> -Pooja
>



-- 
http://home.apache.org/~britter/
http://twitter.com/BenediktRitter
http://github.com/britter

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message