[5279] | 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;
|
---|
| 25 | import java.util.ResourceBundle;
|
---|
| 26 | import java.text.MessageFormat;
|
---|
| 27 |
|
---|
| 28 | /**************************************************************************/
|
---|
| 29 |
|
---|
| 30 | /**
|
---|
| 31 | * This object represents the definition of a long option in the Java port
|
---|
| 32 | * of GNU getopt. An array of LongOpt objects is passed to the Getopt
|
---|
| 33 | * object to define the list of valid long options for a given parsing
|
---|
| 34 | * session. Refer to the getopt documentation for details on the
|
---|
| 35 | * format of long options.
|
---|
| 36 | *
|
---|
| 37 | * @version 1.0.5
|
---|
| 38 | * @author Aaron M. Renn (arenn@urbanophile.com)
|
---|
| 39 | *
|
---|
| 40 | * @see Getopt
|
---|
| 41 | */
|
---|
| 42 | public class LongOpt extends Object
|
---|
| 43 | {
|
---|
| 44 |
|
---|
| 45 | /**************************************************************************/
|
---|
| 46 |
|
---|
| 47 | /*
|
---|
| 48 | * Class Variables
|
---|
| 49 | */
|
---|
| 50 |
|
---|
| 51 | /**
|
---|
| 52 | * Constant value used for the "has_arg" constructor argument. This
|
---|
| 53 | * value indicates that the option takes no argument.
|
---|
| 54 | */
|
---|
| 55 | public static final int NO_ARGUMENT = 0;
|
---|
| 56 |
|
---|
| 57 | /**
|
---|
| 58 | * Constant value used for the "has_arg" constructor argument. This
|
---|
| 59 | * value indicates that the option takes an argument that is required.
|
---|
| 60 | */
|
---|
| 61 | public static final int REQUIRED_ARGUMENT = 1;
|
---|
| 62 |
|
---|
| 63 | /**
|
---|
| 64 | * Constant value used for the "has_arg" constructor argument. This
|
---|
| 65 | * value indicates that the option takes an argument that is optional.
|
---|
| 66 | */
|
---|
| 67 | public static final int OPTIONAL_ARGUMENT = 2;
|
---|
| 68 |
|
---|
| 69 | /**************************************************************************/
|
---|
| 70 |
|
---|
| 71 | /*
|
---|
| 72 | * Instance Variables
|
---|
| 73 | */
|
---|
| 74 |
|
---|
| 75 | /**
|
---|
| 76 | * The name of the long option
|
---|
| 77 | */
|
---|
| 78 | protected String name;
|
---|
| 79 |
|
---|
| 80 | /**
|
---|
| 81 | * Indicates whether the option has no argument, a required argument, or
|
---|
| 82 | * an optional argument.
|
---|
| 83 | */
|
---|
| 84 | protected int has_arg;
|
---|
| 85 |
|
---|
| 86 | /**
|
---|
| 87 | * If this variable is not null, then the value stored in "val" is stored
|
---|
| 88 | * here when this long option is encountered. If this is null, the value
|
---|
| 89 | * stored in "val" is treated as the name of an equivalent short option.
|
---|
| 90 | */
|
---|
| 91 | protected StringBuffer flag;
|
---|
| 92 |
|
---|
| 93 | /**
|
---|
| 94 | * The value to store in "flag" if flag is not null, otherwise the
|
---|
| 95 | * equivalent short option character for this long option.
|
---|
| 96 | */
|
---|
| 97 | protected int val;
|
---|
| 98 |
|
---|
| 99 | /**
|
---|
| 100 | * Localized strings for error messages
|
---|
| 101 | */
|
---|
| 102 | private Getopt.OptI18n _messages = new Getopt.OptI18n(); // ResourceBundle.getBundle("gnu/getopt/MessagesBundle", Locale.getDefault());
|
---|
| 103 |
|
---|
| 104 | /**************************************************************************/
|
---|
| 105 |
|
---|
| 106 | /*
|
---|
| 107 | * Constructors
|
---|
| 108 | */
|
---|
| 109 |
|
---|
| 110 | /**
|
---|
| 111 | * Create a new LongOpt object with the given parameter values. If the
|
---|
| 112 | * value passed as has_arg is not valid, then an exception is thrown.
|
---|
| 113 | *
|
---|
| 114 | * @param name The long option String.
|
---|
| 115 | * @param has_arg Indicates whether the option has no argument (NO_ARGUMENT), a required argument (REQUIRED_ARGUMENT) or an optional argument (OPTIONAL_ARGUMENT).
|
---|
| 116 | * @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.
|
---|
| 117 | * @param val The value to return for this long option, or the equivalent single letter option to emulate if flag is null.
|
---|
| 118 | *
|
---|
| 119 | * @exception IllegalArgumentException If the has_arg param is not one of NO_ARGUMENT, REQUIRED_ARGUMENT or OPTIONAL_ARGUMENT.
|
---|
| 120 | */
|
---|
| 121 | public
|
---|
| 122 | LongOpt(String name, int has_arg,
|
---|
| 123 | StringBuffer flag, int val) throws IllegalArgumentException
|
---|
| 124 | {
|
---|
| 125 | // Validate has_arg
|
---|
| 126 | if ((has_arg != NO_ARGUMENT) && (has_arg != REQUIRED_ARGUMENT)
|
---|
| 127 | && (has_arg != OPTIONAL_ARGUMENT))
|
---|
| 128 | {
|
---|
| 129 | Object[] msgArgs = { new Integer(has_arg).toString() };
|
---|
| 130 | throw new IllegalArgumentException(MessageFormat.format(
|
---|
| 131 | _messages.getString("getopt.invalidValue"), msgArgs));
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | // Store off values
|
---|
| 135 | this.name = name;
|
---|
| 136 | this.has_arg = has_arg;
|
---|
| 137 | this.flag = flag;
|
---|
| 138 | this.val = val;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | /**************************************************************************/
|
---|
| 142 |
|
---|
| 143 | /**
|
---|
| 144 | * Returns the name of this LongOpt as a String
|
---|
| 145 | *
|
---|
| 146 | * @return Then name of the long option
|
---|
| 147 | */
|
---|
| 148 | public String
|
---|
| 149 | getName()
|
---|
| 150 | {
|
---|
| 151 | return(name);
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | /**************************************************************************/
|
---|
| 155 |
|
---|
| 156 | /**
|
---|
| 157 | * Returns the value set for the 'has_arg' field for this long option
|
---|
| 158 | *
|
---|
| 159 | * @return The value of 'has_arg'
|
---|
| 160 | */
|
---|
| 161 | public int
|
---|
| 162 | getHasArg()
|
---|
| 163 | {
|
---|
| 164 | return(has_arg);
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | /**************************************************************************/
|
---|
| 168 |
|
---|
| 169 | /**
|
---|
| 170 | * Returns the value of the 'flag' field for this long option
|
---|
| 171 | *
|
---|
| 172 | * @return The value of 'flag'
|
---|
| 173 | */
|
---|
| 174 | public StringBuffer
|
---|
| 175 | getFlag()
|
---|
| 176 | {
|
---|
| 177 | return(flag);
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | /**
|
---|
| 181 | * Returns the value of the 'val' field for this long option
|
---|
| 182 | *
|
---|
| 183 | * @return The value of 'val'
|
---|
| 184 | */
|
---|
| 185 | public int
|
---|
| 186 | getVal()
|
---|
| 187 | {
|
---|
| 188 | return(val);
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | /**************************************************************************/
|
---|
| 192 |
|
---|
| 193 | } // Class LongOpt
|
---|
| 194 |
|
---|