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 49b14971 DefaultParser.Builder implements Supplier<DefaultParser>
49b14971 is described below
commit 49b14971cd3ce9be786004875288ef678c778488
Author: Gary Gregory <[email protected]>
AuthorDate: Thu Oct 17 11:10:16 2024 -0400
DefaultParser.Builder implements Supplier<DefaultParser>
---
src/changes/changes.xml | 1 +
.../java/org/apache/commons/cli/DefaultParser.java | 15 ++++++++++++++-
.../org/apache/commons/cli/CommandLineTest.java | 14 +++++++-------
.../org/apache/commons/cli/DefaultParserTest.java | 21 ++++++++++++---------
.../org/apache/commons/cli/bug/BugCLI325Test.java | 2 +-
5 files changed, 35 insertions(+), 18 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 801e474e..0a9cc72b 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -27,6 +27,7 @@
<!-- 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>
+ <action type="add" dev="ggregory" due-to="Gary
Gregory">DefaultParser.Builder implements
Supplier<DefaultParser>.</action>
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Gary Gregory,
Dependabot">Bump org.apache.commons:commons-parent from 72 to 77 #302, #304,
#310, #315, #320.</action>
<action type="update" dev="ggregory" due-to="Gary Gregory,
Dependabot">Bump commons-io:commons-io from 2.16.1 to 2.17.0 #309.</action>
diff --git a/src/main/java/org/apache/commons/cli/DefaultParser.java
b/src/main/java/org/apache/commons/cli/DefaultParser.java
index f78d5da6..7b88a7da 100644
--- a/src/main/java/org/apache/commons/cli/DefaultParser.java
+++ b/src/main/java/org/apache/commons/cli/DefaultParser.java
@@ -22,6 +22,7 @@ import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import java.util.function.Consumer;
+import java.util.function.Supplier;
/**
* Default parser.
@@ -44,7 +45,7 @@ public class DefaultParser implements CommandLineParser {
*
* @since 1.5.0
*/
- public static final class Builder {
+ public static final class Builder implements Supplier<DefaultParser> {
/** Flag indicating if partial matching of long options is supported.
*/
private boolean allowPartialMatching = true;
@@ -74,8 +75,20 @@ public class DefaultParser implements CommandLineParser {
*
* @return the new {@link DefaultParser}
* @since 1.5.0
+ * @deprecated Use {@link #get()}.
*/
+ @Deprecated
public DefaultParser build() {
+ return get();
+ }
+
+ /**
+ * Builds an DefaultParser with the values declared by this {@link
Builder}.
+ *
+ * @return the new {@link DefaultParser}
+ * @since 1.10.0
+ */
+ public DefaultParser get() {
return new DefaultParser(allowPartialMatching,
stripLeadingAndTrailingQuotes, deprecatedHandler);
}
diff --git a/src/test/java/org/apache/commons/cli/CommandLineTest.java
b/src/test/java/org/apache/commons/cli/CommandLineTest.java
index 7c80fe5c..27f87baa 100644
--- a/src/test/java/org/apache/commons/cli/CommandLineTest.java
+++ b/src/test/java/org/apache/commons/cli/CommandLineTest.java
@@ -358,7 +358,7 @@ public class CommandLineTest {
final String optValue, final boolean
grpDep, final String grpValue, final Option grpOpt) throws ParseException {
final Options options = new Options().addOptionGroup(optionGroup);
final List<Option> handler = new ArrayList<>();
- final CommandLine commandLine =
DefaultParser.builder().setDeprecatedHandler(handler::add).build().parse(options,
args);
+ final CommandLine commandLine =
DefaultParser.builder().setDeprecatedHandler(handler::add).get().parse(options,
args);
final Supplier<String> thinger = () -> "thing";
final OptionGroup otherGroup = new
OptionGroup().addOption(Option.builder("o").longOpt("other").hasArg().build())
.addOption(Option.builder().option("p").longOpt("part").hasArg().build());
@@ -464,7 +464,7 @@ public class CommandLineTest {
final String[] optValue, final boolean
grpDep, final String[] grpValue, final Option grpOpt) throws ParseException {
final Options options = new Options().addOptionGroup(optionGroup);
final List<Option> handler = new ArrayList<>();
- final CommandLine commandLine =
DefaultParser.builder().setDeprecatedHandler(handler::add).build().parse(options,
args);
+ final CommandLine commandLine =
DefaultParser.builder().setDeprecatedHandler(handler::add).get().parse(options,
args);
final OptionGroup otherGroup = new
OptionGroup().addOption(Option.builder("o").longOpt("other").hasArg().build())
.addOption(Option.builder().option("p").longOpt("part").hasArg().build());
final OptionGroup nullGroup = null;
@@ -509,7 +509,7 @@ public class CommandLineTest {
final Integer optValue, final boolean
grpDep, final Integer grpValue, final Option grpOpt) throws ParseException {
final Options options = new Options().addOptionGroup(optionGroup);
final List<Option> handler = new ArrayList<>();
- final CommandLine commandLine =
DefaultParser.builder().setDeprecatedHandler(handler::add).build().parse(options,
args);
+ final CommandLine commandLine =
DefaultParser.builder().setDeprecatedHandler(handler::add).get().parse(options,
args);
final Supplier<Integer> thinger = () -> 2;
final OptionGroup otherGroup = new
OptionGroup().addOption(Option.builder("o").longOpt("other").hasArg().build())
.addOption(Option.builder().option("p").longOpt("part").hasArg().build());
@@ -617,7 +617,7 @@ public class CommandLineTest {
final boolean has, final boolean grpDep, final
boolean hasGrp, final Option grpOpt) throws ParseException {
final Options options = new Options().addOptionGroup(optionGroup);
final List<Option> handler = new ArrayList<>();
- final CommandLine commandLine =
DefaultParser.builder().setDeprecatedHandler(handler::add).build().parse(options,
args);
+ final CommandLine commandLine =
DefaultParser.builder().setDeprecatedHandler(handler::add).get().parse(options,
args);
final OptionGroup otherGroup = new
OptionGroup().addOption(Option.builder("o").longOpt("other").hasArg().build())
.addOption(Option.builder().option("p").longOpt("part").hasArg().build());
final OptionGroup nullGroup = null;
@@ -674,7 +674,7 @@ public class CommandLineTest {
final boolean has, final boolean grpDep, final
boolean hasGrp, final Option grpOpt) throws ParseException {
final Options options = new Options().addOptionGroup(optionGroup);
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
- final CommandLine commandLine =
DefaultParser.builder().build().parse(options, args);
+ final CommandLine commandLine =
DefaultParser.builder().get().parse(options, args);
final PrintStream ps = System.out;
try {
System.setOut(new PrintStream(baos));
@@ -726,7 +726,7 @@ public class CommandLineTest {
final boolean has, final
boolean grpDep, final boolean hasGrp, final Option grpOpt) throws
ParseException {
final Options options = new Options().addOptionGroup(optionGroup);
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
- final CommandLine commandLine =
DefaultParser.builder().setDeprecatedHandler(null).build().parse(options, args);
+ final CommandLine commandLine =
DefaultParser.builder().setDeprecatedHandler(null).get().parse(options, args);
final PrintStream ps = System.out;
try {
System.setOut(new PrintStream(baos));
@@ -765,7 +765,7 @@ public class CommandLineTest {
public void testNoDeprecationHandler(final String[] args, final Option
opt, final OptionGroup optionGroup, final boolean optDep,
final String optValue, final boolean
grpDep, final String grpValue, final Option grpOpt) throws ParseException {
final Options options = new Options().addOptionGroup(optionGroup);
- final CommandLine commandLine =
DefaultParser.builder().build().parse(options, args);
+ final CommandLine commandLine =
DefaultParser.builder().get().parse(options, args);
final Supplier<String> thinger = () -> "thing";
final Supplier<String> nullSupplier = null;
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
diff --git a/src/test/java/org/apache/commons/cli/DefaultParserTest.java
b/src/test/java/org/apache/commons/cli/DefaultParserTest.java
index 74795c6a..0b829e00 100644
--- a/src/test/java/org/apache/commons/cli/DefaultParserTest.java
+++ b/src/test/java/org/apache/commons/cli/DefaultParserTest.java
@@ -24,6 +24,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.HashSet;
import java.util.Set;
+import org.apache.commons.cli.DefaultParser.Builder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -42,12 +43,14 @@ public class DefaultParserTest extends
AbstractParserTestCase {
@Test
public void testBuilder() {
// @formatter:off
- parser = DefaultParser.builder()
+ final Builder builder = DefaultParser.builder()
.setStripLeadingAndTrailingQuotes(false)
.setAllowPartialMatching(false)
- .setDeprecatedHandler(null)
- .build();
+ .setDeprecatedHandler(null);
// @formatter:on
+ parser = builder.build();
+ assertEquals(DefaultParser.class, parser.getClass());
+ parser = builder.get();
assertEquals(DefaultParser.class, parser.getClass());
}
@@ -82,7 +85,7 @@ public class DefaultParserTest extends AbstractParserTestCase
{
@Test
public void testLongOptionQuoteHandlingWithoutStrip() throws Exception {
- parser =
DefaultParser.builder().setStripLeadingAndTrailingQuotes(false).build();
+ parser =
DefaultParser.builder().setStripLeadingAndTrailingQuotes(false).get();
final String[] args = {"--bfile", "\"quoted string\""};
final CommandLine cl = parser.parse(options, args);
@@ -92,7 +95,7 @@ public class DefaultParserTest extends AbstractParserTestCase
{
@Test
public void testLongOptionQuoteHandlingWithStrip() throws Exception {
- parser =
DefaultParser.builder().setStripLeadingAndTrailingQuotes(true).build();
+ parser =
DefaultParser.builder().setStripLeadingAndTrailingQuotes(true).get();
final String[] args = {"--bfile", "\"quoted string\""};
final CommandLine cl = parser.parse(options, args);
@@ -112,7 +115,7 @@ public class DefaultParserTest extends
AbstractParserTestCase {
@Test
public void testLongOptionWithEqualsQuoteHandlingWithoutStrip() throws
Exception {
- parser =
DefaultParser.builder().setStripLeadingAndTrailingQuotes(false).build();
+ parser =
DefaultParser.builder().setStripLeadingAndTrailingQuotes(false).get();
final String[] args = {"--bfile=\"quoted string\""};
final CommandLine cl = parser.parse(options, args);
@@ -122,7 +125,7 @@ public class DefaultParserTest extends
AbstractParserTestCase {
@Test
public void testLongOptionWithEqualsQuoteHandlingWithStrip() throws
Exception {
- parser =
DefaultParser.builder().setStripLeadingAndTrailingQuotes(true).build();
+ parser =
DefaultParser.builder().setStripLeadingAndTrailingQuotes(true).get();
final String[] args = {"--bfile=\"quoted string\""};
final CommandLine cl = parser.parse(options, args);
@@ -143,7 +146,7 @@ public class DefaultParserTest extends
AbstractParserTestCase {
@Test
public void testShortOptionQuoteHandlingWithoutStrip() throws Exception {
- parser =
DefaultParser.builder().setStripLeadingAndTrailingQuotes(false).build();
+ parser =
DefaultParser.builder().setStripLeadingAndTrailingQuotes(false).get();
final String[] args = {"-b", "\"quoted string\""};
final CommandLine cl = parser.parse(options, args);
@@ -153,7 +156,7 @@ public class DefaultParserTest extends
AbstractParserTestCase {
@Test
public void testShortOptionQuoteHandlingWithStrip() throws Exception {
- parser =
DefaultParser.builder().setStripLeadingAndTrailingQuotes(true).build();
+ parser =
DefaultParser.builder().setStripLeadingAndTrailingQuotes(true).get();
final String[] args = {"-b", "\"quoted string\""};
final CommandLine cl = parser.parse(options, args);
diff --git a/src/test/java/org/apache/commons/cli/bug/BugCLI325Test.java
b/src/test/java/org/apache/commons/cli/bug/BugCLI325Test.java
index aae6a243..10f2069e 100644
--- a/src/test/java/org/apache/commons/cli/bug/BugCLI325Test.java
+++ b/src/test/java/org/apache/commons/cli/bug/BugCLI325Test.java
@@ -40,7 +40,7 @@ public class BugCLI325Test {
.build();
// @formatter:on
final String[] args = {"-x", "A=a", "B=b"};
- final CommandLine cmdLine = DefaultParser.builder().build().parse(new
Options().addOption(option), args);
+ final CommandLine cmdLine = DefaultParser.builder().get().parse(new
Options().addOption(option), args);
final Properties props = cmdLine.getOptionProperties(option);
assertEquals(2, props.size());
assertEquals("a", props.get("A"));