[ 
https://issues.apache.org/jira/browse/CLI-230?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13575868#comment-13575868
 ] 

Thomas Neidhart commented on CLI-230:
-------------------------------------

In the current CLI version, it is not advised to alter Option objects after 
they have been added to an Options instance, as the changes will not be 
reflected, thus you should do something like that:

{noformat}
Options options = new Options();

Option o = OptionBuilder.withLongOpt("foo").isRequired().create("f");
options.addOption(o);
{noformat}

There are several flaws in the current API design of CLI, which will hopefully 
corrected with CLI2, but we can not do it right now as it would break 
binary/backwards compatibility.
                
> Unable to properly require options
> ----------------------------------
>
>                 Key: CLI-230
>                 URL: https://issues.apache.org/jira/browse/CLI-230
>             Project: Commons CLI
>          Issue Type: Bug
>          Components: CLI-1.x, Options definition
>    Affects Versions: 1.2
>         Environment: windows jdk1.6.0_21
>            Reporter: Alexander Fast
>
> Having some problems setting options as required. Have I misunderstood the 
> documentation?
>  * In test1() the option is required but also supplied, I expect to get no 
> exceptions.
>  * In test2() the option is required but *not* supplied, I expect to get an 
> exception.
> {code:java}
> public class Testing {
>     public static void main(String[] args) {
>         System.out.println("Begin test 1");
>         test1();
>         System.out.println("End test 1");
>         System.out.println("Begin test 2");
>         test2();
>         System.out.println("End test 2");
>     }
>     private static void test1() {
>         String[] args = new String[] { "--foo" };
>         Options options = new Options();
>         options.addOption("f", "foo", false, "");
>         options.getRequiredOptions().add(options.getOption("foo"));
>         GnuParser parser = new GnuParser();
>         try {
>             parser.parse(options, args);
>         } catch (ParseException e) {
>             // didn't expect to get MissingOptionException here
>             System.out.println("ERROR: " + e.getMessage());
>         }
>     }
>     private static void test2() {
>         String[] args = new String[] { };
>         Options options = new Options();
>         options.addOption("f", "foo", false, "");
>         options.getOption("f").setRequired(true);
>         GnuParser parser = new GnuParser();
>         try {
>             parser.parse(options, args);
>         } catch (ParseException e) {
>             // expected to get MissingOptionException here
>             System.out.println("ERROR: " + e.getMessage());
>         }
>     }
> }
> {code}

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

Reply via email to