This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-cli.git
The following commit(s) were added to refs/heads/master by this push:
new 91f3acf5 org.apache.commons.cli.Option.Builder.get() should throw
IllegalStateException instead of IllegalArgumentException
91f3acf5 is described below
commit 91f3acf591848ee4803526d820834dc2988f4c2a
Author: Gary D. Gregory <[email protected]>
AuthorDate: Tue Jul 29 13:22:40 2025 -0400
org.apache.commons.cli.Option.Builder.get() should throw
IllegalStateException instead of IllegalArgumentException
Is the state that's wrong, there are no arguments to validate anyway.
---
src/changes/changes.xml | 1 +
src/main/java/org/apache/commons/cli/Option.java | 7 +++----
src/test/java/org/apache/commons/cli/OptionTest.java | 10 +++++-----
3 files changed, 9 insertions(+), 9 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index a5b5d8f7..3d4a8034 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -38,6 +38,7 @@
<action type="fix" issue="CLI-349" dev="ggregory" due-to="Leo Fernandes,
Gary Gregory">Fail faster with a more precise NullPointerException:
Option.processValue() throws NullPointerException when passed null value with
value separator configured.</action>
<action type="fix" issue="CLI-344" dev="ggregory" due-to="Ruiqi Dong,
Gary Gregory">Fail faster with a more precise NullPointerException:
DefaultParser.parse() throws NullPointerException when options parameter is
null.</action>
<action type="fix" issue="CLI-347" dev="ggregory" due-to="Ruiqi Dong,
Gary Gregory">Options.addOptionGroup(OptionGroup) does not remove required
options from requiredOpts list.</action>
+ <action type="fix" dev="ggregory" due-to="Gary
Gregory">org.apache.commons.cli.Option.Builder.get() should throw
IllegalStateException instead of IllegalArgumentException.</action>
<!-- ADD -->
<action type="add" issue="CLI-339" dev="ggregory" due-to="Claude Warren,
Gary Gregory">Help formatter extension in the new package #314.</action>
<action type="add" dev="ggregory" due-to="Gary
Gregory">CommandLine.Builder implements Supplier<CommandLine>.</action>
diff --git a/src/main/java/org/apache/commons/cli/Option.java
b/src/main/java/org/apache/commons/cli/Option.java
index 675f802f..f8784c94 100644
--- a/src/main/java/org/apache/commons/cli/Option.java
+++ b/src/main/java/org/apache/commons/cli/Option.java
@@ -188,11 +188,11 @@ public class Option implements Cloneable, Serializable {
* Constructs an Option with the values declared by this {@link
Builder}.
*
* @return the new {@link Option}.
- * @throws IllegalArgumentException if neither {@code opt} or {@code
longOpt} has been set.
+ * @throws IllegalStateException if neither {@code opt} or {@code
longOpt} has been set.
*/
public Option get() {
if (option == null && longOption == null) {
- throw new IllegalArgumentException("Either opt or longOpt must
be specified");
+ throw new IllegalStateException("Either opt or longOpt must be
specified");
}
return new Option(this);
}
@@ -530,8 +530,7 @@ public class Option implements Cloneable, Serializable {
*/
@Deprecated
public boolean addValue(final String value) {
- throw new UnsupportedOperationException(
- "The addValue method is not intended for client use.
Subclasses should use the processValue method instead.");
+ throw new UnsupportedOperationException("The addValue method is not
intended for client use. Subclasses should use the processValue method
instead.");
}
/**
diff --git a/src/test/java/org/apache/commons/cli/OptionTest.java
b/src/test/java/org/apache/commons/cli/OptionTest.java
index d902a881..fbdb898f 100644
--- a/src/test/java/org/apache/commons/cli/OptionTest.java
+++ b/src/test/java/org/apache/commons/cli/OptionTest.java
@@ -109,27 +109,27 @@ class OptionTest {
@Test
void testBuilderDeprecatedBuildEmpty() {
- assertThrows(IllegalArgumentException.class, () ->
Option.builder().build());
+ assertThrows(IllegalStateException.class, () ->
Option.builder().build());
}
@Test
void testBuilderEmpty() {
- assertThrows(IllegalArgumentException.class, () ->
Option.builder().get());
+ assertThrows(IllegalStateException.class, () ->
Option.builder().get());
}
@Test
void testBuilderInsufficientParams1() {
- assertThrows(IllegalArgumentException.class, () ->
Option.builder().desc("desc").get());
+ assertThrows(IllegalStateException.class, () ->
Option.builder().desc("desc").get());
}
@Test
void testBuilderInsufficientParams2() {
- assertThrows(IllegalArgumentException.class, () ->
Option.builder(null).desc("desc").get());
+ assertThrows(IllegalStateException.class, () ->
Option.builder(null).desc("desc").get());
}
@Test
void testBuilderInvalidOptionName0() {
- assertThrows(IllegalArgumentException.class, () ->
Option.builder().option(null).get());
+ assertThrows(IllegalStateException.class, () ->
Option.builder().option(null).get());
assertThrows(IllegalArgumentException.class, () ->
Option.builder().option(""));
assertThrows(IllegalArgumentException.class, () ->
Option.builder().option(" "));
}