Skip to content

Commit 6d622e2

Browse files
committed
add CommandModel.toBuilder
1 parent dd6a4aa commit 6d622e2

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

jbock/src/main/java/net/jbock/model/CommandModel.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,24 @@ public static final class Builder {
5858
private Builder() {
5959
}
6060

61+
public Builder withOptions(List<Option> newOptions) {
62+
options.clear();
63+
options.addAll(newOptions);
64+
return this;
65+
}
66+
67+
public Builder withParameters(List<Parameter> newParameters) {
68+
parameters.clear();
69+
parameters.addAll(newParameters);
70+
return this;
71+
}
72+
73+
public Builder withDescriptionLines(List<String> newDescriptionLines) {
74+
descriptionLines.clear();
75+
descriptionLines.addAll(newDescriptionLines);
76+
return this;
77+
}
78+
6179
/**
6280
* Sets the description key.
6381
*
@@ -140,6 +158,16 @@ public CommandModel build() {
140158
}
141159
}
142160

161+
public Builder toBuilder() {
162+
return builder()
163+
.withDescriptionKey(descriptionKey)
164+
.withDescriptionLines(descriptionLines)
165+
.withProgramName(programName)
166+
.withSuperCommand(superCommand)
167+
.withOptions(options)
168+
.withParameters(parameters);
169+
}
170+
143171
/**
144172
* Returns the description key from the {@link Command#descriptionKey()} attribute,
145173
* possibly an empty string.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package net.jbock.model;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.List;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
class CommandModelTest {
10+
11+
@Test
12+
void testToBuilder() {
13+
Option option1 = Option.nullary().withParamLabel("v").withNames(List.of("-v")).build();
14+
Option option2 = Option.nullary().withParamLabel("p").withNames(List.of("-p")).build();
15+
CommandModel model1 = CommandModel.builder()
16+
.addOption(option1)
17+
.addOption(option2)
18+
.build();
19+
assertEquals(2, model1.options().size());
20+
CommandModel model2 = model1.toBuilder().withOptions(List.of(option1)).build();
21+
assertEquals(1, model2.options().size());
22+
}
23+
}

0 commit comments

Comments
 (0)