1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.cli2.builder;
18
19 import java.util.HashSet;
20 import java.util.Set;
21
22 import org.apache.commons.cli2.Argument;
23 import org.apache.commons.cli2.Group;
24 import org.apache.commons.cli2.option.DefaultOption;
25 import org.apache.commons.cli2.resource.ResourceConstants;
26 import org.apache.commons.cli2.resource.ResourceHelper;
27
28
29
30
31 public class DefaultOptionBuilder {
32 private final String shortPrefix;
33 private final String longPrefix;
34 private final boolean burstEnabled;
35 private String preferredName;
36 private Set aliases;
37 private Set burstAliases;
38 private boolean required;
39 private String description;
40 private Argument argument;
41 private Group children;
42 private int id;
43
44
45
46
47
48
49
50 public DefaultOptionBuilder() {
51 this(DefaultOption.DEFAULT_SHORT_PREFIX, DefaultOption.DEFAULT_LONG_PREFIX,
52 DefaultOption.DEFAULT_BURST_ENABLED);
53 }
54
55
56
57
58
59
60
61
62
63 public DefaultOptionBuilder(final String shortPrefix,
64 final String longPrefix,
65 final boolean burstEnabled)
66 throws IllegalArgumentException {
67 if ((shortPrefix == null) || (shortPrefix.length() == 0)) {
68 throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.OPTION_ILLEGAL_SHORT_PREFIX));
69 }
70
71 if ((longPrefix == null) || (longPrefix.length() == 0)) {
72 throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.OPTION_ILLEGAL_LONG_PREFIX));
73 }
74
75 this.shortPrefix = shortPrefix;
76 this.longPrefix = longPrefix;
77 this.burstEnabled = burstEnabled;
78 reset();
79 }
80
81
82
83
84
85
86 public DefaultOption create()
87 throws IllegalStateException {
88 if (preferredName == null) {
89 throw new IllegalStateException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.OPTION_NO_NAME));
90 }
91
92 final DefaultOption option =
93 new DefaultOption(shortPrefix, longPrefix, burstEnabled, preferredName, description,
94 aliases, burstAliases, required, argument, children, id);
95
96 reset();
97
98 return option;
99 }
100
101
102
103
104
105 public DefaultOptionBuilder reset() {
106 preferredName = null;
107 description = null;
108 aliases = new HashSet();
109 burstAliases = new HashSet();
110 required = false;
111 argument = null;
112 children = null;
113 id = 0;
114
115 return this;
116 }
117
118
119
120
121
122
123
124
125 public DefaultOptionBuilder withShortName(final String shortName) {
126 final String name = shortPrefix + shortName;
127
128 if (preferredName == null) {
129 preferredName = name;
130 } else {
131 aliases.add(name);
132 }
133
134 if (burstEnabled && (name.length() == (shortPrefix.length() + 1))) {
135 burstAliases.add(name);
136 }
137
138 return this;
139 }
140
141
142
143
144
145
146
147
148 public DefaultOptionBuilder withLongName(final String longName) {
149 final String name = longPrefix + longName;
150
151 if (preferredName == null) {
152 preferredName = name;
153 } else {
154 aliases.add(name);
155 }
156
157 return this;
158 }
159
160
161
162
163
164
165 public DefaultOptionBuilder withDescription(final String newDescription) {
166 this.description = newDescription;
167
168 return this;
169 }
170
171
172
173
174
175
176 public DefaultOptionBuilder withRequired(final boolean newRequired) {
177 this.required = newRequired;
178
179 return this;
180 }
181
182
183
184
185
186
187 public DefaultOptionBuilder withChildren(final Group newChildren) {
188 this.children = newChildren;
189
190 return this;
191 }
192
193
194
195
196
197
198 public DefaultOptionBuilder withArgument(final Argument newArgument) {
199 this.argument = newArgument;
200
201 return this;
202 }
203
204
205
206
207
208
209
210
211 public final DefaultOptionBuilder withId(final int newId) {
212 this.id = newId;
213
214 return this;
215 }
216 }