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