Author: ebourg Date: Wed Jun 16 22:59:59 2010 New Revision: 955420 URL: http://svn.apache.org/viewvc?rev=955420&view=rev Log: Default options are now taken into account for the required options (CLI-202)
Modified: commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/DefaultParser.java commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/Parser.java commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/ParserTestCase.java Modified: commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/DefaultParser.java URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/DefaultParser.java?rev=955420&r1=955419&r2=955420&view=diff ============================================================================== --- commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/DefaultParser.java (original) +++ commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/DefaultParser.java Wed Jun 16 22:59:59 2010 @@ -136,7 +136,7 @@ public class DefaultParser implements Co * * @param properties The value properties to be processed. */ - private void handleProperties(Properties properties) + private void handleProperties(Properties properties) throws ParseException { if (properties == null) { @@ -169,7 +169,8 @@ public class DefaultParser implements Co continue; } - cmd.addOption(opt); + handleOption(opt); + currentOption = null; } } } Modified: commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/Parser.java URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/Parser.java?rev=955420&r1=955419&r2=955420&view=diff ============================================================================== --- commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/Parser.java (original) +++ commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/Parser.java Wed Jun 16 22:59:59 2010 @@ -249,7 +249,7 @@ public abstract class Parser implements * * @param properties The value properties to be processed. */ - protected void processProperties(Properties properties) + protected void processProperties(Properties properties) throws ParseException { if (properties == null) { @@ -291,6 +291,7 @@ public abstract class Parser implements } cmd.addOption(opt); + updateRequiredOptions(opt); } } } @@ -376,7 +377,27 @@ public abstract class Parser implements // get the option represented by arg Option opt = (Option) getOptions().getOption(arg).clone(); + + // update the required options and groups + updateRequiredOptions(opt); + + // if the option takes an argument value + if (opt.hasArg()) + { + processArgs(opt, iter); + } + + // set the option on the command line + cmd.addOption(opt); + } + /** + * Removes the option or its group from the list of expected elements. + * + * @param opt + */ + private void updateRequiredOptions(Option opt) throws ParseException + { // if the option is a required option remove the option from // the requiredOptions list if (opt.isRequired()) @@ -397,14 +418,5 @@ public abstract class Parser implements group.setSelected(opt); } - - // if the option takes an argument value - if (opt.hasArg()) - { - processArgs(opt, iter); - } - - // set the option on the command line - cmd.addOption(opt); } } Modified: commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/ParserTestCase.java URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/ParserTestCase.java?rev=955420&r1=955419&r2=955420&view=diff ============================================================================== --- commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/ParserTestCase.java (original) +++ commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/ParserTestCase.java Wed Jun 16 22:59:59 2010 @@ -986,4 +986,16 @@ public abstract class ParserTestCase ext assertEquals( "ink", cmd.getOptionValue("i") ); assertTrue( !cmd.hasOption("fake") ); } + + public void testPropertyOptionRequired() throws Exception + { + Options opts = new Options(); + opts.addOption(OptionBuilder.isRequired().create("f")); + + Properties properties = new Properties(); + properties.setProperty("f", "true"); + + CommandLine cmd = parse(parser, opts, null, properties); + assertTrue(cmd.hasOption("f")); + } }