source: josm/trunk/src/gnu/getopt/Getopt.java@ 13561

Last change on this file since 13561 was 12892, checked in by bastiK, 7 years ago

see #15229 - remove dependency of getopt library on JOSM classes

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