public class CmdlineParser
extends java.lang.Object
The central method to parse a command line is parse(String...).
The command line will be parsed and validated based on configuration objects which are annotated with CmdOption-specific annotations, which are:
Each parsed option will be directly applied to the corresponding method or
field. The configuration objects are typically provided as constructor
arguments, but it is also possible to use the addObject(Object...)
method to add additional configuration objects.
| Modifier | Constructor and Description |
|---|---|
protected |
CmdlineParser(CmdlineParser parent,
java.lang.String commandName,
java.lang.Object commandObject)
The constructor is only intended for internal use.
|
|
CmdlineParser(java.lang.Object... objects)
Create a new commandline parser instance and scan all given object for
supported options, parameters and commands using the pre-registered default
handlers.
|
| Modifier and Type | Method and Description |
|---|---|
protected boolean |
addCommand(java.lang.Object object) |
void |
addObject(java.lang.Object... objects)
Add an additional configuration object containing CmdOption-specific
annotations to the configuration.
|
protected void |
addOptions(java.lang.Object object) |
void |
commandUsage(java.lang.Class<?> command) |
protected boolean |
containsMethod(java.lang.Iterable<java.lang.reflect.Method> methods,
java.lang.reflect.Method method) |
java.util.List<CmdOptionHandler> |
defaultHandlers() |
protected CmdOptionHandler |
findHandler(java.lang.reflect.AccessibleObject element,
int argsCount,
java.lang.Class<? extends CmdOptionHandler> cmdOptionHandlerType)
Find a
CmdOptionHandler for the given element, argument count and
requested handler type. |
protected java.lang.reflect.Method |
findMethod(java.lang.Iterable<java.lang.reflect.Method> methods,
java.lang.reflect.Method method) |
CmdlineModel |
getCmdlineModel() |
java.lang.String |
getParsedCommandName() |
java.lang.Object |
getParsedCommandObject() |
protected java.util.List<OptionHandle> |
inspectElements(java.lang.Object object,
java.util.Set<java.lang.reflect.AccessibleObject> elements) |
protected boolean |
isPackagePrivate(java.lang.reflect.Method method) |
protected boolean |
isPrivate(java.lang.reflect.Method method) |
protected boolean |
isPublicOrProtected(java.lang.reflect.Method method) |
protected boolean |
isVisible(java.lang.Class<?> baseClass,
java.lang.reflect.Member element) |
void |
parse(boolean dryrun,
boolean detectHelpAndSkipValidation,
java.lang.String... cmdline)
Parses the given commandline arguments.
|
void |
parse(java.lang.String... cmdline) |
void |
registerHandler(CmdOptionHandler handler)
Register a new CmdOptionHandler.
|
void |
setAboutLine(java.lang.String aboutLine)
An additional text displayed at the top of the usage/help display.
|
void |
setAggregateShortOptionsWithPrefix(java.lang.String prefix)
Set the prefix of short options, that should be aggregated.
|
void |
setDebugMode(boolean debugMode)
Programmatically enable or disable the debug mode.
|
void |
setDebugModeAllowed(boolean debugAllowed)
Allow or disallow the recognition of a request of the debug mode via the
special command line option --CMDOPTION_DEBUG.
|
void |
setDefaultCommandClass(java.lang.Class<?> defaultCommandClass) |
void |
setDefaultCommandName(java.lang.String defaultCommandName) |
void |
setProgramName(java.lang.String programName)
Set the name of the program is usually called on the command line.
|
void |
setReadArgsFromFilePrefix(java.lang.String prefix)
Set the argument prefix used to mark a cmdline argument as file which
contains more commandline parameters.
|
void |
setResourceBundle(java.util.ResourceBundle resourceBundle) |
void |
setResourceBundle(java.lang.String resourceBundleName,
java.lang.ClassLoader classloader) |
void |
setResourceBundle(java.lang.String resourceBundleName,
java.util.Locale locale,
java.lang.ClassLoader classloader) |
void |
setShortOptionWithArgsPrefix(java.lang.String prefix)
If used to set a non-empty string, this enables the feature to give the argument of a short options without a space.
|
void |
setStopAcceptOptionsAfterParameterIsSet(boolean stopAcceptOptionAfterParameterIsSet)
If set to
true, the parser will no longer parse options once it has seen a parameter. |
void |
setUsageFormatter(UsageFormatter2 usageFormatter) |
void |
unregisterAllHandler() |
void |
unregisterHandler(java.lang.Class<? extends CmdOptionHandler> type) |
void |
usage() |
void |
usage(java.io.PrintStream output) |
void |
usage(java.lang.StringBuilder output)
Deprecated.
Use
usage(PrintStream) instead. |
java.lang.String |
usageString() |
void |
validate()
Check validity of the given configutaion classes.
|
protected void |
validateOptions()
Do a consistency check for the given cmdoption model (all annotated opitons).
|
protected CmdlineParser(CmdlineParser parent, java.lang.String commandName, java.lang.Object commandObject)
parent - The parent parser.commandName - The command name.commandObject - The target object holding the parsed options.public CmdlineParser(java.lang.Object... objects)
addObject(Object...) method after you registered the desired set of
handlers.objects - The configuration objects containing supported annotations.public java.util.List<CmdOptionHandler> defaultHandlers()
public void setDebugMode(boolean debugMode)
public void setDebugModeAllowed(boolean debugAllowed)
public void setUsageFormatter(UsageFormatter2 usageFormatter)
public void setStopAcceptOptionsAfterParameterIsSet(boolean stopAcceptOptionAfterParameterIsSet)
true, the parser will no longer parse options once it has seen a parameter.stopAcceptOptionAfterParameterIsSet - public void setDefaultCommandName(java.lang.String defaultCommandName)
public void setDefaultCommandClass(java.lang.Class<?> defaultCommandClass)
public void parse(java.lang.String... cmdline)
public void parse(boolean dryrun,
boolean detectHelpAndSkipValidation,
java.lang.String... cmdline)
If no error were detected and not in dryrun-mode, the result is applied to the config object(s).
If any errors where detected, they will be thrown as
CmdlineParserException.
dryrun - If true, only checks for errors.detectHelpAndSkipValidation - If true, the given cmdline is first checked for applied help
options. In such a case, no other validation errors will be
thrown.cmdline - The commandline argument to be parsed.CmdlineParserException - If any errors were detected.public java.lang.String getParsedCommandName()
public java.lang.Object getParsedCommandObject()
protected CmdOptionHandler findHandler(java.lang.reflect.AccessibleObject element, int argsCount, java.lang.Class<? extends CmdOptionHandler> cmdOptionHandlerType)
CmdOptionHandler for the given element, argument count and
requested handler type.CmdOptionHandler or null.CmdlineParserException - in case an error occured.public void addObject(java.lang.Object... objects)
Classed annotated with CmdCommand are registered as commands, and all
found options and parameters are registered to the command.
objects - CmdlineParserException - if the given objects contain configutation errors or are
inconsistent.protected boolean addCommand(java.lang.Object object)
public void validate()
CmdlineParserException - if the configutation is not valid.protected void validateOptions()
CmdlineParserException - if the configutation is not valid.protected boolean isVisible(java.lang.Class<?> baseClass,
java.lang.reflect.Member element)
protected boolean isPublicOrProtected(java.lang.reflect.Method method)
protected boolean isPackagePrivate(java.lang.reflect.Method method)
protected boolean isPrivate(java.lang.reflect.Method method)
protected boolean containsMethod(java.lang.Iterable<java.lang.reflect.Method> methods,
java.lang.reflect.Method method)
protected java.lang.reflect.Method findMethod(java.lang.Iterable<java.lang.reflect.Method> methods,
java.lang.reflect.Method method)
protected void addOptions(java.lang.Object object)
protected java.util.List<OptionHandle> inspectElements(java.lang.Object object, java.util.Set<java.lang.reflect.AccessibleObject> elements)
public void unregisterAllHandler()
public void unregisterHandler(java.lang.Class<? extends CmdOptionHandler> type)
public void registerHandler(CmdOptionHandler handler)
addObject(Object...).public void commandUsage(java.lang.Class<?> command)
public void usage()
@Deprecated public void usage(java.lang.StringBuilder output)
usage(PrintStream) instead.output - public void usage(java.io.PrintStream output)
public java.lang.String usageString()
public CmdlineModel getCmdlineModel()
public void setProgramName(java.lang.String programName)
public void setAboutLine(java.lang.String aboutLine)
public void setResourceBundle(java.lang.String resourceBundleName,
java.lang.ClassLoader classloader)
public void setResourceBundle(java.lang.String resourceBundleName,
java.util.Locale locale,
java.lang.ClassLoader classloader)
public void setResourceBundle(java.util.ResourceBundle resourceBundle)
public void setReadArgsFromFilePrefix(java.lang.String prefix)
"@" sign. You can also disable this feature by setting
null or the empty string.
The file contains additional arguments, each one on a new line.
prefix - The prefix to mark an argument as arguments-file or
null to disable the feature. Typically a "@"public void setAggregateShortOptionsWithPrefix(java.lang.String prefix)
prefix - The prefix, typically a "-".
If null or the empty string, this short option aggregation is disabledpublic void setShortOptionWithArgsPrefix(java.lang.String prefix)
prefix - The prefix denoting a short options that accepts its argument without a space, typically a "-"