001: /**************************************************************************
002: /* LongOpt.java -- Long option object for Getopt
003: /*
004: /* Copyright (c) 1998 by Aaron M. Renn (arenn@urbanophile.com)
005: /*
006: /* This program is free software; you can redistribute it and/or modify
007: /* it under the terms of the GNU Library General Public License as published
008: /* by the Free Software Foundation; either version 2 of the License or
009: /* (at your option) any later version.
010: /*
011: /* This program is distributed in the hope that it will be useful, but
012: /* WITHOUT ANY WARRANTY; without even the implied warranty of
013: /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: /* GNU Library General Public License for more details.
015: /*
016: /* You should have received a copy of the GNU Library General Public License
017: /* along with this program; see the file COPYING.LIB. If not, write to
018: /* the Free Software Foundation Inc., 59 Temple Place - Suite 330,
019: /* Boston, MA 02111-1307 USA
020: /**************************************************************************/package gnu.getopt;
021:
022: import java.util.Locale;
023: import java.util.ResourceBundle;
024: import java.util.PropertyResourceBundle;
025: import java.text.MessageFormat;
026:
027: /**************************************************************************/
028:
029: /**
030: * This object represents the definition of a long option in the Java port
031: * of GNU getopt. An array of LongOpt objects is passed to the Getopt
032: * object to define the list of valid long options for a given parsing
033: * session. Refer to the getopt documentation for details on the
034: * format of long options.
035: *
036: * @version 1.0.5
037: * @author Aaron M. Renn (arenn@urbanophile.com)
038: *
039: * @see Getopt
040: */
041: public class LongOpt extends Object {
042:
043: /**************************************************************************/
044:
045: /*
046: * Class Variables
047: */
048:
049: /**
050: * Constant value used for the "has_arg" constructor argument. This
051: * value indicates that the option takes no argument.
052: */
053: public static final int NO_ARGUMENT = 0;
054:
055: /**
056: * Constant value used for the "has_arg" constructor argument. This
057: * value indicates that the option takes an argument that is required.
058: */
059: public static final int REQUIRED_ARGUMENT = 1;
060:
061: /**
062: * Constant value used for the "has_arg" constructor argument. This
063: * value indicates that the option takes an argument that is optional.
064: */
065: public static final int OPTIONAL_ARGUMENT = 2;
066:
067: /**************************************************************************/
068:
069: /*
070: * Instance Variables
071: */
072:
073: /**
074: * The name of the long option
075: */
076: protected String name;
077:
078: /**
079: * Indicates whether the option has no argument, a required argument, or
080: * an optional argument.
081: */
082: protected int has_arg;
083:
084: /**
085: * If this variable is not null, then the value stored in "val" is stored
086: * here when this long option is encountered. If this is null, the value
087: * stored in "val" is treated as the name of an equivalent short option.
088: */
089: protected StringBuffer flag;
090:
091: /**
092: * The value to store in "flag" if flag is not null, otherwise the
093: * equivalent short option character for this long option.
094: */
095: protected int val;
096:
097: /**
098: * Localized strings for error messages
099: */
100: private ResourceBundle _messages = PropertyResourceBundle
101: .getBundle("gnu/getopt/MessagesBundle", Locale.getDefault());
102:
103: /**************************************************************************/
104:
105: /*
106: * Constructors
107: */
108:
109: /**
110: * Create a new LongOpt object with the given parameter values. If the
111: * value passed as has_arg is not valid, then an exception is thrown.
112: *
113: * @param name The long option String.
114: * @param has_arg Indicates whether the option has no argument (NO_ARGUMENT), a required argument (REQUIRED_ARGUMENT) or an optional argument (OPTIONAL_ARGUMENT).
115: * @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.
116: * @param val The value to return for this long option, or the equivalent single letter option to emulate if flag is null.
117: *
118: * @exception IllegalArgumentException If the has_arg param is not one of NO_ARGUMENT, REQUIRED_ARGUMENT or OPTIONAL_ARGUMENT.
119: */
120: public LongOpt(String name, int has_arg, StringBuffer flag, int val)
121: throws IllegalArgumentException {
122: // Validate has_arg
123: if ((has_arg != NO_ARGUMENT) && (has_arg != REQUIRED_ARGUMENT)
124: && (has_arg != OPTIONAL_ARGUMENT)) {
125: Object[] msgArgs = { new Integer(has_arg).toString() };
126: throw new IllegalArgumentException(MessageFormat
127: .format(_messages.getString("getopt.invalidValue"),
128: msgArgs));
129: }
130:
131: // Store off values
132: this .name = name;
133: this .has_arg = has_arg;
134: this .flag = flag;
135: this .val = val;
136: }
137:
138: /**************************************************************************/
139:
140: /**
141: * Returns the name of this LongOpt as a String
142: *
143: * @return Then name of the long option
144: */
145: public String getName() {
146: return (name);
147: }
148:
149: /**************************************************************************/
150:
151: /**
152: * Returns the value set for the 'has_arg' field for this long option
153: *
154: * @return The value of 'has_arg'
155: */
156: public int getHasArg() {
157: return (has_arg);
158: }
159:
160: /**************************************************************************/
161:
162: /**
163: * Returns the value of the 'flag' field for this long option
164: *
165: * @return The value of 'flag'
166: */
167: public StringBuffer getFlag() {
168: return (flag);
169: }
170:
171: /**
172: * Returns the value of the 'val' field for this long option
173: *
174: * @return The value of 'val'
175: */
176: public int getVal() {
177: return (val);
178: }
179:
180: /**************************************************************************/
181:
182: } // Class LongOpt
|