001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
014: * implied.
015: *
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019: package org.apache.commons.cli.avalon;
020:
021: // Renamed from org.apache.avalon.excalibur.cli
022:
023: /**
024: * Basic class describing an type of option. Typically, one creates a static
025: * array of <code>CLOptionDescriptor</code>s, and passes it to
026: * {@link CLArgsParser#CLArgsParser(String[], CLOptionDescriptor[])}.
027: *
028: * @see CLArgsParser
029: * @see CLUtil
030: */
031: public final class CLOptionDescriptor {
032: /** Flag to say that one argument is required */
033: public static final int ARGUMENT_REQUIRED = 1 << 1;
034:
035: /** Flag to say that the argument is optional */
036: public static final int ARGUMENT_OPTIONAL = 1 << 2;
037:
038: /** Flag to say this option does not take arguments */
039: public static final int ARGUMENT_DISALLOWED = 1 << 3;
040:
041: /** Flag to say this option requires 2 arguments */
042: public static final int ARGUMENTS_REQUIRED_2 = 1 << 4;
043:
044: /** Flag to say this option may be repeated on the command line */
045: public static final int DUPLICATES_ALLOWED = 1 << 5;
046:
047: private final int m_id;
048:
049: private final int m_flags;
050:
051: private final String m_name;
052:
053: private final String m_description;
054:
055: private final int[] m_incompatible;
056:
057: /**
058: * Constructor.
059: *
060: * @param name
061: * the name/long option
062: * @param flags
063: * the flags
064: * @param id
065: * the id/character option
066: * @param description
067: * description of option usage
068: */
069: public CLOptionDescriptor(final String name, final int flags,
070: final int id, final String description) {
071:
072: checkFlags(flags);
073:
074: m_id = id;
075: m_name = name;
076: m_flags = flags;
077: m_description = description;
078: m_incompatible = ((flags & DUPLICATES_ALLOWED) > 0) ? new int[0]
079: : new int[] { id };
080: }
081:
082: /**
083: * Constructor.
084: *
085: * @param name
086: * the name/long option
087: * @param flags
088: * the flags
089: * @param id
090: * the id/character option
091: * @param description
092: * description of option usage
093: * @param incompatible
094: * descriptors for incompatible options
095: */
096: public CLOptionDescriptor(final String name, final int flags,
097: final int id, final String description,
098: final CLOptionDescriptor[] incompatible) {
099:
100: checkFlags(flags);
101:
102: m_id = id;
103: m_name = name;
104: m_flags = flags;
105: m_description = description;
106:
107: m_incompatible = new int[incompatible.length];
108: for (int i = 0; i < incompatible.length; i++) {
109: m_incompatible[i] = incompatible[i].getId();
110: }
111: }
112:
113: private void checkFlags(final int flags) {
114: int modeCount = 0;
115: if ((ARGUMENT_REQUIRED & flags) == ARGUMENT_REQUIRED) {
116: modeCount++;
117: }
118: if ((ARGUMENT_OPTIONAL & flags) == ARGUMENT_OPTIONAL) {
119: modeCount++;
120: }
121: if ((ARGUMENT_DISALLOWED & flags) == ARGUMENT_DISALLOWED) {
122: modeCount++;
123: }
124: if ((ARGUMENTS_REQUIRED_2 & flags) == ARGUMENTS_REQUIRED_2) {
125: modeCount++;
126: }
127:
128: if (0 == modeCount) {
129: final String message = "No mode specified for option "
130: + this ;
131: throw new IllegalStateException(message);
132: } else if (1 != modeCount) {
133: final String message = "Multiple modes specified for option "
134: + this ;
135: throw new IllegalStateException(message);
136: }
137: }
138:
139: /**
140: * Get the array of incompatible option ids.
141: *
142: * @return the array of incompatible option ids
143: */
144: protected final int[] getIncompatible() {
145: return m_incompatible;
146: }
147:
148: /**
149: * Retrieve textual description.
150: *
151: * @return the description
152: */
153: public final String getDescription() {
154: return m_description;
155: }
156:
157: /**
158: * Retrieve flags about option. Flags include details such as whether it
159: * allows parameters etc.
160: *
161: * @return the flags
162: */
163: public final int getFlags() {
164: return m_flags;
165: }
166:
167: /**
168: * Retrieve the id for option. The id is also the character if using single
169: * character options.
170: *
171: * @return the id
172: */
173: public final int getId() {
174: return m_id;
175: }
176:
177: /**
178: * Retrieve name of option which is also text for long option.
179: *
180: * @return name/long option
181: */
182: public final String getName() {
183: return m_name;
184: }
185:
186: /**
187: * Convert to String.
188: *
189: * @return the converted value to string.
190: */
191: public final String toString() {
192: final StringBuffer sb = new StringBuffer();
193: sb.append("[OptionDescriptor ");
194: sb.append(m_name);
195: sb.append(", ");
196: sb.append(m_id);
197: sb.append(", ");
198: sb.append(m_flags);
199: sb.append(", ");
200: sb.append(m_description);
201: sb.append(" ]");
202: return sb.toString();
203: }
204: }
|