|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectde.uni_hamburg.eggink.autojar.Getopt
public class Getopt
Handles program arguments like Unix getopt(). Example:
void main(String[] argv) { int opt; boolean x = false; Getopt getopt = new Getopt(argv, "a:hx"); String argA = ""; // get Options while ((opt = getopt.getOption()) != -1) { switch (opt) { case 'a': argA = getopt.getOptarg(); break; case 'h': help(); break; case 'x': x = true; break; default: System.out.println("wrong option: " + getOptopt()); } } // handle non-option parameters String[] files = getopt.getParms(); for (int i = 0; i < files.length; ++i) doSomethingWith(files[i]); } Legal calls: java Mainclass -hx file1 file2 java Mainclass -xa blurp -h -- file3 Illegal calls: java Mainclass -y f java Mainclass -a The special argument -- denotes the end of the options. All Arguments after this will be considered as non-options, even if starting with -. If any command line argument is of the form "@filename", this file is read and each line is considered a program parameter.
Constructor Summary | |
---|---|
Getopt(String[] argv,
String opts)
Like Getopt(argv, opts, true). |
|
Getopt(String[] argv,
String opts,
boolean opterr)
Constructs a Getopt object using the main() parameter list. |
|
Getopt(String filename,
String opts)
Like Getopt(filename, opts, true) |
|
Getopt(String filename,
String opts,
boolean opterr)
Constructs a Getopt object by reading all arguments from a file. |
Method Summary | |
---|---|
String |
getOptarg()
Returns the current argument or null. |
int |
getOption()
Returns the next option as int value, -1 if no more options are available, '?' if the option is illegal. |
char |
getOptopt()
Returns the unrecognized option character. |
String[] |
getParms()
Returns parameters not handled by getOption() as array of Strings. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public Getopt(String filename, String opts, boolean opterr) throws IOException
filename
- Name of the file to read.opts
- The possible options.opterr
- If true, an error message will be printed if an illegal option character
is found.
IOException
public Getopt(String filename, String opts) throws IOException
IOException
public Getopt(String[] argv, String opts, boolean opterr) throws IOException
argv
- The arguments of main()opts
- The possible options. Each option is a single character.
If followed by a colon, the option given in the command line
must be followed by an argument.opterr
- If true, an error message will be printed if an illegal option character
is encountered.
IOException
public Getopt(String[] argv, String opts) throws IOException
IOException
Method Detail |
---|
public String getOptarg()
public int getOption()
public char getOptopt()
public String[] getParms()
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |