1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.cli2.option;
18
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.Comparator;
22 import java.util.List;
23 import java.util.ListIterator;
24 import java.util.Set;
25
26 import org.apache.commons.cli2.Argument;
27 import org.apache.commons.cli2.DisplaySetting;
28 import org.apache.commons.cli2.Group;
29 import org.apache.commons.cli2.Option;
30 import org.apache.commons.cli2.OptionException;
31 import org.apache.commons.cli2.Parent;
32 import org.apache.commons.cli2.WriteableCommandLine;
33
34
35
36
37
38 public abstract class ParentImpl
39 extends OptionImpl implements Parent {
40 private static final char NUL = '\0';
41 private final Group children;
42 private final Argument argument;
43 private final String description;
44
45 protected ParentImpl(final Argument argument,
46 final Group children,
47 final String description,
48 final int id,
49 final boolean required) {
50 super(id, required);
51 this.children = children;
52 this.argument = argument;
53 this.description = description;
54 }
55
56
57
58
59
60
61
62 public void process(final WriteableCommandLine commandLine,
63 final ListIterator arguments)
64 throws OptionException {
65 if (argument != null) {
66 handleInitialSeparator(arguments, argument.getInitialSeparator());
67 }
68
69 processParent(commandLine, arguments);
70
71 if (argument != null) {
72 argument.processValues(commandLine, arguments, this);
73 }
74
75 if ((children != null) && children.canProcess(commandLine, arguments)) {
76 children.process(commandLine, arguments);
77 }
78 }
79
80
81
82
83
84
85 public boolean canProcess(final WriteableCommandLine commandLine,
86 final String arg) {
87 final Set triggers = getTriggers();
88
89 if (argument != null) {
90 final char separator = argument.getInitialSeparator();
91
92
93 if (separator != NUL) {
94 final int initialIndex = arg.indexOf(separator);
95
96
97 if (initialIndex > 0) {
98 return triggers.contains(arg.substring(0, initialIndex));
99 }
100 }
101 }
102
103 return triggers.contains(arg);
104 }
105
106
107
108
109
110
111 public Set getPrefixes() {
112 return (children == null) ? Collections.EMPTY_SET : children.getPrefixes();
113 }
114
115
116
117
118
119
120 public void validate(WriteableCommandLine commandLine)
121 throws OptionException {
122 if (commandLine.hasOption(this)) {
123 if (argument != null) {
124 argument.validate(commandLine, this);
125 }
126
127 if (children != null) {
128 children.validate(commandLine);
129 }
130 }
131 }
132
133
134
135
136
137
138
139 public void appendUsage(final StringBuffer buffer,
140 final Set helpSettings,
141 final Comparator comp) {
142 final boolean displayArgument =
143 (this.argument != null) &&
144 helpSettings.contains(DisplaySetting.DISPLAY_PARENT_ARGUMENT);
145 final boolean displayChildren =
146 (this.children != null) &&
147 helpSettings.contains(DisplaySetting.DISPLAY_PARENT_CHILDREN);
148
149 if (displayArgument) {
150 buffer.append(' ');
151 argument.appendUsage(buffer, helpSettings, comp);
152 }
153
154 if (displayChildren) {
155 buffer.append(' ');
156 children.appendUsage(buffer, helpSettings, comp);
157 }
158 }
159
160
161
162
163 public String getDescription() {
164 return description;
165 }
166
167
168
169
170
171
172
173 public List helpLines(final int depth,
174 final Set helpSettings,
175 final Comparator comp) {
176 final List helpLines = new ArrayList();
177 helpLines.add(new HelpLineImpl(this, depth));
178
179 if (helpSettings.contains(DisplaySetting.DISPLAY_PARENT_ARGUMENT) && (argument != null)) {
180 helpLines.addAll(argument.helpLines(depth + 1, helpSettings, comp));
181 }
182
183 if (helpSettings.contains(DisplaySetting.DISPLAY_PARENT_CHILDREN) && (children != null)) {
184 helpLines.addAll(children.helpLines(depth + 1, helpSettings, comp));
185 }
186
187 return helpLines;
188 }
189
190
191
192
193 public Argument getArgument() {
194 return argument;
195 }
196
197
198
199
200 public Group getChildren() {
201 return children;
202 }
203
204
205
206
207
208
209 private void handleInitialSeparator(final ListIterator arguments,
210 final char separator) {
211
212 final String newArgument = (String) arguments.next();
213
214
215 final int initialIndex = newArgument.indexOf(separator);
216
217 if (initialIndex > 0) {
218 arguments.remove();
219 arguments.add(newArgument.substring(0, initialIndex));
220 String value = newArgument.substring(initialIndex + 1);
221
222
223 if (value.startsWith("-")) {
224 value = '"' + value + '"';
225 }
226 arguments.add(value);
227 arguments.previous();
228 }
229
230 arguments.previous();
231 }
232
233
234
235
236 public Option findOption(final String trigger) {
237 final Option found = super.findOption(trigger);
238
239 if ((found == null) && (children != null)) {
240 return children.findOption(trigger);
241 } else {
242 return found;
243 }
244 }
245
246 public void defaults(final WriteableCommandLine commandLine) {
247 super.defaults(commandLine);
248
249 if (argument != null) {
250 argument.defaultValues(commandLine, this);
251 }
252
253 if (children != null) {
254 children.defaults(commandLine);
255 }
256 }
257 }