1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied.
15 *
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19 package org.apache.commons.cli.avalon;
20
21 // Renamed from org.apache.avalon.excalibur.cli
22
23 /**
24 * Basic class describing an type of option. Typically, one creates a static
25 * array of <code>CLOptionDescriptor</code>s, and passes it to
26 * {@link CLArgsParser#CLArgsParser(String[], CLOptionDescriptor[])}.
27 *
28 * @see CLArgsParser
29 * @see CLUtil
30 */
31 public final class CLOptionDescriptor {
32 /** Flag to say that one argument is required */
33 public static final int ARGUMENT_REQUIRED = 1 << 1;
34
35 /** Flag to say that the argument is optional */
36 public static final int ARGUMENT_OPTIONAL = 1 << 2;
37
38 /** Flag to say this option does not take arguments */
39 public static final int ARGUMENT_DISALLOWED = 1 << 3;
40
41 /** Flag to say this option requires 2 arguments */
42 public static final int ARGUMENTS_REQUIRED_2 = 1 << 4;
43
44 /** Flag to say this option may be repeated on the command line */
45 public static final int DUPLICATES_ALLOWED = 1 << 5;
46
47 private final int m_id;
48
49 private final int m_flags;
50
51 private final String m_name;
52
53 private final String m_description;
54
55 private final int[] m_incompatible;
56
57 /**
58 * Constructor.
59 *
60 * @param name
61 * the name/long option
62 * @param flags
63 * the flags
64 * @param id
65 * the id/character option
66 * @param description
67 * description of option usage
68 */
69 public CLOptionDescriptor(final String name, final int flags, final int id, final String description) {
70
71 checkFlags(flags);
72
73 m_id = id;
74 m_name = name;
75 m_flags = flags;
76 m_description = description;
77 m_incompatible = ((flags & DUPLICATES_ALLOWED) > 0) ? new int[0] : new int[] { id };
78 }
79
80
81 /**
82 * Constructor.
83 *
84 * @param name
85 * the name/long option
86 * @param flags
87 * the flags
88 * @param id
89 * the id/character option
90 * @param description
91 * description of option usage
92 * @param incompatible
93 * descriptors for incompatible options
94 */
95 public CLOptionDescriptor(final String name, final int flags, final int id, final String description,
96 final CLOptionDescriptor[] incompatible) {
97
98 checkFlags(flags);
99
100 m_id = id;
101 m_name = name;
102 m_flags = flags;
103 m_description = description;
104
105 m_incompatible = new int[incompatible.length];
106 for (int i = 0; i < incompatible.length; i++) {
107 m_incompatible[i] = incompatible[i].getId();
108 }
109 }
110
111 private void checkFlags(final int flags) {
112 int modeCount = 0;
113 if ((ARGUMENT_REQUIRED & flags) == ARGUMENT_REQUIRED) {
114 modeCount++;
115 }
116 if ((ARGUMENT_OPTIONAL & flags) == ARGUMENT_OPTIONAL) {
117 modeCount++;
118 }
119 if ((ARGUMENT_DISALLOWED & flags) == ARGUMENT_DISALLOWED) {
120 modeCount++;
121 }
122 if ((ARGUMENTS_REQUIRED_2 & flags) == ARGUMENTS_REQUIRED_2) {
123 modeCount++;
124 }
125
126 if (0 == modeCount) {
127 final String message = "No mode specified for option " + this;
128 throw new IllegalStateException(message);
129 } else if (1 != modeCount) {
130 final String message = "Multiple modes specified for option " + this;
131 throw new IllegalStateException(message);
132 }
133 }
134
135 /**
136 * Get the array of incompatible option ids.
137 *
138 * @return the array of incompatible option ids
139 */
140 protected final int[] getIncompatible() {
141 return m_incompatible;
142 }
143
144 /**
145 * Retrieve textual description.
146 *
147 * @return the description
148 */
149 public final String getDescription() {
150 return m_description;
151 }
152
153 /**
154 * Retrieve flags about option. Flags include details such as whether it
155 * allows parameters etc.
156 *
157 * @return the flags
158 */
159 public final int getFlags() {
160 return m_flags;
161 }
162
163 /**
164 * Retrieve the id for option. The id is also the character if using single
165 * character options.
166 *
167 * @return the id
168 */
169 public final int getId() {
170 return m_id;
171 }
172
173 /**
174 * Retrieve name of option which is also text for long option.
175 *
176 * @return name/long option
177 */
178 public final String getName() {
179 return m_name;
180 }
181
182 /**
183 * Convert to String.
184 *
185 * @return the converted value to string.
186 */
187 public final String toString() {
188 final StringBuffer sb = new StringBuffer();
189 sb.append("[OptionDescriptor ");
190 sb.append(m_name);
191 sb.append(", ");
192 sb.append(m_id);
193 sb.append(", ");
194 sb.append(m_flags);
195 sb.append(", ");
196 sb.append(m_description);
197 sb.append(" ]");
198 return sb.toString();
199 }
200 }