KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > getopt > LongOpt


1 /**************************************************************************
2 /* LongOpt.java -- Long option object for Getopt
3 /*
4 /* Copyright (c) 1998 by Aaron M. Renn (arenn@urbanophile.com)
5 /*
6 /* This program is free software; you can redistribute it and/or modify
7 /* it under the terms of the GNU Library General Public License as published
8 /* by the Free Software Foundation; either version 2 of the License or
9 /* (at your option) any later version.
10 /*
11 /* This program is distributed in the hope that it will be useful, but
12 /* WITHOUT ANY WARRANTY; without even the implied warranty of
13 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 /* GNU Library General Public License for more details.
15 /*
16 /* You should have received a copy of the GNU Library General Public License
17 /* along with this program; see the file COPYING.LIB. If not, write to
18 /* the Free Software Foundation Inc., 59 Temple Place - Suite 330,
19 /* Boston, MA 02111-1307 USA
20 /**************************************************************************/

21
22 package gnu.getopt;
23
24 import java.util.Locale JavaDoc;
25 import java.util.ResourceBundle JavaDoc;
26 import java.util.PropertyResourceBundle JavaDoc;
27 import java.text.MessageFormat JavaDoc;
28
29 /**************************************************************************/
30
31 /**
32   * This object represents the definition of a long option in the Java port
33   * of GNU getopt. An array of LongOpt objects is passed to the Getopt
34   * object to define the list of valid long options for a given parsing
35   * session. Refer to the getopt documentation for details on the
36   * format of long options.
37   *
38   * @version 1.0.5
39   * @author Aaron M. Renn (arenn@urbanophile.com)
40   *
41   * @see Getopt
42   */

43 public class LongOpt extends Object JavaDoc
44 {
45
46 /**************************************************************************/
47
48 /*
49  * Class Variables
50  */

51
52 /**
53   * Constant value used for the "has_arg" constructor argument. This
54   * value indicates that the option takes no argument.
55   */

56 public static final int NO_ARGUMENT = 0;
57
58 /**
59   * Constant value used for the "has_arg" constructor argument. This
60   * value indicates that the option takes an argument that is required.
61   */

62 public static final int REQUIRED_ARGUMENT = 1;
63
64 /**
65   * Constant value used for the "has_arg" constructor argument. This
66   * value indicates that the option takes an argument that is optional.
67   */

68 public static final int OPTIONAL_ARGUMENT = 2;
69
70 /**************************************************************************/
71
72 /*
73  * Instance Variables
74  */

75
76 /**
77   * The name of the long option
78   */

79 protected String JavaDoc name;
80
81 /**
82   * Indicates whether the option has no argument, a required argument, or
83   * an optional argument.
84   */

85 protected int has_arg;
86
87 /**
88   * If this variable is not null, then the value stored in "val" is stored
89   * here when this long option is encountered. If this is null, the value
90   * stored in "val" is treated as the name of an equivalent short option.
91   */

92 protected StringBuffer JavaDoc flag;
93
94 /**
95   * The value to store in "flag" if flag is not null, otherwise the
96   * equivalent short option character for this long option.
97   */

98 protected int val;
99
100 /**
101   * Localized strings for error messages
102   */

103 private ResourceBundle JavaDoc _messages = PropertyResourceBundle.getBundle(
104                             "gnu/getopt/MessagesBundle", Locale.getDefault());
105
106 /**************************************************************************/
107
108 /*
109  * Constructors
110  */

111
112 /**
113   * Create a new LongOpt object with the given parameter values. If the
114   * value passed as has_arg is not valid, then an exception is thrown.
115   *
116   * @param name The long option String.
117   * @param has_arg Indicates whether the option has no argument (NO_ARGUMENT), a required argument (REQUIRED_ARGUMENT) or an optional argument (OPTIONAL_ARGUMENT).
118   * @param flag If non-null, this is a location to store the value of "val" when this option is encountered, otherwise "val" is treated as the equivalent short option character.
119   * @param val The value to return for this long option, or the equivalent single letter option to emulate if flag is null.
120   *
121   * @exception IllegalArgumentException If the has_arg param is not one of NO_ARGUMENT, REQUIRED_ARGUMENT or OPTIONAL_ARGUMENT.
122   */

123 public
124 LongOpt(String JavaDoc name, int has_arg,
125         StringBuffer JavaDoc flag, int val) throws IllegalArgumentException JavaDoc
126 {
127   // Validate has_arg
128
if ((has_arg != NO_ARGUMENT) && (has_arg != REQUIRED_ARGUMENT)
129      && (has_arg != OPTIONAL_ARGUMENT))
130     {
131       Object JavaDoc[] msgArgs = { new Integer JavaDoc(has_arg).toString() };
132       throw new IllegalArgumentException JavaDoc(MessageFormat.format(
133                     _messages.getString("getopt.invalidValue"), msgArgs));
134     }
135
136   // Store off values
137
this.name = name;
138   this.has_arg = has_arg;
139   this.flag = flag;
140   this.val = val;
141 }
142
143 /**************************************************************************/
144
145 /**
146   * Returns the name of this LongOpt as a String
147   *
148   * @return Then name of the long option
149   */

150 public String JavaDoc
151 getName()
152 {
153   return(name);
154 }
155
156 /**************************************************************************/
157
158 /**
159   * Returns the value set for the 'has_arg' field for this long option
160   *
161   * @return The value of 'has_arg'
162   */

163 public int
164 getHasArg()
165 {
166   return(has_arg);
167 }
168
169 /**************************************************************************/
170
171 /**
172   * Returns the value of the 'flag' field for this long option
173   *
174   * @return The value of 'flag'
175   */

176 public StringBuffer JavaDoc
177 getFlag()
178 {
179   return(flag);
180 }
181
182 /**
183   * Returns the value of the 'val' field for this long option
184   *
185   * @return The value of 'val'
186   */

187 public int
188 getVal()
189 {
190   return(val);
191 }
192
193 /**************************************************************************/
194
195 } // Class LongOpt
196

197
Popular Tags