Skip to content

Commit 49b1497

Browse files
committed
DefaultParser.Builder implements Supplier<DefaultParser>
1 parent 4bae1b5 commit 49b1497

5 files changed

Lines changed: 35 additions & 18 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<!-- ADD -->
2828
<action type="add" issue="CLI-339" dev="ggregory" due-to="Claude Warren, Gary Gregory">Help formatter extension in the new package #314.</action>
2929
<action type="add" dev="ggregory" due-to="Gary Gregory">CommandLine.Builder implements Supplier&lt;CommandLine&gt;.</action>
30+
<action type="add" dev="ggregory" due-to="Gary Gregory">DefaultParser.Builder implements Supplier&lt;DefaultParser&gt;.</action>
3031
<!-- UPDATE -->
3132
<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>
3233
<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>

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
2222
import java.util.List;
2323
import java.util.Properties;
2424
import java.util.function.Consumer;
25+
import java.util.function.Supplier;
2526

2627
/**
2728
* Default parser.
@@ -44,7 +45,7 @@ public class DefaultParser implements CommandLineParser {
4445
*
4546
* @since 1.5.0
4647
*/
47-
public static final class Builder {
48+
public static final class Builder implements Supplier<DefaultParser> {
4849

4950
/** Flag indicating if partial matching of long options is supported. */
5051
private boolean allowPartialMatching = true;
@@ -74,8 +75,20 @@ private Builder() {
7475
*
7576
* @return the new {@link DefaultParser}
7677
* @since 1.5.0
78+
* @deprecated Use {@link #get()}.
7779
*/
80+
@Deprecated
7881
public DefaultParser build() {
82+
return get();
83+
}
84+
85+
/**
86+
* Builds an DefaultParser with the values declared by this {@link Builder}.
87+
*
88+
* @return the new {@link DefaultParser}
89+
* @since 1.10.0
90+
*/
91+
public DefaultParser get() {
7992
return new DefaultParser(allowPartialMatching, stripLeadingAndTrailingQuotes, deprecatedHandler);
8093
}
8194

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ public void testGetOptionValue(final String[] args, final Option opt, final Opti
358358
final String optValue, final boolean grpDep, final String grpValue, final Option grpOpt) throws ParseException {
359359
final Options options = new Options().addOptionGroup(optionGroup);
360360
final List<Option> handler = new ArrayList<>();
361-
final CommandLine commandLine = DefaultParser.builder().setDeprecatedHandler(handler::add).build().parse(options, args);
361+
final CommandLine commandLine = DefaultParser.builder().setDeprecatedHandler(handler::add).get().parse(options, args);
362362
final Supplier<String> thinger = () -> "thing";
363363
final OptionGroup otherGroup = new OptionGroup().addOption(Option.builder("o").longOpt("other").hasArg().build())
364364
.addOption(Option.builder().option("p").longOpt("part").hasArg().build());
@@ -464,7 +464,7 @@ public void testGetOptionValues(final String[] args, final Option opt, final Opt
464464
final String[] optValue, final boolean grpDep, final String[] grpValue, final Option grpOpt) throws ParseException {
465465
final Options options = new Options().addOptionGroup(optionGroup);
466466
final List<Option> handler = new ArrayList<>();
467-
final CommandLine commandLine = DefaultParser.builder().setDeprecatedHandler(handler::add).build().parse(options, args);
467+
final CommandLine commandLine = DefaultParser.builder().setDeprecatedHandler(handler::add).get().parse(options, args);
468468
final OptionGroup otherGroup = new OptionGroup().addOption(Option.builder("o").longOpt("other").hasArg().build())
469469
.addOption(Option.builder().option("p").longOpt("part").hasArg().build());
470470
final OptionGroup nullGroup = null;
@@ -509,7 +509,7 @@ public void testGetParsedOptionValue(final String[] args, final Option opt, fina
509509
final Integer optValue, final boolean grpDep, final Integer grpValue, final Option grpOpt) throws ParseException {
510510
final Options options = new Options().addOptionGroup(optionGroup);
511511
final List<Option> handler = new ArrayList<>();
512-
final CommandLine commandLine = DefaultParser.builder().setDeprecatedHandler(handler::add).build().parse(options, args);
512+
final CommandLine commandLine = DefaultParser.builder().setDeprecatedHandler(handler::add).get().parse(options, args);
513513
final Supplier<Integer> thinger = () -> 2;
514514
final OptionGroup otherGroup = new OptionGroup().addOption(Option.builder("o").longOpt("other").hasArg().build())
515515
.addOption(Option.builder().option("p").longOpt("part").hasArg().build());
@@ -617,7 +617,7 @@ public void testHasOption(final String[] args, final Option opt, final OptionGro
617617
final boolean has, final boolean grpDep, final boolean hasGrp, final Option grpOpt) throws ParseException {
618618
final Options options = new Options().addOptionGroup(optionGroup);
619619
final List<Option> handler = new ArrayList<>();
620-
final CommandLine commandLine = DefaultParser.builder().setDeprecatedHandler(handler::add).build().parse(options, args);
620+
final CommandLine commandLine = DefaultParser.builder().setDeprecatedHandler(handler::add).get().parse(options, args);
621621
final OptionGroup otherGroup = new OptionGroup().addOption(Option.builder("o").longOpt("other").hasArg().build())
622622
.addOption(Option.builder().option("p").longOpt("part").hasArg().build());
623623
final OptionGroup nullGroup = null;
@@ -674,7 +674,7 @@ public void testHasOptionNoDeprecationHandler(final String[] args, final Option
674674
final boolean has, final boolean grpDep, final boolean hasGrp, final Option grpOpt) throws ParseException {
675675
final Options options = new Options().addOptionGroup(optionGroup);
676676
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
677-
final CommandLine commandLine = DefaultParser.builder().build().parse(options, args);
677+
final CommandLine commandLine = DefaultParser.builder().get().parse(options, args);
678678
final PrintStream ps = System.out;
679679
try {
680680
System.setOut(new PrintStream(baos));
@@ -726,7 +726,7 @@ public void testHasOptionNullDeprecationHandler(final String[] args, final Optio
726726
final boolean has, final boolean grpDep, final boolean hasGrp, final Option grpOpt) throws ParseException {
727727
final Options options = new Options().addOptionGroup(optionGroup);
728728
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
729-
final CommandLine commandLine = DefaultParser.builder().setDeprecatedHandler(null).build().parse(options, args);
729+
final CommandLine commandLine = DefaultParser.builder().setDeprecatedHandler(null).get().parse(options, args);
730730
final PrintStream ps = System.out;
731731
try {
732732
System.setOut(new PrintStream(baos));
@@ -765,7 +765,7 @@ public void testHasOptionNullDeprecationHandler(final String[] args, final Optio
765765
public void testNoDeprecationHandler(final String[] args, final Option opt, final OptionGroup optionGroup, final boolean optDep,
766766
final String optValue, final boolean grpDep, final String grpValue, final Option grpOpt) throws ParseException {
767767
final Options options = new Options().addOptionGroup(optionGroup);
768-
final CommandLine commandLine = DefaultParser.builder().build().parse(options, args);
768+
final CommandLine commandLine = DefaultParser.builder().get().parse(options, args);
769769
final Supplier<String> thinger = () -> "thing";
770770
final Supplier<String> nullSupplier = null;
771771
final ByteArrayOutputStream baos = new ByteArrayOutputStream();

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
2424
import java.util.HashSet;
2525
import java.util.Set;
2626

27+
import org.apache.commons.cli.DefaultParser.Builder;
2728
import org.junit.jupiter.api.BeforeEach;
2829
import org.junit.jupiter.api.Test;
2930

@@ -42,12 +43,14 @@ public void setUp() {
4243
@Test
4344
public void testBuilder() {
4445
// @formatter:off
45-
parser = DefaultParser.builder()
46+
final Builder builder = DefaultParser.builder()
4647
.setStripLeadingAndTrailingQuotes(false)
4748
.setAllowPartialMatching(false)
48-
.setDeprecatedHandler(null)
49-
.build();
49+
.setDeprecatedHandler(null);
5050
// @formatter:on
51+
parser = builder.build();
52+
assertEquals(DefaultParser.class, parser.getClass());
53+
parser = builder.get();
5154
assertEquals(DefaultParser.class, parser.getClass());
5255
}
5356

@@ -82,7 +85,7 @@ public void testDeprecated() throws ParseException {
8285

8386
@Test
8487
public void testLongOptionQuoteHandlingWithoutStrip() throws Exception {
85-
parser = DefaultParser.builder().setStripLeadingAndTrailingQuotes(false).build();
88+
parser = DefaultParser.builder().setStripLeadingAndTrailingQuotes(false).get();
8689
final String[] args = {"--bfile", "\"quoted string\""};
8790

8891
final CommandLine cl = parser.parse(options, args);
@@ -92,7 +95,7 @@ public void testLongOptionQuoteHandlingWithoutStrip() throws Exception {
9295

9396
@Test
9497
public void testLongOptionQuoteHandlingWithStrip() throws Exception {
95-
parser = DefaultParser.builder().setStripLeadingAndTrailingQuotes(true).build();
98+
parser = DefaultParser.builder().setStripLeadingAndTrailingQuotes(true).get();
9699
final String[] args = {"--bfile", "\"quoted string\""};
97100

98101
final CommandLine cl = parser.parse(options, args);
@@ -112,7 +115,7 @@ public void testLongOptionWithEqualsQuoteHandling() throws Exception {
112115

113116
@Test
114117
public void testLongOptionWithEqualsQuoteHandlingWithoutStrip() throws Exception {
115-
parser = DefaultParser.builder().setStripLeadingAndTrailingQuotes(false).build();
118+
parser = DefaultParser.builder().setStripLeadingAndTrailingQuotes(false).get();
116119
final String[] args = {"--bfile=\"quoted string\""};
117120

118121
final CommandLine cl = parser.parse(options, args);
@@ -122,7 +125,7 @@ public void testLongOptionWithEqualsQuoteHandlingWithoutStrip() throws Exception
122125

123126
@Test
124127
public void testLongOptionWithEqualsQuoteHandlingWithStrip() throws Exception {
125-
parser = DefaultParser.builder().setStripLeadingAndTrailingQuotes(true).build();
128+
parser = DefaultParser.builder().setStripLeadingAndTrailingQuotes(true).get();
126129
final String[] args = {"--bfile=\"quoted string\""};
127130

128131
final CommandLine cl = parser.parse(options, args);
@@ -143,7 +146,7 @@ public void testShortOptionConcatenatedQuoteHandling() throws Exception {
143146

144147
@Test
145148
public void testShortOptionQuoteHandlingWithoutStrip() throws Exception {
146-
parser = DefaultParser.builder().setStripLeadingAndTrailingQuotes(false).build();
149+
parser = DefaultParser.builder().setStripLeadingAndTrailingQuotes(false).get();
147150
final String[] args = {"-b", "\"quoted string\""};
148151

149152
final CommandLine cl = parser.parse(options, args);
@@ -153,7 +156,7 @@ public void testShortOptionQuoteHandlingWithoutStrip() throws Exception {
153156

154157
@Test
155158
public void testShortOptionQuoteHandlingWithStrip() throws Exception {
156-
parser = DefaultParser.builder().setStripLeadingAndTrailingQuotes(true).build();
159+
parser = DefaultParser.builder().setStripLeadingAndTrailingQuotes(true).get();
157160
final String[] args = {"-b", "\"quoted string\""};
158161

159162
final CommandLine cl = parser.parse(options, args);

src/test/java/org/apache/commons/cli/bug/BugCLI325Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void testCli325() throws ParseException {
4040
.build();
4141
// @formatter:on
4242
final String[] args = {"-x", "A=a", "B=b"};
43-
final CommandLine cmdLine = DefaultParser.builder().build().parse(new Options().addOption(option), args);
43+
final CommandLine cmdLine = DefaultParser.builder().get().parse(new Options().addOption(option), args);
4444
final Properties props = cmdLine.getOptionProperties(option);
4545
assertEquals(2, props.size());
4646
assertEquals("a", props.get("A"));

0 commit comments

Comments
 (0)