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
41
42 public class Command
43 extends ParentImpl {
44
45 private final String preferredName;
46
47
48 private final Set aliases;
49
50
51 private final Set triggers;
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 public Command(final String preferredName,
74 final String description,
75 final Set aliases,
76 final boolean required,
77 final Argument argument,
78 final Group children,
79 final int id) {
80 super(argument, children, description, id, required);
81
82
83 if ((preferredName == null) || (preferredName.length() < 1)) {
84 throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.COMMAND_PREFERRED_NAME_TOO_SHORT));
85 }
86
87 this.preferredName = preferredName;
88
89
90 this.aliases =
91 (aliases == null) ? Collections.EMPTY_SET
92 : Collections.unmodifiableSet(new HashSet(aliases));
93
94
95 final Set newTriggers = new HashSet();
96 newTriggers.add(preferredName);
97 newTriggers.addAll(this.aliases);
98 this.triggers = Collections.unmodifiableSet(newTriggers);
99 }
100
101 public void processParent(final WriteableCommandLine commandLine,
102 final ListIterator arguments)
103 throws OptionException {
104
105 final String arg = (String) arguments.next();
106
107
108 if (canProcess(commandLine, arg)) {
109
110 commandLine.addOption(this);
111
112
113 arguments.set(preferredName);
114 } else {
115 throw new OptionException(this, ResourceConstants.UNEXPECTED_TOKEN, arg);
116 }
117 }
118
119 public Set getTriggers() {
120 return triggers;
121 }
122
123 public void validate(WriteableCommandLine commandLine)
124 throws OptionException {
125 if (isRequired() && !commandLine.hasOption(this)) {
126 throw new OptionException(this, ResourceConstants.OPTION_MISSING_REQUIRED,
127 getPreferredName());
128 }
129
130 super.validate(commandLine);
131 }
132
133 public void appendUsage(final StringBuffer buffer,
134 final Set helpSettings,
135 final Comparator comp) {
136
137 final boolean optional =
138 !isRequired() && helpSettings.contains(DisplaySetting.DISPLAY_OPTIONAL);
139 final boolean displayAliases = helpSettings.contains(DisplaySetting.DISPLAY_ALIASES);
140
141 if (optional) {
142 buffer.append('[');
143 }
144
145 buffer.append(preferredName);
146
147 if (displayAliases && !aliases.isEmpty()) {
148 buffer.append(" (");
149
150 final List list = new ArrayList(aliases);
151 Collections.sort(list);
152
153 for (final Iterator i = list.iterator(); i.hasNext();) {
154 final String alias = (String) i.next();
155 buffer.append(alias);
156
157 if (i.hasNext()) {
158 buffer.append(',');
159 }
160 }
161
162 buffer.append(')');
163 }
164
165 super.appendUsage(buffer, helpSettings, comp);
166
167 if (optional) {
168 buffer.append(']');
169 }
170 }
171
172 public String getPreferredName() {
173 return preferredName;
174 }
175 }