|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.knowceans.util.Arguments
public class Arguments
Arguments is a convenience class that reads command line arguments for main and validates them against a given format. The general grammar of a commandline is:
commandline ::= command " " options arguments
Here the options and arguments follow the grammar:
options ::= ( option " " )+ option ::= "-" name (" " value)? name ::= <LITERAL> arguments ::= ( argument )+ argument ::= value value ::= <LITERAL> | """( <LITERAL> | " " )+ """with some value. Values can be restricted to specific formats, which is done using a format string. Known types are int, float, long, double, boolean, String, File and URL. String and File values can contain spaces if put within quotes.
TODO: resolve problem with command line monitor getCallString()
Constructor Summary | |
---|---|
Arguments(java.lang.String optformat,
java.lang.String argtypes)
Initialise the arguments parser with a format string and an argument type specification. |
|
Arguments(java.lang.String optformat,
java.lang.String argtypes,
java.lang.String helptext)
same as 2-argument constructor, but sets a help text. |
Method Summary | |
---|---|
void |
close()
This method must be called if output redirection is used. |
void |
debug(boolean b)
|
java.lang.Object |
getArgument(int i)
Returns the argument with index i and null if i is compliant with the format but not specified at commandline. |
java.lang.Object |
getArgument(int i,
java.lang.Object defaultValue)
Same as getArgument, but returns a default value if optional argument is not set. |
java.util.Vector<java.lang.Object> |
getArguments()
get the vector of all arguments. |
java.lang.String |
getCallString(boolean format)
The full call string of the program, including the classpath and working directory. |
java.lang.Object |
getOption(java.lang.String string)
returns the named option value. |
java.lang.Object |
getOption(java.lang.String string,
java.lang.Object defaultValue)
Same as getOption, but allows default value (whose type is NOT checked). |
java.util.HashMap<java.lang.String,java.lang.Object> |
getOptions()
get the map of options. |
void |
help(java.lang.String string)
|
static void |
main(java.lang.String[] args)
|
void |
parse(java.lang.String[] args)
Parses the command line arguments string whose values can be found with the getOption and getArgument methods afterwards. |
void |
redirect(boolean b)
Enable / disable redirection of pipe streams (default = disabled) |
void |
spacePad(java.lang.StringBuffer b,
int length)
|
void |
tee(boolean b)
Enable / disable output duplication (default = enabled). |
java.lang.String |
toString()
describe the current arguments set |
java.lang.String |
type(char c)
|
void |
variable(boolean b)
Enable / disable variable replacement (default = disabled). |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public Arguments(java.lang.String optformat, java.lang.String argtypes)
The format string for the options is composed of the following grammar:
foptions ::= ( option )* foption ::= name ( "|" name )? "=" fotype " "? ( "{" fhelp "}" )? fname ::= <LITERAL> fotype ::= ( i | l | f | d | b | u | p | s | 0 ) fhelp ::= <LITERAL>The literals of fotype correspond to the types int, float, long, double, boolean, java.net.URL, java.io.File (p), java.lang.String, and void (0) for unparametrised options
The format string for the arguments is composed of the following grammar:
farguments ::= frequiredargs "|" foptionalargs frequiredargs ::= ( fatype " "? ( "{" fhelp "}" )? )+ foptionalargs ::= ( fatype " "? ( "{" fhelp "}" )? )+)+ fatype ::= ( i | l | f | d | b | u | p | s ) fhelp ::= <LITERAL>The help strings can include line breaks "\n".
optformat
- format string for optionsargtypes
- format string for argumentspublic Arguments(java.lang.String optformat, java.lang.String argtypes, java.lang.String helptext)
optformat
- argtypes
- helptext
- Method Detail |
---|
public static void main(java.lang.String[] args)
public java.util.HashMap<java.lang.String,java.lang.Object> getOptions()
public java.lang.Object getOption(java.lang.String string) throws java.lang.IllegalArgumentException
string
- key for the option parameter.
java.lang.IllegalArgumentException
public java.lang.Object getOption(java.lang.String string, java.lang.Object defaultValue) throws java.lang.IllegalArgumentException
string
- defaultValue
-
java.lang.IllegalArgumentException
public java.util.Vector<java.lang.Object> getArguments()
public java.lang.Object getArgument(int i) throws java.lang.IllegalArgumentException
i
-
java.lang.IllegalArgumentException
public java.lang.String getCallString(boolean format)
format
- true to format lines.
public java.lang.Object getArgument(int i, java.lang.Object defaultValue)
i
- defaultValue
-
public void parse(java.lang.String[] args) throws java.lang.IllegalArgumentException
Another important possibility is the option -$, which allows to set a variable that can be used afterwards in the option and argument values by using $@. Enable this using the variable(true);
args
- the argument string, typically directly that of a main
method.
java.lang.IllegalArgumentException
- if the commandline arguments do not
match the given format.public java.lang.String toString()
toString
in class java.lang.Object
public void spacePad(java.lang.StringBuffer b, int length)
public java.lang.String type(char c)
public void debug(boolean b)
b
- public void help(java.lang.String string)
string
- public void close()
public void redirect(boolean b)
b
- public void variable(boolean b)
b
- public void tee(boolean b)
b
-
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |