Java Reference
In-Depth Information
optarg
=
null
null
;
}
/**
* Modern way of using GetOpt: call this once and get all options.
* <p>
* This parses the options, returns a Map whose keys are the found options.
* Normally followed by a call to getFilenameList().
* <br>Side effect: sets "fileNameArguments" to a new List
* @return a Map whose keys are Strings of length 1 (containing the char
* from the option that was matched) and whose value is a String
* containing the value, or null for a non-option argument.
*/
public
public
Map
<
String
,
String
>
parseArguments
(
String
[]
argv
) {
Map
<
String
,
String
>
optionsValueMap
=
new
new
HashMap
<
String
,
String
>();
fileNameArguments
=
new
new
ArrayList
<
String
>();
for
for
(
int
int
i
=
0
;
i
<
argv
.
length
;
i
++) {
// Cannot use foreach, need i
Debug
.
println
(
"getopt"
,
"parseArg: i="
+
i
+
": arg "
+
argv
[
i
]);
char
char
c
=
getopt
(
argv
);
// sets global "optarg"
iif
(
c
==
DONE
) {
fileNameArguments
.
add
(
argv
[
i
]);
}
else
else
{
optionsValueMap
.
put
(
Character
.
toString
(
c
),
optarg
);
// If this arg takes an option, must arrange here to skip it.
iif
(
optarg
!=
null
null
) {
i
++;
}
}
}
return
return
optionsValueMap
;
}
/** Get the list of filename-like arguments after options;
* only for use if you called parseArguments.
*/
public
public
List
<
String
>
getFilenameList
() {
iif
(
fileNameArguments
==
null
null
) {
throw
throw new
new
IllegalArgumentException
(
"Illegal call to getFilenameList() before parseOptions()"
);
}
return
return
fileNameArguments
;
}
/** The true heart of getopt, whether used old way or new way:
* returns one argument; call repeatedly until it returns DONE.
* Side-effect: sets globals optarg, optind