|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.townleyenterprises.command.CommandParser
This class provides support for parsing command-line arguments.
Constructor Summary | |
CommandParser(java.lang.String appName)
The default constructor initializes the parser with the standard '-' and '--' switches for the short and long options. |
|
CommandParser(java.lang.String appName,
java.lang.String argHelp)
This version of the constructor alows a description for the unhandled arguments to be supplied to the parser. |
|
CommandParser(java.lang.String appName,
java.lang.String argHelp,
char sSwitch,
java.lang.String lSwitch)
This version of the constructor allows the client to specify the switch characters to be used for the short and long options. |
|
CommandParser(java.lang.String appName,
java.lang.String argHelp,
char sSwitch,
java.lang.String lSwitch,
java.lang.String endOfArgsMarker)
This version of the constructor allows the client to specify the switch characters to be used for the short and long options. |
Method Summary | |
void |
addCommandListener(CommandListener listener)
This method is used to register a new command listener with the parser. |
void |
addConstraint(OptionConstraint constraint)
This method is used to add an option constraint to the parser. |
void |
enableAutohelp(boolean autohelp,
boolean allowZeroArgs)
This method tells the parser to automatically handle command lines with the help character. |
void |
executeCommands()
This method is used to check all of the command constraints and execute all of the options. |
java.lang.String |
getDescription()
This method is used to retrieve the description of the command listener's options when printing the help message. |
CommandOption[] |
getOptions()
This method is called by the CommandParser to determine all of the arguments that should be handled by the listener. |
java.lang.String[] |
getUnhandledArguments()
This method allows the client of the argument parser to retrieve any unhandled arguments in the argument list. |
void |
help()
This method prints the automatically generated help messages for the registered options. |
void |
optionMatched(CommandOption opt,
java.lang.String arg)
This method is called whenever an arguement registered with the parser is detected in the argument stream. |
void |
parse(java.lang.String[] args)
This is the main parsing function that should be called to trigger the parsing of the command-line arguments registered with the parser. |
void |
removeCommandListener(CommandListener listener)
This method is used to unregister a command listener with the parser. |
void |
removeConstraint(OptionConstraint constraint)
This method is used to remove a constraint from the parser. |
void |
setAbortExecuteOnError(boolean val)
This method is used to configure the command parser to stop executing commands when an unhandled exeception is thrown by an option. |
void |
setExitOnMissingArg(boolean val,
int status)
This method is used to configure the command parser to exit with the specified return code when it encounters arguments with missing required parameters. |
void |
setExtraHelpText(java.lang.String preamble,
java.lang.String postamble)
This method is used to set optional text which can be printed before and after the command option descriptions. |
void |
usage()
This method is used to print the usage summary information. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public CommandParser(java.lang.String appName)
appName
- the name of the applicationpublic CommandParser(java.lang.String appName, java.lang.String argHelp)
appName
- the name of the applicationargHelp
- the help for the additional arguments which
may be supplied to the applicationpublic CommandParser(java.lang.String appName, java.lang.String argHelp, char sSwitch, java.lang.String lSwitch)
appName
- the name of the applicationargHelp
- the help for the additional arguments which
may be supplied to the applicationsSwitch
- the single character option switchlSwitch
- the long option switch
java.lang.RuntimeException
- if a single character is used for the long switchpublic CommandParser(java.lang.String appName, java.lang.String argHelp, char sSwitch, java.lang.String lSwitch, java.lang.String endOfArgsMarker)
--
which conforms to the POSIX
standard.
appName
- the name of the applicationargHelp
- the help for the additional arguments which
may be supplied to the applicationsSwitch
- the single character option switchlSwitch
- the long option switchendOfArgsMarker
- the string marking the end of the
argument list (may be null). Anything after this
string is treated as a leftover argument.
java.lang.RuntimeException
- if a single character is used for the long switchMethod Detail |
public void enableAutohelp(boolean autohelp, boolean allowZeroArgs)
autohelp
- true to use autohelp; false to disableallowZeroArgs
- true to allow commands to have no
arguments; false to require at least one argumentpublic void addCommandListener(CommandListener listener)
listener
- the CommandListener instancepublic void removeCommandListener(CommandListener listener)
listener
- the CommandListener instancepublic void parse(java.lang.String[] args)
args
- the command-line arguments to parsepublic java.lang.String[] getUnhandledArguments()
public void optionMatched(CommandOption opt, java.lang.String arg)
CommandListener
optionMatched
in interface CommandListener
opt
- the CommandOption matched by the parserarg
- the argument (if any) specified for the optionpublic CommandOption[] getOptions()
CommandListener
getOptions
in interface CommandListener
public java.lang.String getDescription()
CommandListener
getDescription
in interface CommandListener
public void help()
public void usage()
public void setExitOnMissingArg(boolean val, int status)
val
- toggles the behaviorstatus
- the exit status to pass to System.exit()public void setAbortExecuteOnError(boolean val)
val
- toggles the behaviorpublic void setExtraHelpText(java.lang.String preamble, java.lang.String postamble)
preamble
- the text to be printed before the option
descriptionspostamble
- the text to be printed after the option
descriptionspublic void executeCommands() throws java.lang.Exception
java.lang.Exception
- if anything bad happenspublic void addConstraint(OptionConstraint constraint)
constraint
- the constraint to checkpublic void removeConstraint(OptionConstraint constraint)
constraint
- the constraint to remove
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |