|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectbpiwowar.argparser.ArgParser
public class ArgParser
ArgParser is used to parse the command line arguments for a java application program.
Nested Class Summary | |
---|---|
static interface |
ArgParser.TypeCheckerInterface
If a handler implements this interface, it can be used to know if the handler can process a given field |
Constructor Summary | |
---|---|
ArgParser(String synopsisString,
boolean defaultHelp)
Creates an ArgParser with a synopsis string. |
|
ArgParser(String synopsisString,
Object... objects)
Creates an ArgParser with a synopsis string. |
Method Summary | |
---|---|
void |
activate(String groupId)
|
void |
addOption(bpiwowar.argparser.Option option)
Add an option |
ArgParser |
addOptions(Object object)
Add options from an annotated object instance, disregarding the superclasses |
void |
addOptions(Object object,
boolean recursive)
Automatically add options for an object |
void |
addOptions(Object object,
boolean recursive,
boolean activated,
String prefix,
String group)
Add options for an object |
void |
addOptions(Object object,
Class<?> objectClass,
boolean defaultActivated,
String prefix,
String defaultGroup)
|
static String |
concatenatePrefixes(String... strings)
Concatenate a list of string with "-" separator |
static void |
debug(org.apache.log4j.Logger logger,
String format,
Object... args)
|
PrintStream |
getDefaultPrintStream()
Returns the default print stream used for outputting help and error information. |
String |
getErrorMessage()
Returns the parser's error message. |
int |
getHelpIndentation()
Gets the indentation used by getHelpMessage . |
String |
getHelpMessage()
Returns a string describing the allowed options in detail. |
boolean |
getHelpOptionsEnabled()
Indicates whether or not help options are enabled. |
String |
getSynopsisString()
Returns the synopsis string used by the parser. |
void |
matchAllArgs(String... args)
Matches arguments within an argument list. |
String[] |
matchAllArgs(String[] args,
EnumSet<ArgParserOption> parseFlags)
Matches a command line |
String[] |
matchAllArgs(String[] args,
int idx,
ArgParserOption... exitFlags)
|
String[] |
matchAllArgs(String[] args,
int idx,
EnumSet<ArgParserOption> exitFlags)
Matches arguments within an argument list and returns those which were not matched. |
int |
matchArg(String[] args,
int idx)
Matches one option starting at a specified location in an argument list. |
static String[] |
prependArgs(File file,
String[] args)
Reads in a set of strings from a file and prepends them to an argument list. |
static String[] |
prependArgs(Reader reader,
String[] args)
Reads in a set of strings from a reader and prepends them to an argument list. |
void |
printErrorAndExit(String format,
Object... args)
Prints an error message, along with a pointer to help options, if available, and causes the program to exit with code 1. |
void |
setDefaultPrintStream(PrintStream stream)
Sets the default print stream used for outputting help and error information. |
protected void |
setError(String msg)
Sets the parser's error message. |
void |
setHelpIndentation(int indent)
Sets the indentation used by getHelpMessage . |
void |
setHelpOptionsEnabled(boolean enable)
Enables or disables help options. |
void |
setSynopsisString(String s)
Sets the synopsis string used by the parser. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public ArgParser(String synopsisString, Object... objects)
ArgParser
with a synopsis string.
synopsisString
- string that briefly describes program usage, for use by
getHelpMessage
.objects
- An optional list of objects to parse. It is possible to delay
parsing and use addOptions(Object) to add one by
one objectspublic ArgParser(String synopsisString, boolean defaultHelp)
ArgParser
with a synopsis string. The help
options -help
and -?
are added if
defaultHelp
is true.
synopsisString
- string that briefly describes program usage, for use by
getHelpMessage
.defaultHelp
- if true, adds the default help optionsgetSynopsisString()
,
getHelpMessage()
Method Detail |
---|
public String getSynopsisString()
It is used in help and error messages.
setSynopsisString(java.lang.String)
,
getHelpMessage()
public void setSynopsisString(String s)
s
- The new synopsis stringgetSynopsisString()
,
getHelpMessage()
public boolean getHelpOptionsEnabled()
setHelpOptionsEnabled(boolean)
,
addOption(bpiwowar.argparser.Option)
public void setHelpOptionsEnabled(boolean enable)
%h
. If help options are enabled, and a
help option is matched, then the string produced by
getHelpMessage
is printed to the default print
stream and the program exits with code 0. Otherwise, arguments which
match help options are ignored.
enable
- enables help options if true
.getHelpOptionsEnabled()
,
addOption(bpiwowar.argparser.Option)
,
setDefaultPrintStream(java.io.PrintStream)
public PrintStream getDefaultPrintStream()
setDefaultPrintStream(java.io.PrintStream)
public void setDefaultPrintStream(PrintStream stream)
stream
- new default print streamgetDefaultPrintStream()
public int getHelpIndentation()
getHelpMessage
.
setHelpIndentation(int)
,
getHelpMessage()
public void setHelpIndentation(int indent)
getHelpMessage
. This
is the number of columns that an option's help information is indented.
If the option's name and value information can fit within this number of
columns, then all information about the option is placed on one line.
Otherwise, the indented help information is placed on a separate line.
indent
- number of indentation columnsgetHelpIndentation()
,
getHelpMessage()
public void addOptions(Object object, boolean recursive) throws InvalidHolderException
object
- The object instance to analyserecursive
- If true, explores the super classes of this object, and the
members annotated ArgumentClass
InvalidHolderException
- If an error occurs while reading the object annotationspublic void addOptions(Object object, boolean recursive, boolean activated, String prefix, String group) throws InvalidHolderException
object
- The object to analyserecursive
- Should we analyse the ancestors of this objectactivated
- prefix
- group
-
InvalidHolderException
public ArgParser addOptions(Object object) throws InvalidHolderException
object
- The object to explore
InvalidHolderException
- if there is an error while parsing annotationspublic void addOption(bpiwowar.argparser.Option option)
option
- The option to addpublic void addOptions(Object object, Class<?> objectClass, boolean defaultActivated, String prefix, String defaultGroup) throws InvalidHolderException
InvalidHolderException
public static String concatenatePrefixes(String... strings)
strings
- An array of strings
public static String[] prependArgs(Reader reader, String[] args) throws IOException
"
. The character #
acts as a comment character,
causing input to the end of the current line to be ignored.
reader
- Reader from which to read the stringsargs
- Initial set of argument values. Can be specified as
null
.
IOException
- if an error occured while reading.public static String[] prependArgs(File file, String[] args) throws IOException
"
. The character #
acts as a comment character,
causing input to the end of the current line to be ignored.
file
- File to be readargs
- Initial set of argument values. Can be specified as
null
.
IOException
- if an error occured while reading the file.protected void setError(String msg)
s
- Error messagepublic void printErrorAndExit(String format, Object... args)
public void matchAllArgs(String... args) throws ArgParserException
In the event of an erroneous or unmatched argument, the method prints a message and exits the program with code 1.
If help options are enabled and one of the arguments matches a help
option, then the result of getHelpMessage
is
printed to the default print stream and the program exits with code 0. If
help options are not enabled, they are ignored.
args
- argument list
ArgParserException
getDefaultPrintStream()
public String[] matchAllArgs(String[] args, EnumSet<ArgParserOption> parseFlags) throws ArgParserException
args
- parseFlags
-
ArgParserException
public String[] matchAllArgs(String[] args, int idx, ArgParserOption... exitFlags) throws ArgParserException
ArgParserException
public String[] matchAllArgs(String[] args, int idx, EnumSet<ArgParserOption> exitFlags) throws ArgParserException
args
specified by idx
, and unmatched arguments are returned in a
String array.
In the event of an erroneous argument, the method either prints a message
and exits the program (if #EXIT_ON_ERROR
is set in
exitFlags
) or terminates the matching and creates a error
message that can be retrieved by getErrorMessage()
.
In the event of an umatched argument, the method will print a message and
exit if #EXIT_ON_UNMATCHED
is set in errorFlags
.
Otherwise, the unmatched argument will be appended to the returned array
of unmatched values, and the matching will continue at the next location.
If help options are enabled and one of the arguments matches a help
option, then the result of getHelpMessage
is
printed to the the default print stream and the program exits with code
0. If help options are not enabled, then they will not be matched.
args
- argument listidx
- starting location in listexitFlags
- conditions causing the program to exit. Should be an or-ed
combination of #ParseFlag.EXIT_ON_ERROR
, #ParseFlag.STOP_FIRST_UNMATCHED
or
#ParseFlag.EXIT_ON_UNMATCHED
.
null
if
all arguments were successfully matched
ArgParserException
StringScanException
ArgParserOption
,
getErrorMessage()
,
getDefaultPrintStream()
public static void debug(org.apache.log4j.Logger logger, String format, Object... args)
public int matchArg(String[] args, int idx) throws ArgParserException
In the event of an erroneous argument, the method throws an
ArgParseException
with an
appropriate error message. This error message can also be retrieved using
getErrorMessage
.
In the event of an umatched argument, the method will return idx
If help options are enabled and the argument matches a help option, then
the result of getHelpMessage
is printed to the
the default print stream and the program exits with code 0. If help
options are not enabled, then they are ignored.
args
- argument listidx
- location in list where match should start
ArgParserException
StringScanException
setDefaultPrintStream(java.io.PrintStream)
,
getHelpOptionsEnabled()
,
getErrorMessage()
,
ArgParser#getUnmatchedArgument
public String getHelpMessage()
public String getErrorMessage()
matchArg
or
matchAllArgs
, and is automatically set to null
at the beginning of these methods.
public void activate(String groupId)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |