Skip to content

Commit 4535fae

Browse files
committed
CommandLine.Builder implements Supplier<CommandLine>
1 parent 62d1ba7 commit 4535fae

5 files changed

Lines changed: 31 additions & 7 deletions

File tree

src/changes/changes.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
<release version="1.9.1" date="YYYY-MM-DD" description="This is a feature and maintenance release. Java 8 or later is required.">
2626
<!-- FIX -->
2727
<!-- ADD -->
28-
<action type="add" issue="CLI-339" dev="ggregory" due-to="Claude Warren, Gary Gregory">Help formatter extension #314.</action>
28+
<action type="add" issue="CLI-339" dev="ggregory" due-to="Claude Warren, Gary Gregory">Help formatter extension in the new package #314.</action>
29+
<action type="add" dev="ggregory" due-to="Gary Gregory">CommandLine.Builder implements Supplier&lt;CommandLine&gt;.</action>
2930
<!-- UPDATE -->
3031
<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>
3132
<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/CommandLine.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class CommandLine implements Serializable {
4444
*
4545
* @since 1.4
4646
*/
47-
public static final class Builder {
47+
public static final class Builder implements Supplier<CommandLine> {
4848

4949
/**
5050
* Prints an Option to {@link System#out}.
@@ -91,11 +91,24 @@ public Builder addOption(final Option opt) {
9191
}
9292

9393
/**
94-
* Creates the new instance.
94+
* Creates a new instance.
9595
*
96-
* @return the new instance.
96+
* @return a new instance.
97+
* @deprecated Use {@link #get()}.
9798
*/
99+
@Deprecated
98100
public CommandLine build() {
101+
return get();
102+
}
103+
104+
/**
105+
* Creates a new instance.
106+
*
107+
* @return a new instance.
108+
* @since 1.10.0
109+
*/
110+
@Override
111+
public CommandLine get() {
99112
return new CommandLine(args, options, deprecatedHandler);
100113
}
101114

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ public CommandLine parse(final Options options, final String[] arguments, final
710710
for (final OptionGroup group : options.getOptionGroups()) {
711711
group.setSelected(null);
712712
}
713-
cmd = CommandLine.builder().setDeprecatedHandler(deprecatedHandler).build();
713+
cmd = CommandLine.builder().setDeprecatedHandler(deprecatedHandler).get();
714714
if (arguments != null) {
715715
for (final String argument : arguments) {
716716
handleToken(argument);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public CommandLine parse(final Options options, final String[] arguments, final
149149
}
150150
// initialize members
151151
setOptions(options);
152-
cmd = CommandLine.builder().build();
152+
cmd = CommandLine.builder().get();
153153
boolean eatTheRest = false;
154154
final List<String> tokenList = Arrays.asList(flatten(getOptions(), arguments == null ? new String[0] : arguments, stopAtNonOption));
155155
final ListIterator<String> iterator = tokenList.listIterator();

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,22 @@ public void testBadGetParsedOptionValue() throws Exception {
218218
}
219219

220220
@Test
221-
public void testBuilder() {
221+
public void testBuilderBuild() {
222222
final CommandLine.Builder builder = new CommandLine.Builder();
223223
builder.addArg("foo").addArg("bar");
224224
builder.addOption(Option.builder("T").build());
225225
final CommandLine cmd = builder.build();
226+
assertEquals("foo", cmd.getArgs()[0]);
227+
assertEquals("bar", cmd.getArgList().get(1));
228+
assertEquals("T", cmd.getOptions()[0].getOpt());
229+
}
226230

231+
@Test
232+
public void testBuilderGet() {
233+
final CommandLine.Builder builder = new CommandLine.Builder();
234+
builder.addArg("foo").addArg("bar");
235+
builder.addOption(Option.builder("T").build());
236+
final CommandLine cmd = builder.get();
227237
assertEquals("foo", cmd.getArgs()[0]);
228238
assertEquals("bar", cmd.getArgList().get(1));
229239
assertEquals("T", cmd.getOptions()[0].getOpt());

0 commit comments

Comments
 (0)