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: import java.util.Arrays;
024:
025: /**
026: * Basic class describing an instance of option.
027: *
028: */
029: public final class CLOption {
030: /**
031: * Value of {@link CLOptionDescriptor#getId} when the option is a text argument.
032: */
033: public static final int TEXT_ARGUMENT = 0;
034:
035: /**
036: * Default descriptor. Required, since code assumes that getDescriptor will
037: * never return null.
038: */
039: private static final CLOptionDescriptor TEXT_ARGUMENT_DESCRIPTOR = new CLOptionDescriptor(
040: null, CLOptionDescriptor.ARGUMENT_OPTIONAL, TEXT_ARGUMENT,
041: null);
042:
043: private String[] m_arguments;
044:
045: private CLOptionDescriptor m_descriptor = TEXT_ARGUMENT_DESCRIPTOR;
046:
047: /**
048: * Retrieve argument to option if it takes arguments.
049: *
050: * @return the (first) argument
051: */
052: public final String getArgument() {
053: return getArgument(0);
054: }
055:
056: /**
057: * Retrieve indexed argument to option if it takes arguments.
058: *
059: * @param index
060: * The argument index, from 0 to {@link #getArgumentCount()}-1.
061: * @return the argument
062: */
063: public final String getArgument(final int index) {
064: if (null == m_arguments || index < 0
065: || index >= m_arguments.length) {
066: return null;
067: } else {
068: return m_arguments[index];
069: }
070: }
071:
072: public final CLOptionDescriptor getDescriptor() {
073: return m_descriptor;
074: }
075:
076: /**
077: * Constructor taking an descriptor
078: *
079: * @param descriptor
080: * the descriptor iff null, will default to a "text argument"
081: * descriptor.
082: */
083: public CLOption(final CLOptionDescriptor descriptor) {
084: if (descriptor != null) {
085: m_descriptor = descriptor;
086: }
087: }
088:
089: /**
090: * Constructor taking argument for option.
091: *
092: * @param argument
093: * the argument
094: */
095: public CLOption(final String argument) {
096: this ((CLOptionDescriptor) null);
097: addArgument(argument);
098: }
099:
100: /**
101: * Mutator of Argument property.
102: *
103: * @param argument
104: * the argument
105: */
106: public final void addArgument(final String argument) {
107: if (null == m_arguments) {
108: m_arguments = new String[] { argument };
109: } else {
110: final String[] arguments = new String[m_arguments.length + 1];
111: System.arraycopy(m_arguments, 0, arguments, 0,
112: m_arguments.length);
113: arguments[m_arguments.length] = argument;
114: m_arguments = arguments;
115: }
116: }
117:
118: /**
119: * Get number of arguments.
120: *
121: * @return the number of arguments
122: */
123: public final int getArgumentCount() {
124: if (null == m_arguments) {
125: return 0;
126: } else {
127: return m_arguments.length;
128: }
129: }
130:
131: /**
132: * Convert to String.
133: *
134: * @return the string value
135: */
136: public final String toString() {
137: final StringBuffer sb = new StringBuffer();
138: sb.append("[");
139: final char id = (char) m_descriptor.getId();
140: if (id == TEXT_ARGUMENT) {
141: sb.append("TEXT ");
142: } else {
143: sb.append("Option ");
144: sb.append(id);
145: }
146:
147: if (null != m_arguments) {
148: sb.append(", ");
149: sb.append(Arrays.asList(m_arguments));
150: }
151:
152: sb.append(" ]");
153:
154: return sb.toString();
155: }
156:
157: /*
158: * Convert to a shorter String for test purposes
159: *
160: * @return the string value
161: */
162: final String toShortString() {
163: final StringBuffer sb = new StringBuffer();
164: final char id = (char) m_descriptor.getId();
165: if (id != TEXT_ARGUMENT) {
166: sb.append("-");
167: sb.append(id);
168: }
169:
170: if (null != m_arguments) {
171: if (id != TEXT_ARGUMENT) {
172: sb.append("=");
173: }
174: sb.append(Arrays.asList(m_arguments));
175: }
176: return sb.toString();
177: }
178: }
|