Java Apache CLI OptionBuilder not working as Builder pattern Java Apache CLI OptionBuilder not working as Builder pattern apache apache

Java Apache CLI OptionBuilder not working as Builder pattern


The problem is that every method in OptionBuilder is static, operating on static fields and returning a single static instance. Hence you don't require an instance of OptionBuilder to execute the methods. This doesn't marry well with the natural desire to chain the calls together, as you've done.

There is no solution other than to either calm the compiler down (perhaps disabling warnings in your IDE?) or adjust your code as follows (untested):

public static final Option job1;static {    OptionBuilder.hasArg(false);    OptionBuilder.isRequired(false)    OptionBuilder.withDescription("description of job1")    job1 = OptionBuilder.create(JOB1);}

It would be better if the OptionBuilder class was rewritten with a public no-argument constructor and only instance methods, thus behaving like every other builder out there. There is an existing bug in the commons-cli issue tracker highlighting this: https://issues.apache.org/jira/browse/CLI-224

Update: my patch has been submitted to trunk, so a new "proper" builder will be available in the next release of commons-cli (v1.3). See Javadocs here.