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.HashSet; |
23 | |
import java.util.Iterator; |
24 | |
import java.util.List; |
25 | |
import java.util.ListIterator; |
26 | |
import java.util.Set; |
27 | |
|
28 | |
import org.apache.commons.cli2.Argument; |
29 | |
import org.apache.commons.cli2.DisplaySetting; |
30 | |
import org.apache.commons.cli2.Group; |
31 | |
import org.apache.commons.cli2.OptionException; |
32 | |
import org.apache.commons.cli2.WriteableCommandLine; |
33 | |
import org.apache.commons.cli2.resource.ResourceConstants; |
34 | |
import org.apache.commons.cli2.resource.ResourceHelper; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public class Switch |
41 | |
extends ParentImpl { |
42 | |
|
43 | 1 | public static final ResourceHelper resources = ResourceHelper.getResourceHelper(); |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
public static final String DEFAULT_ENABLED_PREFIX = "+"; |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
public static final String DEFAULT_DISABLED_PREFIX = "-"; |
54 | |
private final String enabledPrefix; |
55 | |
private final String disabledPrefix; |
56 | |
private final Set triggers; |
57 | |
private final String preferredName; |
58 | |
private final Set aliases; |
59 | |
private final Set prefixes; |
60 | |
private final Boolean defaultSwitch; |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
public Switch(final String enabledPrefix, |
77 | |
final String disabledPrefix, |
78 | |
final String preferredName, |
79 | |
final Set aliases, |
80 | |
final String description, |
81 | |
final boolean required, |
82 | |
final Argument argument, |
83 | |
final Group children, |
84 | |
final int id, |
85 | |
final Boolean switchDefault) { |
86 | 39 | super(argument, children, description, id, required); |
87 | |
|
88 | 39 | if (enabledPrefix == null) { |
89 | 1 | throw new IllegalArgumentException(resources.getMessage(ResourceConstants.SWITCH_NO_ENABLED_PREFIX)); |
90 | |
} |
91 | |
|
92 | 38 | if (disabledPrefix == null) { |
93 | 1 | throw new IllegalArgumentException(resources.getMessage(ResourceConstants.SWITCH_NO_DISABLED_PREFIX)); |
94 | |
} |
95 | |
|
96 | 37 | if (enabledPrefix.startsWith(disabledPrefix)) { |
97 | 1 | throw new IllegalArgumentException(resources.getMessage(ResourceConstants.SWITCH_ENABLED_STARTS_WITH_DISABLED)); |
98 | |
} |
99 | |
|
100 | 36 | if (disabledPrefix.startsWith(enabledPrefix)) { |
101 | 1 | throw new IllegalArgumentException(resources.getMessage(ResourceConstants.SWITCH_DISABLED_STARTWS_WITH_ENABLED)); |
102 | |
} |
103 | |
|
104 | 35 | this.enabledPrefix = enabledPrefix; |
105 | 35 | this.disabledPrefix = disabledPrefix; |
106 | 35 | this.preferredName = preferredName; |
107 | |
|
108 | 35 | if ((preferredName == null) || (preferredName.length() < 1)) { |
109 | 2 | throw new IllegalArgumentException(resources.getMessage(ResourceConstants.SWITCH_PREFERRED_NAME_TOO_SHORT)); |
110 | |
} |
111 | |
|
112 | 33 | final Set newTriggers = new HashSet(); |
113 | 33 | newTriggers.add(enabledPrefix + preferredName); |
114 | 33 | newTriggers.add(disabledPrefix + preferredName); |
115 | 33 | this.triggers = Collections.unmodifiableSet(newTriggers); |
116 | |
|
117 | 33 | if (aliases == null) { |
118 | 1 | this.aliases = Collections.EMPTY_SET; |
119 | |
} else { |
120 | 32 | this.aliases = Collections.unmodifiableSet(new HashSet(aliases)); |
121 | |
|
122 | 32 | for (final Iterator i = aliases.iterator(); i.hasNext();) { |
123 | 50 | final String alias = (String) i.next(); |
124 | 50 | newTriggers.add(enabledPrefix + alias); |
125 | 50 | newTriggers.add(disabledPrefix + alias); |
126 | 50 | } |
127 | |
} |
128 | |
|
129 | 33 | final Set newPrefixes = new HashSet(super.getPrefixes()); |
130 | 33 | newPrefixes.add(enabledPrefix); |
131 | 33 | newPrefixes.add(disabledPrefix); |
132 | 33 | this.prefixes = Collections.unmodifiableSet(newPrefixes); |
133 | |
|
134 | 33 | this.defaultSwitch = switchDefault; |
135 | |
|
136 | 33 | checkPrefixes(newPrefixes); |
137 | 33 | } |
138 | |
|
139 | |
public void processParent(final WriteableCommandLine commandLine, |
140 | |
final ListIterator arguments) |
141 | |
throws OptionException { |
142 | 10 | final String arg = (String) arguments.next(); |
143 | |
|
144 | 10 | if (canProcess(commandLine, arg)) { |
145 | 10 | if (arg.startsWith(enabledPrefix)) { |
146 | 9 | commandLine.addSwitch(this, true); |
147 | 9 | arguments.set(enabledPrefix + preferredName); |
148 | |
} |
149 | |
|
150 | 10 | if (arg.startsWith(disabledPrefix)) { |
151 | 1 | commandLine.addSwitch(this, false); |
152 | 1 | arguments.set(disabledPrefix + preferredName); |
153 | |
} |
154 | |
} else { |
155 | 0 | throw new OptionException(this, ResourceConstants.UNEXPECTED_TOKEN, arg); |
156 | |
} |
157 | 10 | } |
158 | |
|
159 | |
public Set getTriggers() { |
160 | 243 | return triggers; |
161 | |
} |
162 | |
|
163 | |
public Set getPrefixes() { |
164 | 160 | return prefixes; |
165 | |
} |
166 | |
|
167 | |
public void validate(WriteableCommandLine commandLine) |
168 | |
throws OptionException { |
169 | 9 | if (isRequired() && !commandLine.hasOption(this)) { |
170 | 1 | throw new OptionException(this, ResourceConstants.OPTION_MISSING_REQUIRED, |
171 | |
getPreferredName()); |
172 | |
} |
173 | |
|
174 | 8 | super.validate(commandLine); |
175 | 8 | } |
176 | |
|
177 | |
public void appendUsage(final StringBuffer buffer, |
178 | |
final Set helpSettings, |
179 | |
final Comparator comp) { |
180 | |
|
181 | 6 | final boolean optional = |
182 | |
!isRequired() && helpSettings.contains(DisplaySetting.DISPLAY_OPTIONAL); |
183 | 6 | final boolean displayAliases = helpSettings.contains(DisplaySetting.DISPLAY_ALIASES); |
184 | 6 | final boolean disabled = helpSettings.contains(DisplaySetting.DISPLAY_SWITCH_DISABLED); |
185 | 6 | final boolean enabled = |
186 | |
!disabled || helpSettings.contains(DisplaySetting.DISPLAY_SWITCH_ENABLED); |
187 | 6 | final boolean both = disabled && enabled; |
188 | |
|
189 | 6 | if (optional) { |
190 | 0 | buffer.append('['); |
191 | |
} |
192 | |
|
193 | 6 | if (enabled) { |
194 | 5 | buffer.append(enabledPrefix).append(preferredName); |
195 | |
} |
196 | |
|
197 | 6 | if (both) { |
198 | 2 | buffer.append('|'); |
199 | |
} |
200 | |
|
201 | 6 | if (disabled) { |
202 | 3 | buffer.append(disabledPrefix).append(preferredName); |
203 | |
} |
204 | |
|
205 | 6 | if (displayAliases && !aliases.isEmpty()) { |
206 | 4 | buffer.append(" ("); |
207 | |
|
208 | 4 | final List list = new ArrayList(aliases); |
209 | 4 | Collections.sort(list); |
210 | |
|
211 | 4 | for (final Iterator i = list.iterator(); i.hasNext();) { |
212 | 8 | final String alias = (String) i.next(); |
213 | |
|
214 | 8 | if (enabled) { |
215 | 6 | buffer.append(enabledPrefix).append(alias); |
216 | |
} |
217 | |
|
218 | 8 | if (both) { |
219 | 2 | buffer.append('|'); |
220 | |
} |
221 | |
|
222 | 8 | if (disabled) { |
223 | 4 | buffer.append(disabledPrefix).append(alias); |
224 | |
} |
225 | |
|
226 | 8 | if (i.hasNext()) { |
227 | 4 | buffer.append(','); |
228 | |
} |
229 | 8 | } |
230 | |
|
231 | 4 | buffer.append(')'); |
232 | |
} |
233 | |
|
234 | 6 | super.appendUsage(buffer, helpSettings, comp); |
235 | |
|
236 | 6 | if (optional) { |
237 | 0 | buffer.append(']'); |
238 | |
} |
239 | 6 | } |
240 | |
|
241 | |
public String getPreferredName() { |
242 | 245 | return enabledPrefix + preferredName; |
243 | |
} |
244 | |
|
245 | |
public void defaults(final WriteableCommandLine commandLine) { |
246 | 15 | commandLine.setDefaultSwitch(this, defaultSwitch); |
247 | 15 | } |
248 | |
} |