Java Reference
In-Depth Information
Options: Numeric: false Output: null; Inputs: -M
>
java environ.GetOptDemoNew -n a b c
Options: Numeric: true Output: null; Inputs: a b c
>
java environ.GetOptDemoNew -numeric a b c
Options: Numeric: true Output: null; Inputs: a b c
>
java environ.GetOptDemoNew -numeric -output-file /tmp/foo a b c
Options: Numeric: true Output: /tmp/foo; Inputs: a b c
You can find a longer example exercising all the ins and outs of this version of
GetOpt
in the
online
darwinsys-api
repo under
src/main/test/lang/
. The source code for
GetOpt
itself lives
in
darwinsys-api
under
src/main/java/com/darwinsys/lang/GetOpt.java
, and is shown in
Example 2-2. Source code for GetOpt
// package com.darwinsys.lang;
public
public class
GetOpt
{
/** The List of File Names found after args */
protected
class
GetOpt
protected
List
<
String
>
fileNameArguments
;
/** The set of characters to look for */
protected
protected final
final
GetOptDesc
[]
options
;
/** Where we are in the options */
protected
protected
int
int
optind
=
0
;
/** Public constant for "no more options" */
public
public static
int
DONE
=
0
;
/** Internal flag - whether we are done all the options */
protected
static final
final
int
protected
boolean
false
;
/** The current option argument. */
protected
boolean
done
=
false
protected
String optarg
;
/** Retrieve the current option argument; UNIX variant spelling. */
public
public
String
optarg
() {
return
return
optarg
;
}
/** Retrieve the current option argument; Java variant spelling. */
public
public
String
optArg
() {
return
return
optarg
;
}
/** Construct a GetOpt parser, given the option specifications
* in an array of GetOptDesc objects. This is the preferred constructor.
*/
public
public
GetOpt
(
final
final
GetOptDesc
[]
opt
) {
this
this
.
options
=
opt
.
clone
();
}