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

chenshuming commented on CLI-284:
---------------------------------

 [~fury]

+1, agree.

Option instance can have longOpt value null first and set real value later.

The check should in Options.addOption(Option) because it means we think Option 
instance is set up ready.

But if we add a check in Options.addOption(Option) , we want to throw a 
exception when check fail. That may break the binary compatibility.

> Inconsistency in constraints for creating an instance of class Option using 
> builder pattern or constructor
> ----------------------------------------------------------------------------------------------------------
>
>                 Key: CLI-284
>                 URL: https://issues.apache.org/jira/browse/CLI-284
>             Project: Commons CLI
>          Issue Type: Bug
>            Reporter: Dilraj Singh
>            Assignee: Bruno P. Kinoshita
>            Priority: Major
>
> Builder pattern for creating an instance of class *Option* ensures that at 
> least one of the representation (short and long representation) for the 
> option is not null. It throws an *IllegalArgumentException* in-case program 
> tries to build an instance with both the representations as null. Consider 
> the following code snippet for reference.
> {code:java}
> public static void main(String[] args) {
>   Option.Builder builder = Option.builder();
>   Option op = builder.build();
> } 
> {code}
> This piece of code fails with the following exception
> {noformat}
> Exception in thread "main" java.lang.IllegalArgumentException: Either opt or 
> longOpt must be specified
> {noformat}
> But if we try to create an instance of Option by calling its constructor, It 
> allows the creation with both opt and longOpt for Option as null. Consider 
> the following code snippet for reference
> {code:java}
> public static void main(String[] args) {
>   Option op = new Option(null, null);
>   System.out.format("Short Representation: %s\n" +
>   "Long Representation: %s", op.getOpt(), 
>   op.getLongOpt());
> }
> {code}
> Output:
> {noformat}
> Short Representation: null
> Long Representation: null
> {noformat}
> Calling a method on an instance with both opt and longOpt as null will lead 
> to undesired null pointer exception. For example, calling
> {code:java}
>  op.getId() {code}
> will throw a null pointer exception, thus violating the method contract.
> So as to fix this we need to make sure that whenever a constructor is invoked 
> it has a non-null argument value for at least one of the Option 
> representation. 



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to