Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: gnu/getopt/Getopt.java


1   /**************************************************************************
2   /* Getopt.java -- Java port of GNU getopt from glibc 2.0.6
3   /*
4   /* Copyright (c) 1987-1997 Free Software Foundation, Inc.
5   /* Java Port Copyright (c) 1998 by Aaron M. Renn (arenn@urbanophile.com)
6   /*
7   /* This program is free software; you can redistribute it and/or modify
8   /* it under the terms of the GNU Library General Public License as published 
9   /* by  the Free Software Foundation; either version 2 of the License or
10  /* (at your option) any later version.
11  /*
12  /* This program is distributed in the hope that it will be useful, but
13  /* WITHOUT ANY WARRANTY; without even the implied warranty of
14  /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  /* GNU Library General Public License for more details.
16  /*
17  /* You should have received a copy of the GNU Library General Public License
18  /* along with this program; see the file COPYING.LIB.  If not, write to 
19  /* the Free Software Foundation Inc., 59 Temple Place - Suite 330, 
20  /* Boston, MA  02111-1307 USA
21  /**************************************************************************/
22  
23  package gnu.getopt;
24  
25  import java.util.Locale;
26  import java.util.ResourceBundle;
27  import java.util.PropertyResourceBundle;
28  import java.text.MessageFormat;
29  
30  /**************************************************************************/
31  
32  /**
33    * This is a Java port of GNU getopt, a class for parsing command line
34    * arguments passed to programs.  It it based on the C getopt() functions
35    * in glibc 2.0.6 and should parse options in a 100% compatible manner.
36    * If it does not, that is a bug.  The programmer's interface is also
37    * very compatible.
38    * <p>
39    * To use Getopt, create a Getopt object with a argv array passed to the
40    * main method, then call the getopt() method in a loop.  It will return an
41    * int that contains the value of the option character parsed from the
42    * command line.  When there are no more options to be parsed, it
43    * returns -1.
44    * <p>
45    * A command line option can be defined to take an argument.  If an
46    * option has an argument, the value of that argument is stored in an
47    * instance variable called optarg, which can be accessed using the
48    * getOptarg() method.  If an option that requires an argument is
49    * found, but there is no argument present, then an error message is
50    * printed. Normally getopt() returns a '?' in this situation, but
51    * that can be changed as described below.
52    * <p>
53    * If an invalid option is encountered, an error message is printed
54    * to the standard error and getopt() returns a '?'.  The value of the
55    * invalid option encountered is stored in the instance variable optopt
56    * which can be retrieved using the getOptopt() method.  To suppress
57    * the printing of error messages for this or any other error, set
58    * the value of the opterr instance variable to false using the 
59    * setOpterr() method.
60    * <p>
61    * Between calls to getopt(), the instance variable optind is used to
62    * keep track of where the object is in the parsing process.  After all
63    * options have been returned, optind is the index in argv of the first
64    * non-option argument.  This variable can be accessed with the getOptind()
65    * method.
66    * <p>
67    * Note that this object expects command line options to be passed in the
68    * traditional Unix manner.  That is, proceeded by a '-' character. 
69    * Multiple options can follow the '-'.  For example "-abc" is equivalent
70    * to "-a -b -c".  If an option takes a required argument, the value
71    * of the argument can immediately follow the option character or be
72    * present in the next argv element.  For example, "-cfoo" and "-c foo"
73    * both represent an option character of 'c' with an argument of "foo"
74    * assuming c takes a required argument.  If an option takes an argument
75    * that is not required, then any argument must immediately follow the
76    * option character in the same argv element.  For example, if c takes
77    * a non-required argument, then "-cfoo" represents option character 'c'
78    * with an argument of "foo" while "-c foo" represents the option
79    * character 'c' with no argument, and a first non-option argv element
80    * of "foo".
81    * <p>
82    * The user can stop getopt() from scanning any further into a command line
83    * by using the special argument "--" by itself.  For example: 
84    * "-a -- -d" would return an option character of 'a', then return -1
85    * The "--" is discarded and "-d" is pointed to by optind as the first
86    * non-option argv element.
87    * <p>
88    * Here is a basic example of using Getopt:
89    * <p>
90    * <pre>
91    * Getopt g = new Getopt("testprog", argv, "ab:c::d");
92    * //
93    * int c;
94    * String arg;
95    * while ((c = g.getopt()) != -1)
96    *   {
97    *     switch(c)
98    *       {
99    *          case 'a':
100   *          case 'd':
101   *            System.out.print("You picked " + (char)c + "\n");
102   *            break;
103   *            //
104   *          case 'b':
105   *          case 'c':
106   *            arg = g.getOptarg();
107   *            System.out.print("You picked " + (char)c + 
108   *                             " with an argument of " +
109   *                             ((arg != null) ? arg : "null") + "\n");
110   *            break;
111   *            //
112   *          case '?':
113   *            break; // getopt() already printed an error
114   *            //
115   *          default:
116   *            System.out.print("getopt() returned " + c + "\n");
117   *       }
118   *   }
119   * </pre>
120   * <p>
121   * In this example, a new Getopt object is created with three params.
122   * The first param is the program name.  This is for printing error
123   * messages in the form "program: error message".  In the C version, this
124   * value is taken from argv[0], but in Java the program name is not passed
125   * in that element, thus the need for this parameter.  The second param is
126   * the argument list that was passed to the main() method.  The third
127   * param is the list of valid options.  Each character represents a valid
128   * option.  If the character is followed by a single colon, then that
129   * option has a required argument.  If the character is followed by two
130   * colons, then that option has an argument that is not required.
131   * <p>
132   * Note in this example that the value returned from getopt() is cast to
133   * a char prior to printing.  This is required in order to make the value
134   * display correctly as a character instead of an integer.
135   * <p>
136   * If the first character in the option string is a colon, for example
137   * ":abc::d", then getopt() will return a ':' instead of a '?' when it
138   * encounters an option with a missing required argument.  This allows the
139   * caller to distinguish between invalid options and valid options that
140   * are simply incomplete.
141   * <p>
142   * In the traditional Unix getopt(), -1 is returned when the first non-option
143   * charcter is encountered.  In GNU getopt(), the default behavior is to
144   * allow options to appear anywhere on the command line.  The getopt()
145   * method permutes the argument to make it appear to the caller that all
146   * options were at the beginning of the command line, and all non-options
147   * were at the end.  For example, calling getopt() with command line args
148   * of "-a foo bar -d" returns options 'a' and 'd', then sets optind to 
149   * point to "foo".  The program would read the last two argv elements as
150   * "foo" and "bar", just as if the user had typed "-a -d foo bar". 
151   * <p> 
152   * The user can force getopt() to stop scanning the command line with
153   * the special argument "--" by itself.  Any elements occuring before the
154   * "--" are scanned and permuted as normal.  Any elements after the "--"
155   * are returned as is as non-option argv elements.  For example, 
156   * "foo -a -- bar -d" would return  option 'a' then -1.  optind would point 
157   * to "foo", "bar" and "-d" as the non-option argv elements.  The "--"
158   * is discarded by getopt().
159   * <p>
160   * There are two ways this default behavior can be modified.  The first is
161   * to specify traditional Unix getopt() behavior (which is also POSIX
162   * behavior) in which scanning stops when the first non-option argument
163   * encountered.  (Thus "-a foo bar -d" would return 'a' as an option and
164   * have "foo", "bar", and "-d" as non-option elements).  The second is to
165   * allow options anywhere, but to return all elements in the order they
166   * occur on the command line.  When a non-option element is ecountered,
167   * an integer 1 is returned and the value of the non-option element is
168   * stored in optarg is if it were the argument to that option.  For
169   * example, "-a foo -d", returns first 'a', then 1 (with optarg set to
170   * "foo") then 'd' then -1.  When this "return in order" functionality
171   * is enabled, the only way to stop getopt() from scanning all command
172   * line elements is to use the special "--" string by itself as described
173   * above.  An example is "-a foo -b -- bar", which would return 'a', then
174   * integer 1 with optarg set to "foo", then 'b', then -1.  optind would
175   * then point to "bar" as the first non-option argv element.  The "--"
176   * is discarded.
177   * <p>
178   * The POSIX/traditional behavior is enabled by either setting the 
179   * property "gnu.posixly_correct" or by putting a '+' sign as the first
180   * character of the option string.  The difference between the two 
181   * methods is that setting the gnu.posixly_correct property also forces
182   * certain error messages to be displayed in POSIX format.  To enable
183   * the "return in order" functionality, put a '-' as the first character
184   * of the option string.  Note that after determining the proper 
185   * behavior, Getopt strips this leading '+' or '-', meaning that a ':'
186   * placed as the second character after one of those two will still cause
187   * getopt() to return a ':' instead of a '?' if a required option
188   * argument is missing.
189   * <p>
190   * In addition to traditional single character options, GNU Getopt also
191   * supports long options.  These are preceeded by a "--" sequence and
192   * can be as long as desired.  Long options provide a more user-friendly
193   * way of entering command line options.  For example, in addition to a
194   * "-h" for help, a program could support also "--help".  
195   * <p>
196   * Like short options, long options can also take a required or non-required 
197   * argument.  Required arguments can either be specified by placing an
198   * equals sign after the option name, then the argument, or by putting the
199   * argument in the next argv element.  For example: "--outputdir=foo" and
200   * "--outputdir foo" both represent an option of "outputdir" with an
201   * argument of "foo", assuming that outputdir takes a required argument.
202   * If a long option takes a non-required argument, then the equals sign
203   * form must be used to specify the argument.  In this case,
204   * "--outputdir=foo" would represent option outputdir with an argument of
205   * "foo" while "--outputdir foo" would represent the option outputdir
206   * with no argument and a first non-option argv element of "foo".
207   * <p>
208   * Long options can also be specified using a special POSIX argument 
209   * format (one that I highly discourage).  This form of entry is 
210   * enabled by placing a "W;" (yes, 'W' then a semi-colon) in the valid
211   * option string.  This causes getopt to treat the name following the
212   * "-W" as the name of the long option.  For example, "-W outputdir=foo"
213   * would be equivalent to "--outputdir=foo".  The name can immediately
214   * follow the "-W" like so: "-Woutputdir=foo".  Option arguments are
215   * handled identically to normal long options.  If a string follows the 
216   * "-W" that does not represent a valid long option, then getopt() returns
217   * 'W' and the caller must decide what to do.  Otherwise getopt() returns
218   * a long option value as described below.
219   * <p>
220   * While long options offer convenience, they can also be tedious to type
221   * in full.  So it is permissible to abbreviate the option name to as
222   * few characters as required to uniquely identify it.  If the name can
223   * represent multiple long options, then an error message is printed and
224   * getopt() returns a '?'.  
225   * <p>
226   * If an invalid option is specified or a required option argument is 
227   * missing, getopt() prints an error and returns a '?' or ':' exactly
228   * as for short options.  Note that when an invalid long option is
229   * encountered, the optopt variable is set to integer 0 and so cannot
230   * be used to identify the incorrect option the user entered.
231   * <p>
232   * Long options are defined by LongOpt objects.  These objects are created
233   * with a contructor that takes four params: a String representing the
234   * object name, a integer specifying what arguments the option takes
235   * (the value is one of LongOpt.NO_ARGUMENT, LongOpt.REQUIRED_ARGUMENT,
236   * or LongOpt.OPTIONAL_ARGUMENT), a StringBuffer flag object (described
237   * below), and an integer value (described below).
238   * <p>
239   * To enable long option parsing, create an array of LongOpt's representing
240   * the legal options and pass it to the Getopt() constructor.  WARNING: If
241   * all elements of the array are not populated with LongOpt objects, the
242   * getopt() method will throw a NullPointerException.
243   * <p>
244   * When getopt() is called and a long option is encountered, one of two
245   * things can be returned.  If the flag field in the LongOpt object 
246   * representing the long option is non-null, then the integer value field
247   * is stored there and an integer 0 is returned to the caller.  The val
248   * field can then be retrieved from the flag field.  Note that since the
249   * flag field is a StringBuffer, the appropriate String to integer converions
250   * must be performed in order to get the actual int value stored there.
251   * If the flag field in the LongOpt object is null, then the value field
252   * of the LongOpt is returned.  This can be the character of a short option.
253   * This allows an app to have both a long and short option sequence 
254   * (say, "-h" and "--help") that do the exact same thing.
255   * <p>
256   * With long options, there is an alternative method of determining 
257   * which option was selected.  The method getLongind() will return the
258   * the index in the long option array (NOT argv) of the long option found.
259   * So if multiple long options are configured to return the same value,
260   * the application can use getLongind() to distinguish between them. 
261   * <p>
262   * Here is an expanded Getopt example using long options and various
263   * techniques described above:
264   * <p>
265   * <pre>
266   * int c;
267   * String arg;
268   * LongOpt[] longopts = new LongOpt[3];
269   * // 
270   * StringBuffer sb = new StringBuffer();
271   * longopts[0] = new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h');
272   * longopts[1] = new LongOpt("outputdir", LongOpt.REQUIRED_ARGUMENT, sb, 'o'); 
273   * longopts[2] = new LongOpt("maximum", LongOpt.OPTIONAL_ARGUMENT, null, 2);
274   * // 
275   * Getopt g = new Getopt("testprog", argv, "-:bc::d:hW;", longopts);
276   * g.setOpterr(false); // We'll do our own error handling
277   * //
278   * while ((c = g.getopt()) != -1)
279   *   switch (c)
280   *     {
281   *        case 0:
282   *          arg = g.getOptarg();
283   *          System.out.println("Got long option with value '" +
284   *                             (char)(new Integer(sb.toString())).intValue()
285   *                             + "' with argument " +
286   *                             ((arg != null) ? arg : "null"));
287   *          break;
288   *          //
289   *        case 1:
290   *          System.out.println("I see you have return in order set and that " +
291   *                             "a non-option argv element was just found " +
292   *                             "with the value '" + g.getOptarg() + "'");
293   *          break;
294   *          //
295   *        case 2:
296   *          arg = g.getOptarg();
297   *          System.out.println("I know this, but pretend I didn't");
298   *          System.out.println("We picked option " +
299   *                             longopts[g.getLongind()].getName() +
300   *                           " with value " + 
301   *                           ((arg != null) ? arg : "null"));
302   *          break;
303   *          //
304   *        case 'b':
305   *          System.out.println("You picked plain old option " + (char)c);
306   *          break;
307   *          //
308   *        case 'c':
309   *        case 'd':
310   *          arg = g.getOptarg();
311   *          System.out.println("You picked option '" + (char)c + 
312   *                             "' with argument " +
313   *                             ((arg != null) ? arg : "null"));
314   *          break;
315   *          //
316   *        case 'h':
317   *          System.out.println("I see you asked for help");
318   *          break;
319   *          //
320   *        case 'W':
321   *          System.out.println("Hmmm. You tried a -W with an incorrect long " +
322   *                             "option name");
323   *          break;
324   *          //
325   *        case ':':
326   *          System.out.println("Doh! You need an argument for option " +
327   *                             (char)g.getOptopt());
328   *          break;
329   *          //
330   *        case '?':
331   *          System.out.println("The option '" + (char)g.getOptopt() + 
332   *                           "' is not valid");
333   *          break;
334   *          //
335   *        default:
336   *          System.out.println("getopt() returned " + c);
337   *          break;
338   *     }
339   * //
340   * for (int i = g.getOptind(); i < argv.length ; i++)
341   *   System.out.println("Non option argv element: " + argv[i] + "\n");
342   * </pre>
343   * <p>
344   * There is an alternative form of the constructor used for long options
345   * above.  This takes a trailing boolean flag.  If set to false, Getopt
346   * performs identically to the example, but if the boolean flag is true
347   * then long options are allowed to start with a single '-' instead of
348   * "--".  If the first character of the option is a valid short option
349   * character, then the option is treated as if it were the short option.
350   * Otherwise it behaves as if the option is a long option.  Note that
351   * the name given to this option - long_only - is very counter-intuitive.
352   * It does not cause only long options to be parsed but instead enables
353   * the behavior described above.
354   * <p> 
355   * Note that the functionality and variable names used are driven from 
356   * the C lib version as this object is a port of the C code, not a 
357   * new implementation.  This should aid in porting existing C/C++ code,
358   * as well as helping programmers familiar with the glibc version to
359   * adapt to the Java version even if it seems very non-Java at times.
360   * <p>
361   * In this release I made all instance variables protected due to
362   * overwhelming public demand.  Any code which relied on optarg,
363   * opterr, optind, or optopt being public will need to be modified to
364   * use the appropriate access methods.
365   * <p>
366   * Please send all bug reports, requests, and comments to
367   * <a href="mailto:arenn@urbanophile.com">arenn@urbanophile.com</a>.
368   *
369   * @version 1.0.7
370   *
371   * @author Roland McGrath (roland@gnu.ai.mit.edu)
372   * @author Ulrich Drepper (drepper@cygnus.com)
373   * @author Aaron M. Renn (arenn@urbanophile.com)
374   *
375   * @see LongOpt
376   */
377 public class Getopt extends Object
378 {
379 
380 /**************************************************************************/
381 
382 /*
383  * Class Variables
384  */
385 
386 /** 
387   * Describe how to deal with options that follow non-option ARGV-elements.
388   *
389   * If the caller did not specify anything,
390   * the default is REQUIRE_ORDER if the property 
391   * gnu.posixly_correct is defined, PERMUTE otherwise.
392   *
393   * The special argument `--' forces an end of option-scanning regardless
394   * of the value of `ordering'.  In the case of RETURN_IN_ORDER, only
395   * `--' can cause `getopt' to return -1 with `optind' != ARGC.
396   *
397   * REQUIRE_ORDER means don't recognize them as options;
398   * stop option processing when the first non-option is seen.
399   * This is what Unix does.
400   * This mode of operation is selected by either setting the property
401   * gnu.posixly_correct, or using `+' as the first character
402   * of the list of option characters.
403   */
404 protected static final int REQUIRE_ORDER = 1;
405 
406 /**
407   * PERMUTE is the default.  We permute the contents of ARGV as we scan,
408   * so that eventually all the non-options are at the end.  This allows options
409   * to be given in any order, even with programs that were not written to
410   * expect this.
411   */
412 protected static final int PERMUTE = 2;
413 
414 /**
415   * RETURN_IN_ORDER is an option available to programs that were written
416   * to expect options and other ARGV-elements in any order and that care about
417   * the ordering of the two.  We describe each non-option ARGV-element
418   * as if it were the argument of an option with character code 1.
419   * Using `-' as the first character of the list of option characters
420   * selects this mode of operation.
421   */
422 protected static final int RETURN_IN_ORDER = 3;
423 
424 /**************************************************************************/
425 
426 /*
427  * Instance Variables
428  */
429  
430 /**
431   * For communication from `getopt' to the caller.
432   * When `getopt' finds an option that takes an argument,
433   * the argument value is returned here.
434   * Also, when `ordering' is RETURN_IN_ORDER,
435   * each non-option ARGV-element is returned here.
436   */
437 protected String optarg;
438 
439 /**
440   *  Index in ARGV of the next element to be scanned.
441   *  This is used for communication to and from the caller
442   *  and for communication between successive calls to `getopt'.
443   *
444   *  On entry to `getopt', zero means this is the first call; initialize.
445   *
446   *  When `getopt' returns -1, this is the index of the first of the
447   *  non-option elements that the caller should itself scan.
448   *
449   *  Otherwise, `optind' communicates from one call to the next
450   *  how much of ARGV has been scanned so far.  
451   */
452 protected int optind = 0;
453 
454 /** 
455   * Callers store false here to inhibit the error message
456   * for unrecognized options.  
457   */
458 protected boolean opterr = true;
459 
460 /** 
461   * When an unrecognized option is encountered, getopt will return a '?'
462   * and store the value of the invalid option here.
463   */
464 protected int optopt = '?';
465 
466 /** 
467   * The next char to be scanned in the option-element
468   * in which the last option character we returned was found.
469   * This allows us to pick up the scan where we left off.
470   *
471   * If this is zero, or a null string, it means resume the scan
472   * by advancing to the next ARGV-element.  
473   */
474 protected String nextchar;
475 
476 /**
477   * This is the string describing the valid short options.
478   */
479 protected String optstring;
480 
481 /**
482   * This is an array of LongOpt objects which describ the valid long 
483   * options.
484   */
485 protected LongOpt[] long_options;
486 
487 /**
488   * This flag determines whether or not we are parsing only long args
489   */
490 protected boolean long_only;
491 
492 /**
493   * Stores the index into the long_options array of the long option found
494   */
495 protected int longind;
496 
497 /**
498   * The flag determines whether or not we operate in strict POSIX compliance
499   */
500 protected boolean posixly_correct;
501 
502 /**
503   * A flag which communicates whether or not checkLongOption() did all
504   * necessary processing for the current option
505   */
506 protected boolean longopt_handled;
507 
508 /**
509   * The index of the first non-option in argv[]
510   */
511 protected int first_nonopt = 1;
512 
513 /**
514   * The index of the last non-option in argv[]
515   */
516 protected int last_nonopt = 1;
517 
518 /**
519   * Flag to tell getopt to immediately return -1 the next time it is
520   * called.
521   */
522 private boolean endparse = false;
523 
524 /**
525   * Saved argument list passed to the program
526   */
527 protected String[] argv;
528 
529 /**
530   * Determines whether we permute arguments or not
531   */
532 protected int ordering;
533 
534 /**
535   * Name to print as the program name in error messages.  This is necessary
536   * since Java does not place the program name in argv[0]
537   */
538 protected String progname;
539 
540 /**
541   * The localized strings are kept in a separate file
542   */
543 private ResourceBundle _messages = PropertyResourceBundle.getBundle(
544                            "gnu/getopt/MessagesBundle", Locale.getDefault());
545 
546 /**************************************************************************/
547 
548 /*
549  * Constructors
550  */
551 
552 /**
553   * Construct a basic Getopt instance with the given input data.  Note that
554   * this handles "short" options only.
555   *
556   * @param progname The name to display as the program name when printing errors
557   * @param argv The String array passed as the command line to the program.
558   * @param optstring A String containing a description of the valid args for this program
559   */
560 public
561 Getopt(String progname, String[] argv, String optstring)
562 {
563   this(progname, argv, optstring, null, false);
564 }
565 
566 /**************************************************************************/
567 
568 /**
569   * Construct a Getopt instance with given input data that is capable of
570   * parsing long options as well as short.
571   *
572   * @param progname The name to display as the program name when printing errors
573   * @param argv The String array passed as the command ilne to the program
574   * @param optstring A String containing a description of the valid short args for this program
575   * @param long_options An array of LongOpt objects that describes the valid long args for this program
576   */
577 public
578 Getopt(String progname, String[] argv, String optstring, 
579        LongOpt[] long_options)
580 {
581   this(progname, argv, optstring, long_options, false);
582 }
583 
584 /**************************************************************************/
585 
586 /**
587   * Construct a Getopt instance with given input data that is capable of
588   * parsing long options and short options.  Contrary to what you might
589   * think, the flag 'long_only' does not determine whether or not we 
590   * scan for only long arguments.  Instead, a value of true here allows
591   * long arguments to start with a '-' instead of '--' unless there is a
592   * conflict with a short option name.
593   *
594   * @param progname The name to display as the program name when printing errors
595   * @param argv The String array passed as the command ilne to the program
596   * @param optstring A String containing a description of the valid short args for this program
597   * @param long_options An array of LongOpt objects that describes the valid long args for this program
598   * @param long_only true if long options that do not conflict with short options can start with a '-' as well as '--'
599   */
600 public
601 Getopt(String progname, String[] argv, String optstring, 
602        LongOpt[] long_options, boolean long_only)
603 {
604   if (optstring.length() == 0)
605     optstring = " ";
606 
607   // This function is essentially _getopt_initialize from GNU getopt
608   this.progname = progname;
609   this.argv = argv;
610   this.optstring = optstring;
611   this.long_options = long_options;
612   this.long_only = long_only;
613 
614   // Check for property "gnu.posixly_correct" to determine whether to
615   // strictly follow the POSIX standard.  This replaces the "POSIXLY_CORRECT"
616   // environment variable in the C version
617   if (System.getProperty("gnu.posixly_correct", null) == null)
618     posixly_correct = false;
619   else
620     {
621       posixly_correct = true;
622       _messages = PropertyResourceBundle.getBundle("gnu/getopt/MessagesBundle",
623                                                    Locale.US);
624     }
625 
626   // Determine how to handle the ordering of options and non-options
627   if (optstring.charAt(0) == '-')
628     {
629       ordering = RETURN_IN_ORDER;
630       if (optstring.length() > 1)
631         this.optstring = optstring.substring(1);
632     }
633   else if (optstring.charAt(0) == '+')
634     {
635       ordering = REQUIRE_ORDER;
636       if (optstring.length() > 1)
637         this.optstring = optstring.substring(1);
638     }
639   else if (posixly_correct)
640     {
641       ordering = REQUIRE_ORDER;
642     }
643   else
644     {
645       ordering = PERMUTE; // The normal default case
646     }
647 }
648 
649 /**************************************************************************/
650  
651 /*
652  * Instance Methods
653  */
654 
655 /**
656   * In GNU getopt, it is possible to change the string containg valid options
657   * on the fly because it is passed as an argument to getopt() each time.  In
658   * this version we do not pass the string on every call.  In order to allow
659   * dynamic option string changing, this method is provided.
660   *
661   * @param optstring The new option string to use
662   */
663 public void
664 setOptstring(String optstring)
665 {
666   if (optstring.length() == 0)
667     optstring = " ";
668 
669   this.optstring = optstring;
670 }
671 
672 /**************************************************************************/
673 
674 /**
675   * optind it the index in ARGV of the next element to be scanned.
676   * This is used for communication to and from the caller
677   * and for communication between successive calls to `getopt'.
678   *
679   * When `getopt' returns -1, this is the index of the first of the
680   * non-option elements that the caller should itself scan.
681   *
682   * Otherwise, `optind' communicates from one call to the next
683   * how much of ARGV has been scanned so far.  
684   */
685 public int
686 getOptind()
687 {
688   return(optind);
689 }
690 
691 /**************************************************************************/
692 
693 /**
694   * This method allows the optind index to be set manually.  Normally this
695   * is not necessary (and incorrect usage of this method can lead to serious
696   * lossage), but optind is a public symbol in GNU getopt, so this method 
697   * was added to allow it to be modified by the caller if desired.
698   *
699   * @param optind The new value of optind
700   */
701 public void
702 setOptind(int optind)
703 {
704   this.optind = optind;
705 }
706 
707 /**************************************************************************/
708 
709 /**
710   * Since in GNU getopt() the argument vector is passed back in to the
711   * function every time, the caller can swap out argv on the fly.  Since
712   * passing argv is not required in the Java version, this method allows
713   * the user to override argv.  Note that incorrect use of this method can
714   * lead to serious lossage.
715   *
716   * @param argv New argument list
717   */
718 public void
719 setArgv(String[] argv)
720 {
721   this.argv = argv;
722 }
723 
724 /**************************************************************************/
725 
726 /** 
727   * For communication from `getopt' to the caller.
728   * When `getopt' finds an option that takes an argument,
729   * the argument value is returned here.
730   * Also, when `ordering' is RETURN_IN_ORDER,
731   * each non-option ARGV-element is returned here.
732   * No set method is provided because setting this variable has no effect.
733   */
734 public String
735 getOptarg()
736 {
737   return(optarg);
738 }
739 
740 /**************************************************************************/
741 
742 /**
743   * Normally Getopt will print a message to the standard error when an
744   * invalid option is encountered.  This can be suppressed (or re-enabled)
745   * by calling this method.  There is no get method for this variable 
746   * because if you can't remember the state you set this to, why should I?
747   */
748 public void
749 setOpterr(boolean opterr)
750 {
751   this.opterr = opterr;
752 }
753 
754 /**************************************************************************/
755 
756 /**
757   * When getopt() encounters an invalid option, it stores the value of that
758   * option in optopt which can be retrieved with this method.  There is
759   * no corresponding set method because setting this variable has no effect.
760   */
761 public int
762 getOptopt()
763 {
764   return(optopt);
765 }
766 
767 /**************************************************************************/
768 
769 /**
770   * Returns the index into the array of long options (NOT argv) representing
771   * the long option that was found.
772   */
773 public int
774 getLongind()
775 {
776   return(longind);
777 }
778 
779 /**************************************************************************/
780 
781 /**
782   * Exchange the shorter segment with the far end of the longer segment.
783   * That puts the shorter segment into the right place.
784   * It leaves the longer segment in the right place overall,
785   * but it consists of two parts that need to be swapped next.
786   * This method is used by getopt() for argument permutation.
787   */
788 protected void
789 exchange(String[] argv)
790 {
791   int bottom = first_nonopt;
792   int middle = last_nonopt;
793   int top = optind;
794   String tem;
795 
796   while (top > middle && middle > bottom)
797     {
798       if (top - middle > middle - bottom)
799         {
800           // Bottom segment is the short one. 
801           int len = middle - bottom;
802           int i;
803 
804           // Swap it with the top part of the top segment. 
805           for (i = 0; i < len; i++)
806             {
807               tem = argv[bottom + i];
808               argv[bottom + i] = argv[top - (middle - bottom) + i];
809               argv[top - (middle - bottom) + i] = tem;
810             }
811           // Exclude the moved bottom segment from further swapping. 
812           top -= len;
813         }
814       else
815         {
816           // Top segment is the short one.
817           int len = top - middle;
818           int i;
819 
820           // Swap it with the bottom part of the bottom segment. 
821           for (i = 0; i < len; i++)
822             {
823               tem = argv[bottom + i];
824               argv[bottom + i] = argv[middle + i];
825               argv[middle + i] = tem;
826             }
827           // Exclude the moved top segment from further swapping. 
828           bottom += len;
829         }
830     }
831 
832   // Update records for the slots the non-options now occupy. 
833 
834   first_nonopt += (optind - last_nonopt);
835   last_nonopt = optind;
836 }
837 
838 /**************************************************************************/
839 
840 /**
841   * Check to see if an option is a valid long option.  Called by getopt().
842   * Put in a separate method because this needs to be done twice.  (The
843   * C getopt authors just copy-pasted the code!).
844   *
845   * @param longind A buffer in which to store the 'val' field of found LongOpt
846   *
847   * @return Various things depending on circumstances
848   */
849 protected int
850 checkLongOption()
851 {
852   LongOpt pfound = null;
853   int nameend;
854   boolean ambig;
855   boolean exact;
856   
857   longopt_handled = true;
858   ambig = false;
859   exact = false;
860   longind = -1;
861 
862   nameend = nextchar.indexOf("=");
863   if (nameend == -1)
864     nameend = nextchar.length();
865   
866   // Test all lnog options for either exact match or abbreviated matches
867   for (int i = 0; i < long_options.length; i++)
868     {
869       if (long_options[i].getName().startsWith(nextchar.substring(0, nameend)))
870         {
871           if (long_options[i].getName().equals(nextchar.substring(0, nameend)))
872             {
873               // Exact match found
874               pfound = long_options[i];
875               longind = i;
876               exact = true;
877               break;
878             }
879           else if (pfound == null)
880             {
881               // First nonexact match found
882               pfound = long_options[i];
883               longind = i;
884             }
885           else
886             {
887               // Second or later nonexact match found
888               ambig = true;
889             }
890         }
891     } // for
892   
893   // Print out an error if the option specified was ambiguous
894   if (ambig && !exact)
895     {
896       if (opterr)
897         {
898           Object[] msgArgs = { progname, argv[optind] };
899           System.err.println(MessageFormat.format(
900                              _messages.getString("getopt.ambigious"), 
901                              msgArgs));
902         }
903 
904        nextchar = "";
905        optopt = 0;
906        ++optind;
907  
908        return('?');
909     }
910  
911   if (pfound != null)
912     {
913       ++optind;
914  
915       if (nameend != nextchar.length())
916         {
917           if (pfound.has_arg != LongOpt.NO_ARGUMENT)
918             {
919               if (nextchar.substring(nameend).length() > 1)
920                 optarg = nextchar.substring(nameend+1);
921               else
922                 optarg = "";
923             }
924           else
925             {
926               if (opterr)
927                 {
928                   // -- option
929                   if (argv[optind - 1].startsWith("--"))
930                     {
931                       Object[] msgArgs = { progname, pfound.name };
932                       System.err.println(MessageFormat.format(
933                                   _messages.getString("getopt.arguments1"), 
934                                   msgArgs));
935                     }
936                   // +option or -option
937                   else
938                     {
939                       Object[] msgArgs = { progname, new 
940                                Character(argv[optind-1].charAt(0)).toString(),
941                                pfound.name };
942                       System.err.println(MessageFormat.format(
943                                _messages.getString("getopt.arguments2"), 
944                                msgArgs));
945                     }
946                  }
947    
948               nextchar = "";
949               optopt = pfound.val;
950    
951               return('?');
952             }
953         } // if (nameend)
954       else if (pfound.has_arg == LongOpt.REQUIRED_ARGUMENT)
955         {
956           if (optind < argv.length)
957             {
958                optarg = argv[optind];
959                ++optind;
960             }
961           else
962             {
963               if (opterr)
964                 {
965                   Object[] msgArgs = { progname, argv[optind-1] };
966                   System.err.println(MessageFormat.format(
967                                      _messages.getString("getopt.requires"), 
968                                      msgArgs));
969                 }
970    
971               nextchar = "";
972               optopt = pfound.val;
973               if (optstring.charAt(0) == ':')
974                 return(':');
975               else
976                 return('?');
977             }
978         } // else if (pfound)
979    
980       nextchar = "";
981 
982       if (pfound.flag != null)
983         {
984           pfound.flag.setLength(0);
985           pfound.flag.append(pfound.val);
986    
987           return(0);
988         }
989 
990       return(pfound.val);
991    } // if (pfound != null)
992   
993   longopt_handled = false;
994 
995   return(0);
996 }
997 
998 /**************************************************************************/
999 
1000/**
1001  * This method returns a char that is the current option that has been
1002  * parsed from the command line.  If the option takes an argument, then
1003  * the internal variable 'optarg' is set which is a String representing
1004  * the the value of the argument.  This value can be retrieved by the
1005  * caller using the getOptarg() method.  If an invalid option is found,
1006  * an error message is printed and a '?' is returned.  The name of the
1007  * invalid option character can be retrieved by calling the getOptopt()
1008  * method.  When there are no more options to be scanned, this method
1009  * returns -1.  The index of first non-option element in argv can be
1010  * retrieved with the getOptind() method.
1011  *
1012  * @return Various things as described above
1013  */
1014public int
1015getopt()
1016{
1017  optarg = null;
1018
1019  if (endparse == true)
1020    return(-1);
1021
1022  if ((nextchar == null) || (nextchar.equals("")))
1023    {
1024      // If we have just processed some options following some non-options,
1025      //  exchange them so that the options come first.
1026      if (last_nonopt > optind)
1027        last_nonopt = optind;
1028      if (first_nonopt > optind)
1029        first_nonopt = optind;
1030
1031      if (ordering == PERMUTE)
1032        {
1033          // If we have just processed some options following some non-options,
1034          // exchange them so that the options come first.
1035          if ((first_nonopt != last_nonopt) && (last_nonopt != optind))
1036            exchange(argv);
1037          else if (last_nonopt != optind)
1038            first_nonopt = optind;
1039
1040          // Skip any additional non-options
1041          // and extend the range of non-options previously skipped.
1042          while ((optind < argv.length) && (argv[optind].equals("") ||
1043            (argv[optind].charAt(0) != '-') || argv[optind].equals("-")))
1044            {
1045              optind++;
1046            }
1047          
1048          last_nonopt = optind;
1049        }
1050
1051      // The special ARGV-element `--' means premature end of options.
1052      // Skip it like a null option,
1053      // then exchange with previous non-options as if it were an option,
1054      // then skip everything else like a non-option.
1055      if ((optind != argv.length) && argv[optind].equals("--"))
1056        {
1057          optind++;
1058
1059          if ((first_nonopt != last_nonopt) && (last_nonopt != optind))
1060            exchange (argv);
1061          else if (first_nonopt == last_nonopt)
1062            first_nonopt = optind;
1063
1064          last_nonopt = argv.length;
1065
1066          optind = argv.length;
1067        }
1068
1069      // If we have done all the ARGV-elements, stop the scan
1070      // and back over any non-options that we skipped and permuted.
1071      if (optind == argv.length)
1072        {
1073          // Set the next-arg-index to point at the non-options
1074          // that we previously skipped, so the caller will digest them.
1075          if (first_nonopt != last_nonopt)
1076            optind = first_nonopt;
1077
1078          return(-1);
1079        }
1080
1081      // If we have come to a non-option and did not permute it,
1082      // either stop the scan or describe it to the caller and pass it by.
1083      if (argv[optind].equals("") || (argv[optind].charAt(0) != '-') || 
1084          argv[optind].equals("-"))
1085        {
1086          if (ordering == REQUIRE_ORDER)
1087            return(-1);
1088
1089            optarg = argv[optind++];
1090            return(1);
1091        }
1092      
1093      // We have found another option-ARGV-element.
1094      // Skip the initial punctuation.
1095      if (argv[optind].startsWith("--"))
1096        nextchar = argv[optind].substring(2);
1097      else
1098        nextchar = argv[optind].substring(1);
1099   }
1100
1101  // Decode the current option-ARGV-element.
1102
1103  /* Check whether the ARGV-element is a long option.
1104
1105     If long_only and the ARGV-element has the form "-f", where f is
1106     a valid short option, don't consider it an abbreviated form of
1107     a long option that starts with f.  Otherwise there would be no
1108     way to give the -f short option.
1109
1110     On the other hand, if there's a long option "fubar" and
1111     the ARGV-element is "-fu", do consider that an abbreviation of
1112     the long option, just like "--fu", and not "-f" with arg "u".
1113
1114     This distinction seems to be the most useful approach.  */
1115  if ((long_options != null) && (argv[optind].startsWith("--")
1116      || (long_only && ((argv[optind].length()  > 2) || 
1117      (optstring.indexOf(argv[optind].charAt(1)) == -1)))))
1118    {
1119       int c = checkLongOption();
1120
1121       if (longopt_handled)
1122         return(c);
1123         
1124      // Can't find it as a long option.  If this is not getopt_long_only,
1125      // or the option starts with '--' or is not a valid short
1126      // option, then it's an error.
1127      // Otherwise interpret it as a short option.
1128      if (!long_only || argv[optind].startsWith("--")
1129        || (optstring.indexOf(nextchar.charAt(0)) == -1))
1130        {
1131          if (opterr)
1132            {
1133              if (argv[optind].startsWith("--"))
1134                {
1135                  Object[] msgArgs = { progname, nextchar };
1136                  System.err.println(MessageFormat.format(
1137                                   _messages.getString("getopt.unrecognized"), 
1138                                   msgArgs));
1139                }
1140              else
1141                {
1142                  Object[] msgArgs = { progname, new 
1143                                 Character(argv[optind].charAt(0)).toString(), 
1144                                 nextchar };
1145                  System.err.println(MessageFormat.format(
1146                                 _messages.getString("getopt.unrecognized2"), 
1147                                 msgArgs));
1148                }
1149            }
1150
1151          nextchar = "";
1152          ++optind;
1153          optopt = 0;
1154    
1155          return('?');
1156        }
1157    } // if (longopts)
1158
1159  // Look at and handle the next short option-character */
1160  int c = nextchar.charAt(0); //**** Do we need to check for empty str?
1161  if (nextchar.length() > 1)
1162    nextchar = nextchar.substring(1);
1163  else
1164    nextchar = "";
1165  
1166  String temp = null;
1167  if (optstring.indexOf(c) != -1)
1168    temp = optstring.substring(optstring.indexOf(c));
1169
1170  if (nextchar.equals(""))
1171    ++optind;
1172
1173  if ((temp == null) || (c == ':'))
1174    {
1175      if (opterr)
1176        {
1177          if (posixly_correct)
1178            {
1179              // 1003.2 specifies the format of this message
1180              Object[] msgArgs = { progname, new 
1181                                   Character((char)c).toString() };
1182              System.err.println(MessageFormat.format(
1183                            _messages.getString("getopt.illegal"), msgArgs));
1184            }
1185          else
1186            {
1187              Object[] msgArgs = { progname, new 
1188                                   Character((char)c).toString() };
1189              System.err.println(MessageFormat.format(
1190                            _messages.getString("getopt.invalid"), msgArgs));
1191            }
1192        }
1193
1194      optopt = c;
1195
1196      return('?');
1197    }
1198
1199  // Convenience. Treat POSIX -W foo same as long option --foo
1200  if ((temp.charAt(0) == 'W') && (temp.length() > 1) && (temp.charAt(1) == ';'))
1201    {
1202      if (!nextchar.equals(""))
1203        {
1204          optarg = nextchar;
1205        }
1206      // No further cars in this argv element and no more argv elements
1207      else if (optind == argv.length)
1208        {
1209          if (opterr)
1210            {
1211              // 1003.2 specifies the format of this message. 
1212              Object[] msgArgs = { progname, new 
1213                                   Character((char)c).toString() };
1214              System.err.println(MessageFormat.format(
1215                            _messages.getString("getopt.requires2"), msgArgs));
1216            }
1217
1218          optopt = c;
1219          if (optstring.charAt(0) == ':')
1220            return(':');
1221          else
1222            return('?');
1223        }
1224      else
1225        {
1226          // We already incremented `optind' once;
1227          // increment it again when taking next ARGV-elt as argument. 
1228          nextchar = argv[optind];
1229          optarg  = argv[optind];
1230        }
1231
1232      c = checkLongOption();
1233
1234      if (longopt_handled)
1235        return(c);
1236      else
1237        // Let the application handle it
1238        {
1239          nextchar = null;
1240          ++optind;
1241          return('W');
1242        }
1243    }
1244
1245  if ((temp.length() > 1) && (temp.charAt(1) == ':'))
1246    {
1247      if ((temp.length() > 2) && (temp.charAt(2) == ':'))
1248        // This is an option that accepts and argument optionally
1249        {
1250          if (!nextchar.equals(""))
1251            {
1252               optarg = nextchar;
1253               ++optind;
1254            }
1255          else
1256            {
1257              optarg = null;
1258            }
1259
1260          nextchar = null;
1261        }
1262      else
1263        {
1264          if (!nextchar.equals(""))
1265            {
1266              optarg = nextchar;
1267              ++optind;
1268            }
1269          else if (optind == argv.length)
1270            {
1271              if (opterr)
1272                {
1273                  // 1003.2 specifies the format of this message
1274                  Object[] msgArgs = { progname, new 
1275                                       Character((char)c).toString() };
1276                  System.err.println(MessageFormat.format(
1277                            _messages.getString("getopt.requires2"), msgArgs));
1278                }
1279
1280              optopt = c;
1281 
1282              if (optstring.charAt(0) == ':')
1283                return(':');
1284              else
1285                return('?');
1286            }
1287          else
1288            {
1289              optarg = argv[optind];
1290              ++optind;
1291
1292              // Ok, here's an obscure Posix case.  If we have o:, and
1293              // we get -o -- foo, then we're supposed to skip the --,
1294              // end parsing of options, and make foo an operand to -o.
1295              // Only do this in Posix mode.
1296              if ((posixly_correct) && optarg.equals("--"))
1297                {
1298                  // If end of argv, error out
1299                  if (optind == argv.length)
1300                    {
1301                      if (opterr)
1302                        {
1303                          // 1003.2 specifies the format of this message
1304                          Object[] msgArgs = { progname, new 
1305                                               Character((char)c).toString() };
1306                          System.err.println(MessageFormat.format(
1307                             _messages.getString("getopt.requires2"), msgArgs));
1308                        }
1309
1310                      optopt = c;
1311 
1312                      if (optstring.charAt(0) == ':')
1313                        return(':');
1314                      else
1315                        return('?');
1316                    }
1317
1318                  // Set new optarg and set to end
1319                  // Don't permute as we do on -- up above since we
1320                  // know we aren't in permute mode because of Posix.
1321                  optarg = argv[optind];
1322                  ++optind;
1323                  first_nonopt = optind;
1324                  last_nonopt = argv.length;
1325                  endparse = true;
1326                }
1327            }
1328
1329          nextchar = null;
1330        }
1331    }
1332
1333  return(c);
1334}
1335
1336} // Class Getopt
1337
1338