Class GetOpt
OVERVIEW:
GetOpt provides a general means for a Java program to parse command
line arguments in accordance with the standard Unix conventions; it is
analogous to, and based on, getopt(3) for C programs. (The following
documentation is based on the man page for getopt(3).)
DESCRIPTION:
GetOpt is a Java class that provides one method, getopt, and some variables that
control behavior of or return additional information from getopt. GetOpt
interprets command arguments in accordance with the standard Unix
conventions:
option arguments of a command are introduced by "-" followed by
a key character, and a non-option argument terminates the processing of
options. GetOpt's option interpretation is controlled by its parameter
optString, which specifies what characters designate legal options and which
of them require associated values. The getopt method returns the next, moving
left to right, option letter in the command line arguments that matches a
letter in optString. optString must contain the option letters the command
using getopt will recognize. For example, getopt("ab") specifies that the
command line should contain no options, only "-a", only "-b", or both "-a"
and "-b" in either order. (The command line can also contain non-option
arguments after any option arguments.) Multiple options per argument are
allowed, e.g., "-ab" for the last case above. If a letter in optString is
followed by a colon, the option is expected to have an argument. The argument
may or may not be separated by whitespace from the option letter. For
example, getopt("w:") allows either "-w 80" or "-w80". The variable optArg is
set to the option argument, e.g., "80" in either of the previous examples.
Conversion functions such as Integer.parseInt(), etc., can then be applied to
optArg. getopt places in the variable optIndex the index of the next command
line argument to be processed; optIndex is automatically initialized to 1
before the first call to getopt. When all options have been processed (that
is, up to the first non-option argument), getopt returns optEOF (-1). getopt
recognizes the command line argument "--" (i.e., two dashes) to delimit the
end of the options; getopt returns optEOF and skips "--". Subsequent,
non-option arguments can be retrieved using the String array passed to
main(), beginning with argument number optIndex.
DIAGNOSTICS:
getopt prints an error message on System.stderr and returns a question mark ('?') when it
encounters an option letter in a command line argument that is not included
in optString. Setting the variable optErr to false disables this error
message.
NOTES:
The following notes describe GetOpt's behavior in a few interesting or special cases; these behaviors are consistent with getopt(3)'s
behaviors.
-- A '-' by itself is treated as a non-option argument.
-- If optString is "a:" and the command line arguments are "-a -x", then "-x" is
treated as the argument associated with the "-a".
-- Duplicate command line options are allowed; it is up to user to deal with them as appropriate.
-- A command line option like "-b-" is considered as the two options "b" and "-"
(so "-" should appear in option string); this differs from "-b --".
-- Sun and DEC getopt(3)'s differ w.r.t. how "---" is handled. Sun treats "---" (or
anything starting with "--") the same as "--" DEC treats "---" as two
separate "-" options (so "-" should appear in option string). Java GetOpt
follows the DEC convention.
-- An option `letter' can be a letter, number, or most special character. Like getopt(3), GetOpt disallows a colon as an option
letter.
SAMPLE:
GetOpt go = new GetOpt(args, "Uab:f:h:w:");
go.optErr = true;
int ch = -1;
// process options in command line arguments
boolean usagePrint = false;
// set
int aflg = 0;
// default
boolean bflg = false;
// values
String filename = "out";
// of
int width = 80;
// options
double height = 1;
// here
int opt;
char ch;
while ((opt = go.getopt()) != go.optEOF)
{
ch = (char)opt;
if ( ch == 'U')
{
usagePrint = true;
}
else if ( ch == 'a')
{
aflg++;
}
else if ( ch == 'b')
{
bflg = go.processArg(go.optArgGet(), bflg);
}
else if ( ch == 'f')
{
filename = go.optArgGet();
}
else if ((char) ch == 'h')
{
height = go.processArg(go.optArgGet(), height);
}
else if ((char) ch == 'w')
{
width = go.processArg(go.optArgGet(), width);
}
else
{
System.exit(1);
}
// undefined option
}
// getopt() returns '?'
if (usagePrint)
{
System.out.println("Usage: -a -b bool -f file -h height -w width");
System.exit(0);
}
System.out.println("These are all the command line arguments " +
"before processing with GetOpt:");
for (int i = 0; i < args.length; i++)
{
System.out.print(" " + args[i]);
}
System.out.println();
System.out.println("-U " + usagePrint);
System.out.println("-a " + aflg);
System.out.println("-b " + bflg);
System.out.println("-f " + filename);
System.out.println("-h " + height);
System.out.println("-w " + width);
// process non-option command line arguments
for (int k = go.optIndexGet(); k %lt; args.length; k++)
{
System.out.println("normal argument " + k + " is " + args[k]);
}
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintgetopt()Method Descriptionstatic voidThe main program for the GetOpt classMethod DescriptionintMethod DescriptionbooleanprocessArg(String arg, boolean b) Method DescriptiondoubleprocessArg(String arg, double d) Method DescriptionfloatprocessArg(String arg, float f) Method DescriptionintprocessArg(String arg, int n) Method DescriptionlongprocessArg(String arg, long n) Method DescriptionbooleantryArg(int k, boolean b) Method DescriptiondoubletryArg(int k, double d) Method DescriptionfloattryArg(int k, float f) Method DescriptioninttryArg(int k, int n) Method DescriptionlongtryArg(int k, long n) Method DescriptionMethod Description
-
Field Details
-
optEOF
public static final int optEOFend of arguments indicator- See Also:
-
optErr
public boolean optErruser can toggle this to control printing of error messages
-
-
Constructor Details
-
GetOpt
Create a new GetOpt object- Parameters:
args- Descriptionopts- Description
-
-
Method Details
-
processArg
Method Description- Parameters:
arg- Descriptionn- Description- Returns:
- Description
-
tryArg
public int tryArg(int k, int n) Method Description- Parameters:
k- Descriptionn- Description- Returns:
- Description
-
processArg
Method Description- Parameters:
arg- Descriptionn- Description- Returns:
- Description
-
tryArg
public long tryArg(int k, long n) Method Description- Parameters:
k- Descriptionn- Description- Returns:
- Description
-
processArg
Method Description- Parameters:
arg- Descriptiond- Description- Returns:
- Description
-
tryArg
public double tryArg(int k, double d) Method Description- Parameters:
k- Descriptiond- Description- Returns:
- Description
-
processArg
Method Description- Parameters:
arg- Descriptionf- Description- Returns:
- Description
-
tryArg
public float tryArg(int k, float f) Method Description- Parameters:
k- Descriptionf- Description- Returns:
- Description
-
processArg
Method Description- Parameters:
arg- Descriptionb- Description- Returns:
- Description
-
tryArg
public boolean tryArg(int k, boolean b) Method Description- Parameters:
k- Descriptionb- Description- Returns:
- Description
-
tryArg
Method Description- Parameters:
k- Descriptions- Description- Returns:
- Description
-
optIndexGet
public int optIndexGet()Method Description- Returns:
- Description
-
optArgGet
Method Description- Returns:
- Description
-
getopt
public int getopt()Method Description- Returns:
- Description
-
main
The main program for the GetOpt class- Parameters:
args- The command line arguments
-