Skip to content

Commit 5560a08

Browse files
committed
Default options are now taken into account for the required options (CLI-202)
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/trunk@955420 13f79535-47bb-0310-9956-ffa450edef68
1 parent f588f55 commit 5560a08

3 files changed

Lines changed: 37 additions & 12 deletions

File tree

src/main/java/org/apache/commons/cli/DefaultParser.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public CommandLine parse(Options options, String[] arguments, Properties propert
136136
*
137137
* @param properties The value properties to be processed.
138138
*/
139-
private void handleProperties(Properties properties)
139+
private void handleProperties(Properties properties) throws ParseException
140140
{
141141
if (properties == null)
142142
{
@@ -169,7 +169,8 @@ else if (!("yes".equalsIgnoreCase(value)
169169
continue;
170170
}
171171

172-
cmd.addOption(opt);
172+
handleOption(opt);
173+
currentOption = null;
173174
}
174175
}
175176
}

src/main/java/org/apache/commons/cli/Parser.java

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ else if (t.startsWith("-"))
249249
*
250250
* @param properties The value properties to be processed.
251251
*/
252-
protected void processProperties(Properties properties)
252+
protected void processProperties(Properties properties) throws ParseException
253253
{
254254
if (properties == null)
255255
{
@@ -291,6 +291,7 @@ else if (!("yes".equalsIgnoreCase(value)
291291
}
292292

293293
cmd.addOption(opt);
294+
updateRequiredOptions(opt);
294295
}
295296
}
296297
}
@@ -376,7 +377,27 @@ protected void processOption(String arg, ListIterator iter) throws ParseExceptio
376377

377378
// get the option represented by arg
378379
Option opt = (Option) getOptions().getOption(arg).clone();
380+
381+
// update the required options and groups
382+
updateRequiredOptions(opt);
383+
384+
// if the option takes an argument value
385+
if (opt.hasArg())
386+
{
387+
processArgs(opt, iter);
388+
}
389+
390+
// set the option on the command line
391+
cmd.addOption(opt);
392+
}
379393

394+
/**
395+
* Removes the option or its group from the list of expected elements.
396+
*
397+
* @param opt
398+
*/
399+
private void updateRequiredOptions(Option opt) throws ParseException
400+
{
380401
// if the option is a required option remove the option from
381402
// the requiredOptions list
382403
if (opt.isRequired())
@@ -397,14 +418,5 @@ protected void processOption(String arg, ListIterator iter) throws ParseExceptio
397418

398419
group.setSelected(opt);
399420
}
400-
401-
// if the option takes an argument value
402-
if (opt.hasArg())
403-
{
404-
processArgs(opt, iter);
405-
}
406-
407-
// set the option on the command line
408-
cmd.addOption(opt);
409421
}
410422
}

src/test/java/org/apache/commons/cli/ParserTestCase.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,4 +986,16 @@ public void testPropertyOverrideValues() throws Exception
986986
assertEquals( "ink", cmd.getOptionValue("i") );
987987
assertTrue( !cmd.hasOption("fake") );
988988
}
989+
990+
public void testPropertyOptionRequired() throws Exception
991+
{
992+
Options opts = new Options();
993+
opts.addOption(OptionBuilder.isRequired().create("f"));
994+
995+
Properties properties = new Properties();
996+
properties.setProperty("f", "true");
997+
998+
CommandLine cmd = parse(parser, opts, null, properties);
999+
assertTrue(cmd.hasOption("f"));
1000+
}
9891001
}

0 commit comments

Comments
 (0)