KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > utils > CLOption


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the docs/licenses/apache-1.1.txt file.
7  */

8 // This file is pulled from package org.apache.avalon.excalibur.cli Excalibur
9
// version 4.1 (Jan 30, 2002). Only the package name has been changed.
10
package org.jboss.axis.utils;
11
12 import java.util.Arrays JavaDoc;
13
14 /**
15  * Basic class describing an instance of option.
16  *
17  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
18  * @since 4.0
19  */

20 public final class CLOption
21 {
22    /**
23     * Value of {@link #getId} when the option is a text argument.
24     */

25    public static final int TEXT_ARGUMENT = 0;
26
27    private final int m_id;
28    private String JavaDoc[] m_arguments;
29
30    /**
31     * Retrieve argument to option if it takes arguments.
32     *
33     * @return the (first) argument
34     */

35    public final String JavaDoc getArgument()
36    {
37       return getArgument(0);
38    }
39
40    /**
41     * Retrieve indexed argument to option if it takes arguments.
42     *
43     * @param index The argument index, from 0 to
44     * {@link #getArgumentCount()}-1.
45     * @return the argument
46     */

47    public final String JavaDoc getArgument(final int index)
48    {
49       if (null == m_arguments || index < 0 || index >= m_arguments.length)
50       {
51          return null;
52       }
53       else
54       {
55          return m_arguments[index];
56       }
57    }
58
59    /**
60     * Retrieve id of option.
61     * <p/>
62     * The id is eqivalent to character code if it can be a single letter option.
63     *
64     * @return the id
65     */

66    public final int getId()
67    {
68       return m_id;
69    }
70
71    /**
72     * Constructor taking an id (that must be a proper character code)
73     *
74     * @param id the new id
75     */

76    public CLOption(final int id)
77    {
78       m_id = id;
79    }
80
81    /**
82     * Constructor taking argument for option.
83     *
84     * @param argument the argument
85     */

86    public CLOption(final String JavaDoc argument)
87    {
88       this(TEXT_ARGUMENT);
89       addArgument(argument);
90    }
91
92    /**
93     * Mutator of Argument property.
94     *
95     * @param argument the argument
96     */

97    public final void addArgument(final String JavaDoc argument)
98    {
99       if (null == m_arguments)
100          m_arguments = new String JavaDoc[]{argument};
101       else
102       {
103          final String JavaDoc[] arguments = new String JavaDoc[m_arguments.length + 1];
104          System.arraycopy(m_arguments, 0, arguments, 0, m_arguments.length);
105          arguments[m_arguments.length] = argument;
106          m_arguments = arguments;
107       }
108    }
109
110    /**
111     * Get number of arguments.
112     */

113    public final int getArgumentCount()
114    {
115       if (null == m_arguments)
116       {
117          return 0;
118       }
119       else
120       {
121          return m_arguments.length;
122       }
123    }
124
125    /**
126     * Convert to String.
127     *
128     * @return the string value
129     */

130    public final String JavaDoc toString()
131    {
132       final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
133       sb.append("[Option ");
134       sb.append((char)m_id);
135
136       if (null != m_arguments)
137       {
138          sb.append(", ");
139          sb.append(Arrays.asList(m_arguments));
140       }
141
142       sb.append(" ]");
143
144       return sb.toString();
145    }
146 }
147
Popular Tags