Description
See junit test below.
The problems appears to be in org.apache.commons.cli.Parser.processProperties, around lines 252 - 259.
It's trying to get the Option object for the property's name, but if none was specified, then opt is null and the attempt to call opt.hasArg() at line 259 throws an NPE.
The fix should be to skip the remainder of the clause if the Option object is null.
Example unit test:
package test; import java.util.Properties; import org.apache.commons.cli.OptionBuilder; import org.apache.commons.cli.Options; import org.apache.commons.cli.PosixParser; import org.junit.Test; public class CommonsCliBugTest { @Test public void testCommonsCliOkay() throws Exception { String[] args = new String[] {}; Properties p = new Properties(); p.setProperty("alpha", "beta"); Options options = new Options(); options.addOption(OptionBuilder.withLongOpt("alpha").hasArg().withArgName("ALPHA").create()); PosixParser parser = new PosixParser(); parser.parse(options, args, p); } @Test public void testCommonsCliFails() throws Exception { String[] args = new String[] {}; Properties p = new Properties(); // Note this property is not specified as a valid option p.setProperty("gamma", "beta"); Options options = new Options(); options.addOption(OptionBuilder.withLongOpt("alpha").hasArg().withArgName("ALPHA").create()); PosixParser parser = new PosixParser(); parser.parse(options, args, p); } }
Attachments
Attachments
Issue Links
- duplicates
-
CLI-204 NullPointerException with unrecognized default options
-
- Closed
-