Skip to content

Commit 81f7c1e

Browse files
committed
Add builder factory CommandLine#builder()
1 parent 1b44795 commit 81f7c1e

5 files changed

Lines changed: 51 additions & 13 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<action type="add" issue="CLI-324" dev="ggregory" due-to="Claude Warren, Gary Gregory">Make adding OptionGroups and Options to existing Options easier #230.</action>
3737
<action type="add" issue="CLI-323" dev="ggregory" due-to="Claude Warren, Gary Gregory">Added Supplier&lt;T&gt; defaults for getParsedOptionValue #229.</action>
3838
<action type="add" issue="CLI-326" dev="ggregory" due-to="Claude Warren, Gary Gregory">Make Option.getKey() public #239.</action>
39+
<action type="add" dev="ggregory" due-to="Claude Warren, Gary Gregory">Add builder factory CommandLine#builder().</action>
3940
<!-- UPDATE -->
4041
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump commons-parent from 64 to 67.</action>
4142
<action type="update" dev="ggregory" due-to="Elric, Gary Gregory">Update the tests to JUnit 5 #238.</action>

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

Lines changed: 34 additions & 10 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.Iterator;
2525
import java.util.LinkedList;
2626
import java.util.List;
27+
import java.util.Objects;
2728
import java.util.Properties;
2829
import java.util.function.Supplier;
2930

@@ -46,10 +47,11 @@ public class CommandLine implements Serializable {
4647
*/
4748
public static final class Builder {
4849

49-
/**
50-
* CommandLine that is being build by this Builder.
51-
*/
52-
private final CommandLine commandLine = new CommandLine();
50+
/** The unrecognized options/arguments */
51+
private final List<String> args = new LinkedList<>();
52+
53+
/** The processed options */
54+
private final List<Option> options = new ArrayList<>();
5355

5456
/**
5557
* Adds left-over unrecognized option/argument.
@@ -59,7 +61,9 @@ public static final class Builder {
5961
* @return this Builder instance for method chaining.
6062
*/
6163
public Builder addArg(final String arg) {
62-
commandLine.addArg(arg);
64+
if (arg != null) {
65+
args.add(arg);
66+
}
6367
return this;
6468
}
6569

@@ -71,7 +75,9 @@ public Builder addArg(final String arg) {
7175
* @return this Builder instance for method chaining.
7276
*/
7377
public Builder addOption(final Option opt) {
74-
commandLine.addOption(opt);
78+
if (opt != null) {
79+
options.add(opt);
80+
}
7581
return this;
7682
}
7783

@@ -81,24 +87,42 @@ public Builder addOption(final Option opt) {
8187
* @return the new instance.
8288
*/
8389
public CommandLine build() {
84-
return commandLine;
90+
return new CommandLine(args, options);
8591
}
8692
}
8793

94+
/**
95+
* Creates a new builder.
96+
*
97+
* @return a new builder.
98+
* @since 1.7.0
99+
*/
100+
public static Builder builder() {
101+
return new Builder();
102+
}
103+
88104
/** The serial version UID. */
89105
private static final long serialVersionUID = 1L;
90106

91107
/** The unrecognized options/arguments */
92-
private final List<String> args = new LinkedList<>();
108+
private final List<String> args;
93109

94110
/** The processed options */
95-
private final List<Option> options = new ArrayList<>();
111+
private final List<Option> options;
96112

97113
/**
98114
* Creates a command line.
99115
*/
100116
protected CommandLine() {
101-
// nothing to do
117+
this(new LinkedList<>(), new ArrayList<>());
118+
}
119+
120+
/**
121+
* Creates a command line.
122+
*/
123+
private CommandLine(final List<String> args, final List<Option> options) {
124+
this.args = Objects.requireNonNull(args, "args");
125+
this.options = Objects.requireNonNull(options, "options");
102126
}
103127

104128
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ public CommandLine parse(final Options options, final String[] arguments, final
677677
group.setSelected(null);
678678
}
679679

680-
cmd = new CommandLine();
680+
cmd = CommandLine.builder().build();
681681

682682
if (arguments != null) {
683683
for (final String argument : arguments) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public CommandLine parse(final Options options, final String[] arguments, final
156156
// initialize members
157157
setOptions(options);
158158

159-
cmd = new CommandLine();
159+
cmd = CommandLine.builder().build();
160160

161161
boolean eatTheRest = false;
162162

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,20 @@ public void testGetOptionPropertiesWithOption() throws Exception {
113113
}
114114

115115
@Test
116-
public void testGetOptions() {
116+
public void testGetOptionsBuilder() {
117+
final CommandLine cmd = CommandLine.builder().build();
118+
assertNotNull(cmd.getOptions());
119+
assertEquals(0, cmd.getOptions().length);
120+
121+
cmd.addOption(new Option("a", null));
122+
cmd.addOption(new Option("b", null));
123+
cmd.addOption(new Option("c", null));
124+
125+
assertEquals(3, cmd.getOptions().length);
126+
}
127+
128+
@Test
129+
public void testGetOptionsCtor() {
117130
final CommandLine cmd = new CommandLine();
118131
assertNotNull(cmd.getOptions());
119132
assertEquals(0, cmd.getOptions().length);

0 commit comments

Comments
 (0)