KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > cli > avalon > CLOption


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.commons.cli.avalon;
18 // Renamed from org.apache.avalon.excalibur.cli
19

20 import java.util.Arrays JavaDoc;
21
22 /**
23  * Basic class describing an instance of option.
24  *
25  * @version $Revision: 1.1.2.2 $ $Date: 2005/02/20 15:56:12 $
26  */

27 public final class CLOption
28 {
29     /**
30      * Value of {@link #getId} when the option is a text argument.
31      */

32     public static final int TEXT_ARGUMENT = 0;
33
34     /**
35      * Default descriptor. Required, since code assumes that getDescriptor will never return null.
36      */

37     private static final CLOptionDescriptor TEXT_ARGUMENT_DESCRIPTOR =
38             new CLOptionDescriptor( null, CLOptionDescriptor.ARGUMENT_OPTIONAL, TEXT_ARGUMENT,
39                     null );
40
41     private String JavaDoc[] m_arguments;
42     private CLOptionDescriptor m_descriptor = TEXT_ARGUMENT_DESCRIPTOR;
43
44     /**
45      * Retrieve argument to option if it takes arguments.
46      *
47      * @return the (first) argument
48      */

49     public final String JavaDoc getArgument()
50     {
51         return getArgument( 0 );
52     }
53
54     /**
55      * Retrieve indexed argument to option if it takes arguments.
56      *
57      * @param index The argument index, from 0 to
58      * {@link #getArgumentCount()}-1.
59      * @return the argument
60      */

61     public final String JavaDoc getArgument( final int index )
62     {
63         if( null == m_arguments || index < 0 || index >= m_arguments.length )
64         {
65             return null;
66         }
67         else
68         {
69             return m_arguments[index];
70         }
71     }
72
73     /**
74      * Retrieve id of option.
75      *
76      * The id is eqivalent to character code if it can be a single letter option.
77      *
78      * @return the id
79      * @deprecated use <code>getDescriptor().getId()</code> instead
80      */

81     public final int getId()
82     {
83         return m_descriptor == null ? TEXT_ARGUMENT : m_descriptor.getId();
84     }
85
86     public final CLOptionDescriptor getDescriptor()
87     {
88         return m_descriptor;
89     }
90
91     /**
92      * Constructor taking an descriptor
93      *
94      * @param descriptor the descriptor iff null, will default to a "text argument" descriptor.
95      */

96     public CLOption( final CLOptionDescriptor descriptor )
97     {
98         if( descriptor != null )
99         {
100             m_descriptor = descriptor;
101         }
102     }
103
104     /**
105      * Constructor taking argument for option.
106      *
107      * @param argument the argument
108      */

109     public CLOption( final String JavaDoc argument )
110     {
111         this( (CLOptionDescriptor)null );
112         addArgument( argument );
113     }
114
115     /**
116      * Mutator of Argument property.
117      *
118      * @param argument the argument
119      */

120     public final void addArgument( final String JavaDoc argument )
121     {
122         if( null == m_arguments )
123         {
124             m_arguments = new String JavaDoc[]{argument};
125         }
126         else
127         {
128             final String JavaDoc[] arguments = new String JavaDoc[m_arguments.length + 1];
129             System.arraycopy( m_arguments, 0, arguments, 0, m_arguments.length );
130             arguments[m_arguments.length] = argument;
131             m_arguments = arguments;
132         }
133     }
134
135     /**
136      * Get number of arguments.
137      *
138      * @return the number of arguments
139      */

140     public final int getArgumentCount()
141     {
142         if( null == m_arguments )
143         {
144             return 0;
145         }
146         else
147         {
148             return m_arguments.length;
149         }
150     }
151
152     /**
153      * Convert to String.
154      *
155      * @return the string value
156      */

157     public final String JavaDoc toString()
158     {
159         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
160         sb.append( "[Option " );
161         sb.append( (char)m_descriptor.getId() );
162
163         if( null != m_arguments )
164         {
165             sb.append( ", " );
166             sb.append( Arrays.asList( m_arguments ) );
167         }
168
169         sb.append( " ]" );
170
171         return sb.toString();
172     }
173 }
174
Popular Tags