public class CliRunner<T> extends Object
Modifier and Type | Field and Description |
---|---|
protected int |
addHelpExitCode |
protected boolean |
addHelpOption |
protected ClassOptions |
classOptions |
protected ClassOptionsData |
classOptionsData |
protected ClassParserChain |
classParserChain |
protected Class<T> |
configClass |
protected org.slf4j.Logger |
logger |
protected org.apache.commons.cli.CommandLineParser |
parser |
protected boolean |
stopAtNonOption |
protected boolean |
systemExit |
Constructor and Description |
---|
CliRunner(org.apache.commons.cli.CommandLineParser parser,
Class<T> configClass) |
CliRunner(org.apache.commons.cli.CommandLineParser parser,
Class<T> configClass,
org.slf4j.Logger logger,
boolean autoExit)
Convenience constructor for
CliRunner(CommandLineParser, Class) that also calls
setLogger(Logger) and setSystemExit(boolean) with the respective arguments. |
Modifier and Type | Method and Description |
---|---|
protected int |
call(Callable<Integer> target,
org.apache.commons.cli.CommandLine cl)
(Extension point.).
|
protected int |
exit(int status) |
int |
getAddHelpExitCode() |
ClassParserChain |
getClassParserChain() |
protected String |
getCmdLineSyntax(Callable<Integer> target)
(Extension point.).
|
org.slf4j.Logger |
getLogger() |
org.apache.commons.cli.CommandLineParser |
getParser() |
boolean |
isAddHelpOption() |
boolean |
isStopAtNonOption() |
boolean |
isSystemExit() |
protected org.apache.commons.cli.CommandLine |
parse(T config,
Callable<Integer> target,
String[] args,
org.apache.commons.cli.Options options)
Extension point.
|
protected int |
parseException(T config,
Callable<Integer> target,
String[] args,
org.apache.commons.cli.Options options,
org.apache.commons.cli.ParseException pe)
Extension point.
|
int |
run(T config,
Callable<Integer> target,
String... args) |
void |
setAddHelpExitCode(int defaultHelpExitCode) |
void |
setAddHelpOption(boolean addHelpOption) |
void |
setClassParserChain(ClassParserChain classParserChain) |
void |
setLogger(org.slf4j.Logger logger) |
void |
setStopAtNonOption(boolean stopAtNonOption) |
void |
setSystemExit(boolean autoExit) |
protected int |
showHelp(Callable<Integer> target,
String header,
org.apache.commons.cli.Options options,
String footer)
(Extension point.).
|
protected final org.apache.commons.cli.CommandLineParser parser
protected final ClassOptions classOptions
protected final ClassOptionsData classOptionsData
protected org.slf4j.Logger logger
protected boolean systemExit
protected boolean stopAtNonOption
protected boolean addHelpOption
protected int addHelpExitCode
protected ClassParserChain classParserChain
public CliRunner(org.apache.commons.cli.CommandLineParser parser, Class<T> configClass)
parser
- Common choices will be an instance of GnuParser
or PosixParser
.
See "[CLI] Why do you use the different parsers"
for some additional details concerning this choice.
(This must be determined by the caller based on the design of the Parameter
s used within
the configuration, and can't be appropriately determined automatically.)
"How to get Apache CLI to handle double-dash?" may also be of some interest.
configClass
- Used to determine options to be read from the command-line.public CliRunner(org.apache.commons.cli.CommandLineParser parser, Class<T> configClass, org.slf4j.Logger logger, boolean autoExit)
Convenience constructor for CliRunner(CommandLineParser, Class)
that also calls
setLogger(Logger)
and setSystemExit(boolean)
with the respective arguments.
public int run(T config, Callable<Integer> target, String... args)
config
- After parsing, the command line options will be set to this instance.target
- The Callable
that will be called once the command line is successfully parsed.
Its result is used as the program's exit / status code.
There is no reason that the same object can't be used for both the configuration and the target - as long as the object class is designed with this in mind. Otherwise, the target will most likely need its own reference to the configuration.
1
" if the default help was requested (which can be overridden with
showHelp(Callable, String, Options, String)
), "254
" if a
ParseException
is generated (which can be overridden with
parseException(Object, Callable, String[], Options, ParseException)
),
or "255
" for any other exception.protected int parseException(T config, Callable<Integer> target, String[] args, org.apache.commons.cli.Options options, org.apache.commons.cli.ParseException pe)
Extension point. The default implementation calls
showHelp(Callable, String, Options, String)
and returns "254
".
protected int call(Callable<Integer> target, org.apache.commons.cli.CommandLine cl) throws Exception
(Extension point.).
Exception
protected org.apache.commons.cli.CommandLine parse(T config, Callable<Integer> target, String[] args, org.apache.commons.cli.Options options) throws org.apache.commons.cli.ParseException
Extension point. Can also be used as an opportunity to modify the Options
used for
the rest of the run(Object, Callable, String...)
method.
org.apache.commons.cli.ParseException
protected int showHelp(Callable<Integer> target, String header, org.apache.commons.cli.Options options, String footer)
(Extension point.).
protected int exit(int status)
public org.apache.commons.cli.CommandLineParser getParser()
public org.slf4j.Logger getLogger()
public void setLogger(org.slf4j.Logger logger)
public boolean isSystemExit()
public void setSystemExit(boolean autoExit)
public boolean isStopAtNonOption()
public void setStopAtNonOption(boolean stopAtNonOption)
public boolean isAddHelpOption()
public void setAddHelpOption(boolean addHelpOption)
public int getAddHelpExitCode()
public void setAddHelpExitCode(int defaultHelpExitCode)
public ClassParserChain getClassParserChain()
public void setClassParserChain(ClassParserChain classParserChain)
Copyright © 2007–2017 Mark A. Ziesemer. All rights reserved.