1 |
d2a8c3fd
|
Marc NICOLAS
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
|
2 |
|
|
|
3 |
|
|
<html>
|
4 |
|
|
<head>
|
5 |
|
|
|
6 |
|
|
<title>
|
7 |
|
|
Class gnu.getopt.Getopt
|
8 |
|
|
</title>
|
9 |
|
|
</head>
|
10 |
|
|
<body>
|
11 |
|
|
<a name="_top_"></a>
|
12 |
|
|
<pre>
|
13 |
|
|
<a href="packages.html">All Packages</a> <a href="tree.html">Class Hierarchy</a> <a href="Package-gnu.getopt.html">This Package</a> <a href="Package-gnu.getopt.html">Previous</a> <a href="Package-gnu.getopt.html">Next</a> <a href="AllNames.html">Index</a></pre>
|
14 |
|
|
<hr>
|
15 |
|
|
<h1>
|
16 |
|
|
Class gnu.getopt.Getopt
|
17 |
|
|
</h1>
|
18 |
|
|
<pre>
|
19 |
|
|
java.lang.Object
|
20 |
|
|
|
|
21 |
|
|
+----gnu.getopt.Getopt
|
22 |
|
|
</pre>
|
23 |
|
|
<hr>
|
24 |
|
|
<dl>
|
25 |
|
|
<dt> public class <b>Getopt</b>
|
26 |
|
|
<dt> extends Object
|
27 |
|
|
</dl>
|
28 |
|
|
This is a Java port of GNU getopt, a class for parsing command line
|
29 |
|
|
arguments passed to programs. It it based on the C getopt() functions
|
30 |
|
|
in glibc 2.0.6 and should parse options in a 100% compatible manner.
|
31 |
|
|
If it does not, that is a bug. The programmer's interface is also
|
32 |
|
|
very compatible.
|
33 |
|
|
<p>
|
34 |
|
|
To use Getopt, create a Getopt object with a argv array passed to the
|
35 |
|
|
main method, then call the getopt() method in a loop. It will return an
|
36 |
|
|
int that contains the value of the option character parsed from the
|
37 |
|
|
command line. When there are no more options to be parsed, it
|
38 |
|
|
returns -1.
|
39 |
|
|
<p>
|
40 |
|
|
A command line option can be defined to take an argument. If an
|
41 |
|
|
option has an argument, the value of that argument is stored in an
|
42 |
|
|
instance variable called optarg, which can be accessed using the
|
43 |
|
|
getOptarg() method. If an option that requires an argument is
|
44 |
|
|
found, but there is no argument present, then an error message is
|
45 |
|
|
printed. Normally getopt() returns a '?' in this situation, but
|
46 |
|
|
that can be changed as described below.
|
47 |
|
|
<p>
|
48 |
|
|
If an invalid option is encountered, an error message is printed
|
49 |
|
|
to the standard error and getopt() returns a '?'. The value of the
|
50 |
|
|
invalid option encountered is stored in the instance variable optopt
|
51 |
|
|
which can be retrieved using the getOptopt() method. To suppress
|
52 |
|
|
the printing of error messages for this or any other error, set
|
53 |
|
|
the value of the opterr instance variable to false using the
|
54 |
|
|
setOpterr() method.
|
55 |
|
|
<p>
|
56 |
|
|
Between calls to getopt(), the instance variable optind is used to
|
57 |
|
|
keep track of where the object is in the parsing process. After all
|
58 |
|
|
options have been returned, optind is the index in argv of the first
|
59 |
|
|
non-option argument. This variable can be accessed with the getOptind()
|
60 |
|
|
method.
|
61 |
|
|
<p>
|
62 |
|
|
Note that this object expects command line options to be passed in the
|
63 |
|
|
traditional Unix manner. That is, proceeded by a '-' character.
|
64 |
|
|
Multiple options can follow the '-'. For example "-abc" is equivalent
|
65 |
|
|
to "-a -b -c". If an option takes a required argument, the value
|
66 |
|
|
of the argument can immediately follow the option character or be
|
67 |
|
|
present in the next argv element. For example, "-cfoo" and "-c foo"
|
68 |
|
|
both represent an option character of 'c' with an argument of "foo"
|
69 |
|
|
assuming c takes a required argument. If an option takes an argument
|
70 |
|
|
that is not required, then any argument must immediately follow the
|
71 |
|
|
option character in the same argv element. For example, if c takes
|
72 |
|
|
a non-required argument, then "-cfoo" represents option character 'c'
|
73 |
|
|
with an argument of "foo" while "-c foo" represents the option
|
74 |
|
|
character 'c' with no argument, and a first non-option argv element
|
75 |
|
|
of "foo".
|
76 |
|
|
<p>
|
77 |
|
|
The user can stop getopt() from scanning any further into a command line
|
78 |
|
|
by using the special argument "--" by itself. For example:
|
79 |
|
|
"-a -- -d" would return an option character of 'a', then return -1
|
80 |
|
|
The "--" is discarded and "-d" is pointed to by optind as the first
|
81 |
|
|
non-option argv element.
|
82 |
|
|
<p>
|
83 |
|
|
Here is a basic example of using Getopt:
|
84 |
|
|
<p>
|
85 |
|
|
<pre>
|
86 |
|
|
Getopt g = new Getopt("testprog", argv, "ab:c::d");
|
87 |
|
|
//
|
88 |
|
|
int c;
|
89 |
|
|
String arg;
|
90 |
|
|
while ((c = g.getopt()) != -1)
|
91 |
|
|
{
|
92 |
|
|
switch(c)
|
93 |
|
|
{
|
94 |
|
|
case 'a':
|
95 |
|
|
case 'd':
|
96 |
|
|
System.out.print("You picked " + (char)c + "\n");
|
97 |
|
|
break;
|
98 |
|
|
//
|
99 |
|
|
case 'b':
|
100 |
|
|
case 'c':
|
101 |
|
|
arg = g.getOptarg();
|
102 |
|
|
System.out.print("You picked " + (char)c +
|
103 |
|
|
" with an argument of " +
|
104 |
|
|
((arg != null) ? arg : "null") + "\n");
|
105 |
|
|
break;
|
106 |
|
|
//
|
107 |
|
|
case '?':
|
108 |
|
|
break; // getopt() already printed an error
|
109 |
|
|
//
|
110 |
|
|
default:
|
111 |
|
|
System.out.print("getopt() returned " + c + "\n");
|
112 |
|
|
}
|
113 |
|
|
}
|
114 |
|
|
</pre>
|
115 |
|
|
<p>
|
116 |
|
|
In this example, a new Getopt object is created with three params.
|
117 |
|
|
The first param is the program name. This is for printing error
|
118 |
|
|
messages in the form "program: error message". In the C version, this
|
119 |
|
|
value is taken from argv[0], but in Java the program name is not passed
|
120 |
|
|
in that element, thus the need for this parameter. The second param is
|
121 |
|
|
the argument list that was passed to the main() method. The third
|
122 |
|
|
param is the list of valid options. Each character represents a valid
|
123 |
|
|
option. If the character is followed by a single colon, then that
|
124 |
|
|
option has a required argument. If the character is followed by two
|
125 |
|
|
colons, then that option has an argument that is not required.
|
126 |
|
|
<p>
|
127 |
|
|
Note in this example that the value returned from getopt() is cast to
|
128 |
|
|
a char prior to printing. This is required in order to make the value
|
129 |
|
|
display correctly as a character instead of an integer.
|
130 |
|
|
<p>
|
131 |
|
|
If the first character in the option string is a colon, for example
|
132 |
|
|
":abc::d", then getopt() will return a ':' instead of a '?' when it
|
133 |
|
|
encounters an option with a missing required argument. This allows the
|
134 |
|
|
caller to distinguish between invalid options and valid options that
|
135 |
|
|
are simply incomplete.
|
136 |
|
|
<p>
|
137 |
|
|
In the traditional Unix getopt(), -1 is returned when the first non-option
|
138 |
|
|
charcter is encountered. In GNU getopt(), the default behavior is to
|
139 |
|
|
allow options to appear anywhere on the command line. The getopt()
|
140 |
|
|
method permutes the argument to make it appear to the caller that all
|
141 |
|
|
options were at the beginning of the command line, and all non-options
|
142 |
|
|
were at the end. For example, calling getopt() with command line args
|
143 |
|
|
of "-a foo bar -d" returns options 'a' and 'd', then sets optind to
|
144 |
|
|
point to "foo". The program would read the last two argv elements as
|
145 |
|
|
"foo" and "bar", just as if the user had typed "-a -d foo bar".
|
146 |
|
|
<p>
|
147 |
|
|
The user can force getopt() to stop scanning the command line with
|
148 |
|
|
the special argument "--" by itself. Any elements occuring before the
|
149 |
|
|
"--" are scanned and permuted as normal. Any elements after the "--"
|
150 |
|
|
are returned as is as non-option argv elements. For example,
|
151 |
|
|
"foo -a -- bar -d" would return option 'a' then -1. optind would point
|
152 |
|
|
to "foo", "bar" and "-d" as the non-option argv elements. The "--"
|
153 |
|
|
is discarded by getopt().
|
154 |
|
|
<p>
|
155 |
|
|
There are two ways this default behavior can be modified. The first is
|
156 |
|
|
to specify traditional Unix getopt() behavior (which is also POSIX
|
157 |
|
|
behavior) in which scanning stops when the first non-option argument
|
158 |
|
|
encountered. (Thus "-a foo bar -d" would return 'a' as an option and
|
159 |
|
|
have "foo", "bar", and "-d" as non-option elements). The second is to
|
160 |
|
|
allow options anywhere, but to return all elements in the order they
|
161 |
|
|
occur on the command line. When a non-option element is ecountered,
|
162 |
|
|
an integer 1 is returned and the value of the non-option element is
|
163 |
|
|
stored in optarg is if it were the argument to that option. For
|
164 |
|
|
example, "-a foo -d", returns first 'a', then 1 (with optarg set to
|
165 |
|
|
"foo") then 'd' then -1. When this "return in order" functionality
|
166 |
|
|
is enabled, the only way to stop getopt() from scanning all command
|
167 |
|
|
line elements is to use the special "--" string by itself as described
|
168 |
|
|
above. An example is "-a foo -b -- bar", which would return 'a', then
|
169 |
|
|
integer 1 with optarg set to "foo", then 'b', then -1. optind would
|
170 |
|
|
then point to "bar" as the first non-option argv element. The "--"
|
171 |
|
|
is discarded.
|
172 |
|
|
<p>
|
173 |
|
|
The POSIX/traditional behavior is enabled by either setting the
|
174 |
|
|
property "gnu.posixly_correct" or by putting a '+' sign as the first
|
175 |
|
|
character of the option string. The difference between the two
|
176 |
|
|
methods is that setting the gnu.posixly_correct property also forces
|
177 |
|
|
certain error messages to be displayed in POSIX format. To enable
|
178 |
|
|
the "return in order" functionality, put a '-' as the first character
|
179 |
|
|
of the option string. Note that after determining the proper
|
180 |
|
|
behavior, Getopt strips this leading '+' or '-', meaning that a ':'
|
181 |
|
|
placed as the second character after one of those two will still cause
|
182 |
|
|
getopt() to return a ':' instead of a '?' if a required option
|
183 |
|
|
argument is missing.
|
184 |
|
|
<p>
|
185 |
|
|
In addition to traditional single character options, GNU Getopt also
|
186 |
|
|
supports long options. These are preceeded by a "--" sequence and
|
187 |
|
|
can be as long as desired. Long options provide a more user-friendly
|
188 |
|
|
way of entering command line options. For example, in addition to a
|
189 |
|
|
"-h" for help, a program could support also "--help".
|
190 |
|
|
<p>
|
191 |
|
|
Like short options, long options can also take a required or non-required
|
192 |
|
|
argument. Required arguments can either be specified by placing an
|
193 |
|
|
equals sign after the option name, then the argument, or by putting the
|
194 |
|
|
argument in the next argv element. For example: "--outputdir=foo" and
|
195 |
|
|
"--outputdir foo" both represent an option of "outputdir" with an
|
196 |
|
|
argument of "foo", assuming that outputdir takes a required argument.
|
197 |
|
|
If a long option takes a non-required argument, then the equals sign
|
198 |
|
|
form must be used to specify the argument. In this case,
|
199 |
|
|
"--outputdir=foo" would represent option outputdir with an argument of
|
200 |
|
|
"foo" while "--outputdir foo" would represent the option outputdir
|
201 |
|
|
with no argument and a first non-option argv element of "foo".
|
202 |
|
|
<p>
|
203 |
|
|
Long options can also be specified using a special POSIX argument
|
204 |
|
|
format (one that I highly discourage). This form of entry is
|
205 |
|
|
enabled by placing a "W;" (yes, 'W' then a semi-colon) in the valid
|
206 |
|
|
option string. This causes getopt to treat the name following the
|
207 |
|
|
"-W" as the name of the long option. For example, "-W outputdir=foo"
|
208 |
|
|
would be equivalent to "--outputdir=foo". The name can immediately
|
209 |
|
|
follow the "-W" like so: "-Woutputdir=foo". Option arguments are
|
210 |
|
|
handled identically to normal long options. If a string follows the
|
211 |
|
|
"-W" that does not represent a valid long option, then getopt() returns
|
212 |
|
|
'W' and the caller must decide what to do. Otherwise getopt() returns
|
213 |
|
|
a long option value as described below.
|
214 |
|
|
<p>
|
215 |
|
|
While long options offer convenience, they can also be tedious to type
|
216 |
|
|
in full. So it is permissible to abbreviate the option name to as
|
217 |
|
|
few characters as required to uniquely identify it. If the name can
|
218 |
|
|
represent multiple long options, then an error message is printed and
|
219 |
|
|
getopt() returns a '?'.
|
220 |
|
|
<p>
|
221 |
|
|
If an invalid option is specified or a required option argument is
|
222 |
|
|
missing, getopt() prints an error and returns a '?' or ':' exactly
|
223 |
|
|
as for short options. Note that when an invalid long option is
|
224 |
|
|
encountered, the optopt variable is set to integer 0 and so cannot
|
225 |
|
|
be used to identify the incorrect option the user entered.
|
226 |
|
|
<p>
|
227 |
|
|
Long options are defined by LongOpt objects. These objects are created
|
228 |
|
|
with a contructor that takes four params: a String representing the
|
229 |
|
|
object name, a integer specifying what arguments the option takes
|
230 |
|
|
(the value is one of LongOpt.NO_ARGUMENT, LongOpt.REQUIRED_ARGUMENT,
|
231 |
|
|
or LongOpt.OPTIONAL_ARGUMENT), a StringBuffer flag object (described
|
232 |
|
|
below), and an integer value (described below).
|
233 |
|
|
<p>
|
234 |
|
|
To enable long option parsing, create an array of LongOpt's representing
|
235 |
|
|
the legal options and pass it to the Getopt() constructor. WARNING: If
|
236 |
|
|
all elements of the array are not populated with LongOpt objects, the
|
237 |
|
|
getopt() method will throw a NullPointerException.
|
238 |
|
|
<p>
|
239 |
|
|
When getopt() is called and a long option is encountered, one of two
|
240 |
|
|
things can be returned. If the flag field in the LongOpt object
|
241 |
|
|
representing the long option is non-null, then the integer value field
|
242 |
|
|
is stored there and an integer 0 is returned to the caller. The val
|
243 |
|
|
field can then be retrieved from the flag field. Note that since the
|
244 |
|
|
flag field is a StringBuffer, the appropriate String to integer converions
|
245 |
|
|
must be performed in order to get the actual int value stored there.
|
246 |
|
|
If the flag field in the LongOpt object is null, then the value field
|
247 |
|
|
of the LongOpt is returned. This can be the character of a short option.
|
248 |
|
|
This allows an app to have both a long and short option sequence
|
249 |
|
|
(say, "-h" and "--help") that do the exact same thing.
|
250 |
|
|
<p>
|
251 |
|
|
With long options, there is an alternative method of determining
|
252 |
|
|
which option was selected. The method getLongind() will return the
|
253 |
|
|
the index in the long option array (NOT argv) of the long option found.
|
254 |
|
|
So if multiple long options are configured to return the same value,
|
255 |
|
|
the application can use getLongind() to distinguish between them.
|
256 |
|
|
<p>
|
257 |
|
|
Here is an expanded Getopt example using long options and various
|
258 |
|
|
techniques described above:
|
259 |
|
|
<p>
|
260 |
|
|
<pre>
|
261 |
|
|
int c;
|
262 |
|
|
String arg;
|
263 |
|
|
LongOpt[] longopts = new LongOpt[3];
|
264 |
|
|
//
|
265 |
|
|
StringBuffer sb = new StringBuffer();
|
266 |
|
|
longopts[0] = new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h');
|
267 |
|
|
longopts[1] = new LongOpt("outputdir", LongOpt.REQUIRED_ARGUMENT, sb, 'o');
|
268 |
|
|
longopts[2] = new LongOpt("maximum", LongOpt.OPTIONAL_ARGUMENT, null, 2);
|
269 |
|
|
//
|
270 |
|
|
Getopt g = new Getopt("testprog", argv, "-:bc::d:hW;", longopts);
|
271 |
|
|
g.setOpterr(false); // We'll do our own error handling
|
272 |
|
|
//
|
273 |
|
|
while ((c = g.getopt()) != -1)
|
274 |
|
|
switch (c)
|
275 |
|
|
{
|
276 |
|
|
case 0:
|
277 |
|
|
arg = g.getOptarg();
|
278 |
|
|
System.out.println("Got long option with value '" +
|
279 |
|
|
(char)(new Integer(sb.toString())).intValue()
|
280 |
|
|
+ "' with argument " +
|
281 |
|
|
((arg != null) ? arg : "null"));
|
282 |
|
|
break;
|
283 |
|
|
//
|
284 |
|
|
case 1:
|
285 |
|
|
System.out.println("I see you have return in order set and that " +
|
286 |
|
|
"a non-option argv element was just found " +
|
287 |
|
|
"with the value '" + g.getOptarg() + "'");
|
288 |
|
|
break;
|
289 |
|
|
//
|
290 |
|
|
case 2:
|
291 |
|
|
arg = g.getOptarg();
|
292 |
|
|
System.out.println("I know this, but pretend I didn't");
|
293 |
|
|
System.out.println("We picked option " +
|
294 |
|
|
longopts[g.getLongind()].getName() +
|
295 |
|
|
" with value " +
|
296 |
|
|
((arg != null) ? arg : "null"));
|
297 |
|
|
break;
|
298 |
|
|
//
|
299 |
|
|
case 'b':
|
300 |
|
|
System.out.println("You picked plain old option " + (char)c);
|
301 |
|
|
break;
|
302 |
|
|
//
|
303 |
|
|
case 'c':
|
304 |
|
|
case 'd':
|
305 |
|
|
arg = g.getOptarg();
|
306 |
|
|
System.out.println("You picked option '" + (char)c +
|
307 |
|
|
"' with argument " +
|
308 |
|
|
((arg != null) ? arg : "null"));
|
309 |
|
|
break;
|
310 |
|
|
//
|
311 |
|
|
case 'h':
|
312 |
|
|
System.out.println("I see you asked for help");
|
313 |
|
|
break;
|
314 |
|
|
//
|
315 |
|
|
case 'W':
|
316 |
|
|
System.out.println("Hmmm. You tried a -W with an incorrect long " +
|
317 |
|
|
"option name");
|
318 |
|
|
break;
|
319 |
|
|
//
|
320 |
|
|
case ':':
|
321 |
|
|
System.out.println("Doh! You need an argument for option " +
|
322 |
|
|
(char)g.getOptopt());
|
323 |
|
|
break;
|
324 |
|
|
//
|
325 |
|
|
case '?':
|
326 |
|
|
System.out.println("The option '" + (char)g.getOptopt() +
|
327 |
|
|
"' is not valid");
|
328 |
|
|
break;
|
329 |
|
|
//
|
330 |
|
|
default:
|
331 |
|
|
System.out.println("getopt() returned " + c);
|
332 |
|
|
break;
|
333 |
|
|
}
|
334 |
|
|
//
|
335 |
|
|
for (int i = g.getOptind(); i < argv.length ; i++)
|
336 |
|
|
System.out.println("Non option argv element: " + argv[i] + "\n");
|
337 |
|
|
</pre>
|
338 |
|
|
<p>
|
339 |
|
|
There is an alternative form of the constructor used for long options
|
340 |
|
|
above. This takes a trailing boolean flag. If set to false, Getopt
|
341 |
|
|
performs identically to the example, but if the boolean flag is true
|
342 |
|
|
then long options are allowed to start with a single '-' instead of
|
343 |
|
|
"--". If the first character of the option is a valid short option
|
344 |
|
|
character, then the option is treated as if it were the short option.
|
345 |
|
|
Otherwise it behaves as if the option is a long option. Note that
|
346 |
|
|
the name given to this option - long_only - is very counter-intuitive.
|
347 |
|
|
It does not cause only long options to be parsed but instead enables
|
348 |
|
|
the behavior described above.
|
349 |
|
|
<p>
|
350 |
|
|
Note that the functionality and variable names used are driven from
|
351 |
|
|
the C lib version as this object is a port of the C code, not a
|
352 |
|
|
new implementation. This should aid in porting existing C/C++ code,
|
353 |
|
|
as well as helping programmers familiar with the glibc version to
|
354 |
|
|
adapt to the Java version even if it seems very non-Java at times.
|
355 |
|
|
<p>
|
356 |
|
|
In this release I made all instance variables protected due to
|
357 |
|
|
overwhelming public demand. Any code which relied on optarg,
|
358 |
|
|
opterr, optind, or optopt being public will need to be modified to
|
359 |
|
|
use the appropriate access methods.
|
360 |
|
|
<p>
|
361 |
|
|
Please send all bug reports, requests, and comments to
|
362 |
|
|
<a href="mailto:arenn@urbanophile.com">arenn@urbanophile.com</a>.
|
363 |
|
|
<p>
|
364 |
|
|
<dl>
|
365 |
|
|
<dt> <b>Version:</b>
|
366 |
|
|
<dd> 1.0.3
|
367 |
|
|
<dt> <b>Author:</b>
|
368 |
|
|
<dd> Roland McGrath (roland@gnu.ai.mit.edu), Ulrich Drepper (drepper@cygnus.com), Aaron M. Renn (arenn@urbanophile.com)
|
369 |
|
|
<dt> <b>See Also:</b>
|
370 |
|
|
<dd> <a href="gnu.getopt.LongOpt.html#_top_">LongOpt</a>
|
371 |
|
|
</dl>
|
372 |
|
|
<hr>
|
373 |
|
|
<a name="index"></a>
|
374 |
|
|
<h2>
|
375 |
|
|
<img src="images/constructor-index.gif" width=275 height=38 alt="Constructor Index">
|
376 |
|
|
</h2>
|
377 |
|
|
<dl>
|
378 |
|
|
<dt> <img src="images/yellow-ball-small.gif" width=6 height=6 alt=" o ">
|
379 |
|
|
<a href="#Getopt(java.lang.String, java.lang.String[], java.lang.String)"><b>Getopt</b></a>(String, String[], String)
|
380 |
|
|
<dd> Construct a basic Getopt instance with the given input data.
|
381 |
|
|
<dt> <img src="images/yellow-ball-small.gif" width=6 height=6 alt=" o ">
|
382 |
|
|
<a href="#Getopt(java.lang.String, java.lang.String[], java.lang.String, gnu.getopt.LongOpt[])"><b>Getopt</b></a>(String, String[], String, LongOpt[])
|
383 |
|
|
<dd> Construct a Getopt instance with given input data that is capable of
|
384 |
|
|
parsing long options as well as short.
|
385 |
|
|
<dt> <img src="images/yellow-ball-small.gif" width=6 height=6 alt=" o ">
|
386 |
|
|
<a href="#Getopt(java.lang.String, java.lang.String[], java.lang.String, gnu.getopt.LongOpt[], boolean)"><b>Getopt</b></a>(String, String[], String, LongOpt[], boolean)
|
387 |
|
|
<dd> Construct a Getopt instance with given input data that is capable of
|
388 |
|
|
parsing long options and short options.
|
389 |
|
|
</dl>
|
390 |
|
|
<h2>
|
391 |
|
|
<img src="images/method-index.gif" width=207 height=38 alt="Method Index">
|
392 |
|
|
</h2>
|
393 |
|
|
<dl>
|
394 |
|
|
<dt> <img src="images/red-ball-small.gif" width=6 height=6 alt=" o ">
|
395 |
|
|
<a href="#getLongind()"><b>getLongind</b></a>()
|
396 |
|
|
<dd> Returns the index into the array of long options (NOT argv) representing
|
397 |
|
|
the long option that was found.
|
398 |
|
|
<dt> <img src="images/red-ball-small.gif" width=6 height=6 alt=" o ">
|
399 |
|
|
<a href="#getopt()"><b>getopt</b></a>()
|
400 |
|
|
<dd> This method returns a char that is the current option that has been
|
401 |
|
|
parsed from the command line.
|
402 |
|
|
<dt> <img src="images/red-ball-small.gif" width=6 height=6 alt=" o ">
|
403 |
|
|
<a href="#getOptarg()"><b>getOptarg</b></a>()
|
404 |
|
|
<dd>
|
405 |
|
|
For communication from `getopt' to the caller.
|
406 |
|
|
<dt> <img src="images/red-ball-small.gif" width=6 height=6 alt=" o ">
|
407 |
|
|
<a href="#getOptind()"><b>getOptind</b></a>()
|
408 |
|
|
<dd> optind it the index in ARGV of the next element to be scanned.
|
409 |
|
|
<dt> <img src="images/red-ball-small.gif" width=6 height=6 alt=" o ">
|
410 |
|
|
<a href="#getOptopt()"><b>getOptopt</b></a>()
|
411 |
|
|
<dd> When getopt() encounters an invalid option, it stores the value of that
|
412 |
|
|
option in optopt which can be retrieved with this method.
|
413 |
|
|
<dt> <img src="images/red-ball-small.gif" width=6 height=6 alt=" o ">
|
414 |
|
|
<a href="#setArgv(java.lang.String[])"><b>setArgv</b></a>(String[])
|
415 |
|
|
<dd> Since in GNU getopt() the argument vector is passed back in to the
|
416 |
|
|
function every time, the caller can swap out argv on the fly.
|
417 |
|
|
<dt> <img src="images/red-ball-small.gif" width=6 height=6 alt=" o ">
|
418 |
|
|
<a href="#setOpterr(boolean)"><b>setOpterr</b></a>(boolean)
|
419 |
|
|
<dd> Normally Getopt will print a message to the standard error when an
|
420 |
|
|
invalid option is encountered.
|
421 |
|
|
<dt> <img src="images/red-ball-small.gif" width=6 height=6 alt=" o ">
|
422 |
|
|
<a href="#setOptind(int)"><b>setOptind</b></a>(int)
|
423 |
|
|
<dd> This method allows the optind index to be set manually.
|
424 |
|
|
<dt> <img src="images/red-ball-small.gif" width=6 height=6 alt=" o ">
|
425 |
|
|
<a href="#setOptstring(java.lang.String)"><b>setOptstring</b></a>(String)
|
426 |
|
|
<dd> In GNU getopt, it is possible to change the string containg valid options
|
427 |
|
|
on the fly because it is passed as an argument to getopt() each time.
|
428 |
|
|
</dl>
|
429 |
|
|
<a name="constructors"></a>
|
430 |
|
|
<h2>
|
431 |
|
|
<img src="images/constructors.gif" width=231 height=38 alt="Constructors">
|
432 |
|
|
</h2>
|
433 |
|
|
<a name="Getopt"></a>
|
434 |
|
|
<a name="Getopt(java.lang.String, java.lang.String[], java.lang.String)"><img src="images/yellow-ball.gif" width=12 height=12 alt=" o "></a>
|
435 |
|
|
<b>Getopt</b>
|
436 |
|
|
<pre>
|
437 |
|
|
public Getopt(String progname,
|
438 |
|
|
String argv[],
|
439 |
|
|
String optstring)
|
440 |
|
|
</pre>
|
441 |
|
|
<dl>
|
442 |
|
|
<dd> Construct a basic Getopt instance with the given input data. Note that
|
443 |
|
|
this handles "short" options only.
|
444 |
|
|
<p>
|
445 |
|
|
<dd><dl>
|
446 |
|
|
<dt> <b>Parameters:</b>
|
447 |
|
|
<dd> progname - The name to display as the program name when printing errors
|
448 |
|
|
<dd> argv - The String array passed as the command line to the program.
|
449 |
|
|
<dd> optstring - A String containing a description of the valid args for this program
|
450 |
|
|
</dl></dd>
|
451 |
|
|
</dl>
|
452 |
|
|
<a name="Getopt(java.lang.String, java.lang.String[], java.lang.String, gnu.getopt.LongOpt[])"><img src="images/yellow-ball.gif" width=12 height=12 alt=" o "></a>
|
453 |
|
|
<b>Getopt</b>
|
454 |
|
|
<pre>
|
455 |
|
|
public Getopt(String progname,
|
456 |
|
|
String argv[],
|
457 |
|
|
String optstring,
|
458 |
|
|
<a href="gnu.getopt.LongOpt.html#_top_">LongOpt</a> long_options[])
|
459 |
|
|
</pre>
|
460 |
|
|
<dl>
|
461 |
|
|
<dd> Construct a Getopt instance with given input data that is capable of
|
462 |
|
|
parsing long options as well as short.
|
463 |
|
|
<p>
|
464 |
|
|
<dd><dl>
|
465 |
|
|
<dt> <b>Parameters:</b>
|
466 |
|
|
<dd> progname - The name to display as the program name when printing errors
|
467 |
|
|
<dd> argv - The String array passed as the command ilne to the program
|
468 |
|
|
<dd> optstring - A String containing a description of the valid short args for this program
|
469 |
|
|
<dd> long_options - An array of LongOpt objects that describes the valid long args for this program
|
470 |
|
|
</dl></dd>
|
471 |
|
|
</dl>
|
472 |
|
|
<a name="Getopt(java.lang.String, java.lang.String[], java.lang.String, gnu.getopt.LongOpt[], boolean)"><img src="images/yellow-ball.gif" width=12 height=12 alt=" o "></a>
|
473 |
|
|
<b>Getopt</b>
|
474 |
|
|
<pre>
|
475 |
|
|
public Getopt(String progname,
|
476 |
|
|
String argv[],
|
477 |
|
|
String optstring,
|
478 |
|
|
<a href="gnu.getopt.LongOpt.html#_top_">LongOpt</a> long_options[],
|
479 |
|
|
boolean long_only)
|
480 |
|
|
</pre>
|
481 |
|
|
<dl>
|
482 |
|
|
<dd> Construct a Getopt instance with given input data that is capable of
|
483 |
|
|
parsing long options and short options. Contrary to what you might
|
484 |
|
|
think, the flag 'long_only' does not determine whether or not we
|
485 |
|
|
scan for only long arguments. Instead, a value of true here allows
|
486 |
|
|
long arguments to start with a '-' instead of '--' unless there is a
|
487 |
|
|
conflict with a short option name.
|
488 |
|
|
<p>
|
489 |
|
|
<dd><dl>
|
490 |
|
|
<dt> <b>Parameters:</b>
|
491 |
|
|
<dd> progname - The name to display as the program name when printing errors
|
492 |
|
|
<dd> argv - The String array passed as the command ilne to the program
|
493 |
|
|
<dd> optstring - A String containing a description of the valid short args for this program
|
494 |
|
|
<dd> long_options - An array of LongOpt objects that describes the valid long args for this program
|
495 |
|
|
<dd> long_only - true if long options that do not conflict with short options can start with a '-' as well as '--'
|
496 |
|
|
</dl></dd>
|
497 |
|
|
</dl>
|
498 |
|
|
<a name="methods"></a>
|
499 |
|
|
<h2>
|
500 |
|
|
<img src="images/methods.gif" width=151 height=38 alt="Methods">
|
501 |
|
|
</h2>
|
502 |
|
|
<a name="setOptstring(java.lang.String)"><img src="images/red-ball.gif" width=12 height=12 alt=" o "></a>
|
503 |
|
|
<a name="setOptstring"><b>setOptstring</b></a>
|
504 |
|
|
<pre>
|
505 |
|
|
public void setOptstring(String optstring)
|
506 |
|
|
</pre>
|
507 |
|
|
<dl>
|
508 |
|
|
<dd> In GNU getopt, it is possible to change the string containg valid options
|
509 |
|
|
on the fly because it is passed as an argument to getopt() each time. In
|
510 |
|
|
this version we do not pass the string on every call. In order to allow
|
511 |
|
|
dynamic option string changing, this method is provided.
|
512 |
|
|
<p>
|
513 |
|
|
<dd><dl>
|
514 |
|
|
<dt> <b>Parameters:</b>
|
515 |
|
|
<dd> optstring - The new option string to use
|
516 |
|
|
</dl></dd>
|
517 |
|
|
</dl>
|
518 |
|
|
<a name="getOptind()"><img src="images/red-ball.gif" width=12 height=12 alt=" o "></a>
|
519 |
|
|
<a name="getOptind"><b>getOptind</b></a>
|
520 |
|
|
<pre>
|
521 |
|
|
public int getOptind()
|
522 |
|
|
</pre>
|
523 |
|
|
<dl>
|
524 |
|
|
<dd> optind it the index in ARGV of the next element to be scanned.
|
525 |
|
|
This is used for communication to and from the caller
|
526 |
|
|
and for communication between successive calls to `getopt'.
|
527 |
|
|
When `getopt' returns -1, this is the index of the first of the
|
528 |
|
|
non-option elements that the caller should itself scan.
|
529 |
|
|
Otherwise, `optind' communicates from one call to the next
|
530 |
|
|
how much of ARGV has been scanned so far.
|
531 |
|
|
<p>
|
532 |
|
|
</dl>
|
533 |
|
|
<a name="setOptind(int)"><img src="images/red-ball.gif" width=12 height=12 alt=" o "></a>
|
534 |
|
|
<a name="setOptind"><b>setOptind</b></a>
|
535 |
|
|
<pre>
|
536 |
|
|
public void setOptind(int optind)
|
537 |
|
|
</pre>
|
538 |
|
|
<dl>
|
539 |
|
|
<dd> This method allows the optind index to be set manually. Normally this
|
540 |
|
|
is not necessary (and incorrect usage of this method can lead to serious
|
541 |
|
|
lossage), but optind is a public symbol in GNU getopt, so this method
|
542 |
|
|
was added to allow it to be modified by the caller if desired.
|
543 |
|
|
<p>
|
544 |
|
|
<dd><dl>
|
545 |
|
|
<dt> <b>Parameters:</b>
|
546 |
|
|
<dd> optind - The new value of optind
|
547 |
|
|
</dl></dd>
|
548 |
|
|
</dl>
|
549 |
|
|
<a name="setArgv(java.lang.String[])"><img src="images/red-ball.gif" width=12 height=12 alt=" o "></a>
|
550 |
|
|
<a name="setArgv"><b>setArgv</b></a>
|
551 |
|
|
<pre>
|
552 |
|
|
public void setArgv(String argv[])
|
553 |
|
|
</pre>
|
554 |
|
|
<dl>
|
555 |
|
|
<dd> Since in GNU getopt() the argument vector is passed back in to the
|
556 |
|
|
function every time, the caller can swap out argv on the fly. Since
|
557 |
|
|
passing argv is not required in the Java version, this method allows
|
558 |
|
|
the user to override argv. Note that incorrect use of this method can
|
559 |
|
|
lead to serious lossage.
|
560 |
|
|
<p>
|
561 |
|
|
<dd><dl>
|
562 |
|
|
<dt> <b>Parameters:</b>
|
563 |
|
|
<dd> argv - New argument list
|
564 |
|
|
</dl></dd>
|
565 |
|
|
</dl>
|
566 |
|
|
<a name="getOptarg()"><img src="images/red-ball.gif" width=12 height=12 alt=" o "></a>
|
567 |
|
|
<a name="getOptarg"><b>getOptarg</b></a>
|
568 |
|
|
<pre>
|
569 |
|
|
public String getOptarg()
|
570 |
|
|
</pre>
|
571 |
|
|
<dl>
|
572 |
|
|
<dd> For communication from `getopt' to the caller.
|
573 |
|
|
When `getopt' finds an option that takes an argument,
|
574 |
|
|
the argument value is returned here.
|
575 |
|
|
Also, when `ordering' is RETURN_IN_ORDER,
|
576 |
|
|
each non-option ARGV-element is returned here.
|
577 |
|
|
No set method is provided because setting this variable has no effect.
|
578 |
|
|
<p>
|
579 |
|
|
</dl>
|
580 |
|
|
<a name="setOpterr(boolean)"><img src="images/red-ball.gif" width=12 height=12 alt=" o "></a>
|
581 |
|
|
<a name="setOpterr"><b>setOpterr</b></a>
|
582 |
|
|
<pre>
|
583 |
|
|
public void setOpterr(boolean opterr)
|
584 |
|
|
</pre>
|
585 |
|
|
<dl>
|
586 |
|
|
<dd> Normally Getopt will print a message to the standard error when an
|
587 |
|
|
invalid option is encountered. This can be suppressed (or re-enabled)
|
588 |
|
|
by calling this method. There is no get method for this variable
|
589 |
|
|
because if you can't remember the state you set this to, why should I?
|
590 |
|
|
<p>
|
591 |
|
|
</dl>
|
592 |
|
|
<a name="getOptopt()"><img src="images/red-ball.gif" width=12 height=12 alt=" o "></a>
|
593 |
|
|
<a name="getOptopt"><b>getOptopt</b></a>
|
594 |
|
|
<pre>
|
595 |
|
|
public int getOptopt()
|
596 |
|
|
</pre>
|
597 |
|
|
<dl>
|
598 |
|
|
<dd> When getopt() encounters an invalid option, it stores the value of that
|
599 |
|
|
option in optopt which can be retrieved with this method. There is
|
600 |
|
|
no corresponding set method because setting this variable has no effect.
|
601 |
|
|
<p>
|
602 |
|
|
</dl>
|
603 |
|
|
<a name="getLongind()"><img src="images/red-ball.gif" width=12 height=12 alt=" o "></a>
|
604 |
|
|
<a name="getLongind"><b>getLongind</b></a>
|
605 |
|
|
<pre>
|
606 |
|
|
public int getLongind()
|
607 |
|
|
</pre>
|
608 |
|
|
<dl>
|
609 |
|
|
<dd> Returns the index into the array of long options (NOT argv) representing
|
610 |
|
|
the long option that was found.
|
611 |
|
|
<p>
|
612 |
|
|
</dl>
|
613 |
|
|
<a name="getopt()"><img src="images/red-ball.gif" width=12 height=12 alt=" o "></a>
|
614 |
|
|
<a name="getopt"><b>getopt</b></a>
|
615 |
|
|
<pre>
|
616 |
|
|
public int getopt()
|
617 |
|
|
</pre>
|
618 |
|
|
<dl>
|
619 |
|
|
<dd> This method returns a char that is the current option that has been
|
620 |
|
|
parsed from the command line. If the option takes an argument, then
|
621 |
|
|
the internal variable 'optarg' is set which is a String representing
|
622 |
|
|
the the value of the argument. This value can be retrieved by the
|
623 |
|
|
caller using the getOptarg() method. If an invalid option is found,
|
624 |
|
|
an error message is printed and a '?' is returned. The name of the
|
625 |
|
|
invalid option character can be retrieved by calling the getOptopt()
|
626 |
|
|
method. When there are no more options to be scanned, this method
|
627 |
|
|
returns -1. The index of first non-option element in argv can be
|
628 |
|
|
retrieved with the getOptind() method.
|
629 |
|
|
<p>
|
630 |
|
|
<dd><dl>
|
631 |
|
|
<dt> <b>Returns:</b>
|
632 |
|
|
<dd> Various things as described above
|
633 |
|
|
</dl></dd>
|
634 |
|
|
</dl>
|
635 |
|
|
<hr>
|
636 |
|
|
<pre>
|
637 |
|
|
<a href="packages.html">All Packages</a> <a href="tree.html">Class Hierarchy</a> <a href="Package-gnu.getopt.html">This Package</a> <a href="Package-gnu.getopt.html">Previous</a> <a href="Package-gnu.getopt.html">Next</a> <a href="AllNames.html">Index</a></pre>
|
638 |
|
|
</body>
|
639 |
|
|
</html> |