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