Java Reference
In-Depth Information
*/
public
public
char
char
getopt
(
String argv
[]) {
Debug
.
println
(
"getopt"
,
"optind="
+
optind
+
", argv.length="
+
argv
.
length
);
iif
(
optind
>= (
argv
.
length
) || !
argv
[
optind
].
startsWith
(
"-"
)) {
done
=
true
true
;
}
// If we are finished (either now OR from before), bail.
// Do not collapse this into the "if" above
iif
(
done
) {
return
return
DONE
;
}
optarg
=
null
null
;
// XXX TODO - two-pass, 1st check long args, 2nd check for
// char, to allow advanced usage like "-no outfile" == "-n -o outfile".
// Pick off next command line argument, if it starts "-",
// then look it up in the list of valid args.
String thisArg
=
argv
[
optind
];
iif
(
thisArg
.
startsWith
(
"-"
)) {
for
for
(
GetOptDesc option
:
options
) {
iif
((
thisArg
.
length
() ==
2
&&
option
.
getArgLetter
() ==
thisArg
.
charAt
(
1
)) ||
(
option
.
getArgName
() !=
null
null
&&
option
.
getArgName
().
equals
(
thisArg
.
substring
(
1
)))) {
// found it
// If it needs an option argument, get it.
iif
(
option
.
takesArgument
()) {
iif
(
optind
<
argv
.
length
-
1
) {
optarg
=
argv
[++
optind
];
}
else
else
{
throw
throw new
new
IllegalArgumentException
(
"Option "
+
option
.
getArgLetter
() +
" needs value but found end of arg list"
);
}
}
++
optind
;
return
return
option
.
getArgLetter
();
}
}
// Began with "-" but not matched, so must be error.
++
optind
;