Skip to content

Commit 267bdf7

Browse files
committed
Add OptionGroup.isSelected()
Next version will be 1.9.0
1 parent 4a9c006 commit 267bdf7

7 files changed

Lines changed: 29 additions & 8 deletions

File tree

pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<modelVersion>4.0.0</modelVersion>
2525
<groupId>commons-cli</groupId>
2626
<artifactId>commons-cli</artifactId>
27-
<version>1.8.1-SNAPSHOT</version>
27+
<version>1.9.0-SNAPSHOT</version>
2828
<name>Apache Commons CLI</name>
2929
<inceptionYear>2002</inceptionYear>
3030
<description>
@@ -70,12 +70,12 @@
7070
<maven.compiler.target>1.8</maven.compiler.target>
7171
<commons.componentid>cli</commons.componentid>
7272
<commons.module.name>org.apache.commons.cli</commons.module.name>
73-
<commons.release.version>1.8.0</commons.release.version>
74-
<commons.release.next>1.8.1</commons.release.next>
73+
<commons.release.version>1.9.0</commons.release.version>
74+
<commons.release.next>1.9.1</commons.release.next>
7575
<commons.release.name>commons-cli-${commons.release.version}</commons.release.name>
7676
<commons.release.isDistModule>true</commons.release.isDistModule>
7777
<commons.rc.version>RC2</commons.rc.version>
78-
<commons.bc.version>1.7.0</commons.bc.version>
78+
<commons.bc.version>1.8.0</commons.bc.version>
7979
<commons.osgi.symbolicName>org.apache.commons.cli</commons.osgi.symbolicName>
8080
<commons.jira.id>CLI</commons.jira.id>
8181
<commons.jira.pid>12310463</commons.jira.pid>

src/changes/changes.xml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@
2222
<title>Apache Commons CLI Release Notes</title>
2323
</properties>
2424
<body>
25-
<release version="1.8.1" date="YYYY-MM-DD" description="This release contains new features and bug fixes and requires Java 8 or above.">
26-
<action type="add" issue="CLI-334" dev="ggregory" due-to="Eric Pugh">Fix Javadoc pathing #280.</action>
25+
<release version="1.9.0" date="YYYY-MM-DD" description="This release contains new features and bug fixes and requires Java 8 or above.">
26+
<!-- ADD -->
27+
<action type="add" dev="ggregory" due-to="Gary Gregory">Add OptionGroup.isSelected().</action>
28+
<!-- FIX -->
29+
<action type="fix" issue="CLI-334" dev="ggregory" due-to="Eric Pugh">Fix Javadoc pathing #280.</action>
30+
<!-- UPDATE -->
2731
</release>
2832
<release version="1.8.0" date="2024-05-18" description="This release contains new features and bug fixes and requires Java 8 or above.">
2933
<!-- ADD -->

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ private void handleProperties(final Properties properties) throws ParseException
443443
}
444444
// if the option is part of a group, check if another option of the group has been selected
445445
final OptionGroup group = options.getOptionGroup(opt);
446-
final boolean selected = group != null && group.getSelected() != null;
446+
final boolean selected = group != null && group.isSelected();
447447
if (!cmd.hasOption(option) && !selected) {
448448
// get the value from the properties
449449
final String value = properties.getProperty(option);

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@ public boolean isRequired() {
9191
return required;
9292
}
9393

94+
/**
95+
* Tests whether an option is selected.
96+
*
97+
* @return whether whether an option is selected.
98+
* @since 1.9.0
99+
*/
100+
public boolean isSelected() {
101+
return selected != null;
102+
}
103+
94104
/**
95105
* Sets whether this group is required.
96106
*

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ protected void processProperties(final Properties properties) throws ParseExcept
273273
}
274274
// if the option is part of a group, check if another option of the group has been selected
275275
final OptionGroup group = options.getOptionGroup(opt);
276-
final boolean selected = group != null && group.getSelected() != null;
276+
final boolean selected = group != null && group.isSelected();
277277
if (!cmd.hasOption(option) && !selected) {
278278
// get the value from the properties instance
279279
final String value = properties.getProperty(option);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,7 @@ public void testOptionAndRequiredOption() throws Exception {
542542
@Test
543543
public void testOptionGroup() throws Exception {
544544
final OptionGroup group = new OptionGroup();
545+
assertFalse(group.isSelected());
545546
group.addOption(OptionBuilder.create("a"));
546547
group.addOption(OptionBuilder.create("b"));
547548

@@ -550,6 +551,7 @@ public void testOptionGroup() throws Exception {
550551

551552
parser.parse(options, new String[] { "-b" });
552553

554+
assertTrue(group.isSelected());
553555
assertEquals("b", group.getSelected(), "selected option");
554556
}
555557

@@ -565,6 +567,7 @@ public void testOptionGroupLong() throws Exception {
565567
final CommandLine cl = parser.parse(options, new String[] { "--bar" });
566568

567569
assertTrue(cl.hasOption("bar"));
570+
assertTrue(group.isSelected());
568571
assertEquals("bar", group.getSelected(), "selected option");
569572
}
570573

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public void setUp() {
6363
@Test
6464
public void testGetNames() {
6565
final OptionGroup group = new OptionGroup();
66+
assertFalse(group.isSelected());
67+
6668
group.addOption(OptionBuilder.create('a'));
6769
group.addOption(OptionBuilder.create('b'));
6870

@@ -156,6 +158,7 @@ public void testTwoLongOptionsFromGroup() throws Exception {
156158
fail("two arguments from group not allowed");
157159
} catch (final AlreadySelectedException e) {
158160
assertNotNull(e.getOptionGroup(), "null option group");
161+
assertTrue(e.getOptionGroup().isSelected());
159162
assertEquals("f", e.getOptionGroup().getSelected(), "selected option");
160163
assertEquals("d", e.getOption().getOpt(), "option");
161164
}
@@ -183,6 +186,7 @@ public void testTwoOptionsFromGroup() throws Exception {
183186
fail("two arguments from group not allowed");
184187
} catch (final AlreadySelectedException e) {
185188
assertNotNull(e.getOptionGroup(), "null option group");
189+
assertTrue(e.getOptionGroup().isSelected());
186190
assertEquals("f", e.getOptionGroup().getSelected(), "selected option");
187191
assertEquals("d", e.getOption().getOpt(), "option");
188192
}

0 commit comments

Comments
 (0)