Skip to content

Commit 324b7f9

Browse files
committed
Fixed the bugs with the default options and the option groups (CLI-203 and CLI-204)
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/trunk@955501 13f79535-47bb-0310-9956-ffa450edef68
1 parent 5560a08 commit 324b7f9

4 files changed

Lines changed: 81 additions & 8 deletions

File tree

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,18 @@ private void handleProperties(Properties properties) throws ParseException
147147
{
148148
String option = e.nextElement().toString();
149149

150-
if (!cmd.hasOption(option))
150+
Option opt = options.getOption(option);
151+
if (opt == null)
152+
{
153+
throw new UnrecognizedOptionException("Default option wasn't defined", option);
154+
}
155+
156+
// if the option is part of a group, check if another option of the group has been selected
157+
OptionGroup group = options.getOptionGroup(opt);
158+
boolean selected = group != null && group.getSelected() != null;
159+
160+
if (!cmd.hasOption(option) && !selected)
151161
{
152-
Option opt = options.getOption(option);
153-
154162
// get the value from the properties
155163
String value = properties.getProperty(option);
156164

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,11 +259,19 @@ protected void processProperties(Properties properties) throws ParseException
259259
for (Enumeration e = properties.propertyNames(); e.hasMoreElements();)
260260
{
261261
String option = e.nextElement().toString();
262-
263-
if (!cmd.hasOption(option))
262+
263+
Option opt = options.getOption(option);
264+
if (opt == null)
265+
{
266+
throw new UnrecognizedOptionException("Default option wasn't defined", option);
267+
}
268+
269+
// if the option is part of a group, check if another option of the group has been selected
270+
OptionGroup group = options.getOptionGroup(opt);
271+
boolean selected = group != null && group.getSelected() != null;
272+
273+
if (!cmd.hasOption(option) && !selected)
264274
{
265-
Option opt = getOptions().getOption(option);
266-
267275
// get the value from the properties instance
268276
String value = properties.getProperty(option);
269277

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package org.apache.commons.cli;
1919

20+
import java.util.Properties;
21+
2022
import junit.framework.TestCase;
2123

2224
/**
@@ -26,7 +28,7 @@
2628
public class OptionGroupTest extends TestCase
2729
{
2830
private Options _options = null;
29-
private CommandLineParser parser = new PosixParser();
31+
private Parser parser = new PosixParser();
3032

3133
public void setUp()
3234
{
@@ -186,6 +188,18 @@ public void testTwoOptionsFromDifferentGroup() throws Exception
186188
assertTrue( "Confirm NO extra args", cl.getArgList().size() == 0);
187189
}
188190

191+
public void testTwoOptionsFromGroupWithProperties() throws Exception
192+
{
193+
String[] args = new String[] { "-f" };
194+
195+
Properties properties = new Properties();
196+
properties.put("d", "true");
197+
198+
CommandLine cl = parser.parse( _options, args, properties);
199+
assertTrue(cl.hasOption("f"));
200+
assertTrue(!cl.hasOption("d"));
201+
}
202+
189203
public void testValidLongOnlyOptions() throws Exception
190204
{
191205
CommandLine cl1 = parser.parse(_options, new String[]{"--export"});

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,4 +998,47 @@ public void testPropertyOptionRequired() throws Exception
998998
CommandLine cmd = parse(parser, opts, null, properties);
999999
assertTrue(cmd.hasOption("f"));
10001000
}
1001+
1002+
public void testPropertyOptionUnexpected() throws Exception
1003+
{
1004+
Options opts = new Options();
1005+
1006+
Properties properties = new Properties();
1007+
properties.setProperty("f", "true");
1008+
1009+
try {
1010+
parse(parser, opts, null, properties);
1011+
fail("UnrecognizedOptionException expected");
1012+
} catch (UnrecognizedOptionException e) {
1013+
// expected
1014+
}
1015+
}
1016+
1017+
public void testPropertyOptionGroup() throws Exception
1018+
{
1019+
Options opts = new Options();
1020+
1021+
OptionGroup group1 = new OptionGroup();
1022+
group1.addOption(new Option("a", null));
1023+
group1.addOption(new Option("b", null));
1024+
opts.addOptionGroup(group1);
1025+
1026+
OptionGroup group2 = new OptionGroup();
1027+
group2.addOption(new Option("x", null));
1028+
group2.addOption(new Option("y", null));
1029+
opts.addOptionGroup(group2);
1030+
1031+
String[] args = new String[] { "-a" };
1032+
1033+
Properties properties = new Properties();
1034+
properties.put("b", "true");
1035+
properties.put("x", "true");
1036+
1037+
CommandLine cmd = parse(parser, opts, args, properties);
1038+
1039+
assertTrue(cmd.hasOption("a"));
1040+
assertFalse(cmd.hasOption("b"));
1041+
assertTrue(cmd.hasOption("x"));
1042+
assertFalse(cmd.hasOption("y"));
1043+
}
10011044
}

0 commit comments

Comments
 (0)