public abstract class PicocliBaseScript extends Script
Base script class that provides picocli declarative (annotation-based) command line argument processing for Groovy scripts.
Scripts may install this base script via the PicocliScript
annotation or via the standard Groovy
@groovy.transform.BaseScript(picocli.groovy.PicocliBaseScript)
annotation, but
the @PicocliScript
annotation is preferred since it enables scripts to use the
@Command
annotation. Example usage:
@Command(name = "myCommand", description = "does something special") @PicocliScript import picocli.groovy.PicocliScript import picocli.CommandLine.Command ...
Before the script body is executed,
the PicocliBaseScript
base class parses the command line and initializes @Field
variables
annotated with @Option
or @Parameters
.
It also takes care of error handling and common use cases like requests for usage help.
See the run()
method for a detailed break-down of the steps the base class takes
before the statements in the script body are executed.
Modifier and Type | Field and Description |
---|---|
static String |
COMMAND_LINE
Name of the property that holds the CommandLine instance for this script ("commandLine").
|
Constructor and Description |
---|
PicocliBaseScript() |
Modifier and Type | Method and Description |
---|---|
CommandLine |
createCommandLine()
Create and returns a new CommandLine instance.
|
protected CommandLine |
getOrCreateCommandLine()
Return the CommandLine for this script.
|
String[] |
getScriptArguments()
Return the script arguments as an array of strings.
|
Object |
handleExecutionException(CommandLine commandLine,
String[] args,
Exception ex)
If an Exception occurs during
runRunnableSubcommand(List) , or runScriptBody()
then this gets called to report the problem. |
Object |
handleParameterException(CommandLine.ParameterException pe,
String[] args)
If a ParameterException occurs during
parseScriptArguments(CommandLine, String[])
then this method gets called to report the problem. |
List<CommandLine> |
parseScriptArguments(CommandLine commandLine,
String[] args)
Returns the result of calling
CommandLine.parse(String...) with the given arguments. |
void |
printErrorMessage(String message)
Error messages that arise from command line processing call this.
|
Object |
printHelpMessage(CommandLine commandLine)
If an @Option whose
usageHelp attribute is annotated as true appears in the arguments. |
Object |
printHelpMessage(CommandLine commandLine,
PrintStream stream)
If an @Option whose
usageHelp attribute is annotated as true appears in the arguments. |
Object |
printVersionHelpMessage(CommandLine commandLine)
If an @Option whose
versionHelp attribute is annotated as true appears in the arguments. |
Object |
run()
Parses the command line and initializes
@Field variables
annotated with @Option or @Parameters before executing the script body. |
void |
runRunnableSubcommand(List<CommandLine> parsedCommands)
If the most specific subcommand (the last
CommandLine object in the list) implements Runnable or Callable,
then run it. |
protected abstract Object |
runScriptBody()
The script body.
|
evaluate, evaluate, getBinding, getProperty, invokeMethod, print, printf, printf, println, println, run, setBinding, setProperty
getMetaClass, setMetaClass
public static final String COMMAND_LINE
protected abstract Object runScriptBody()
public Object run()
Parses the command line and initializes @Field
variables
annotated with @Option
or @Parameters
before executing the script body.
Also takes care of error handling and common use cases like requests for usage help.
Here is a break-down of the steps the base class takes before the statements in the script body are executed:
CommandLine
is created with this script instance as the annotated command object.
The CommandLine
instance is cached in the commandLine
property
(so it can be referred to in script code with
this.commandLine
). CommandLine
creation and initialization may be
customized by overriding createCommandLine()
.parseScriptArguments(CommandLine, String[])
method is called with the script arguments.
This initialises all @Field
variables annotated with CommandLine.Option
or
CommandLine.Parameters
, unless the user input was invalid.handleParameterException(CommandLine.ParameterException, String[])
.Runnable
or Callable
, its run
(or call
) method
is called. The script may support subcommands. In that case only the last specified subcommand is run or called
if it implements Runnable
or Callable
. This may be customized by overriding
runRunnableSubcommand(List)
.public String[] getScriptArguments()
protected CommandLine getOrCreateCommandLine()
createCommandLine()
.public CommandLine createCommandLine()
@Command(name = "...")
annotation).
Subclasses may override to register custom type converters or programmatically add subcommands.
public List<CommandLine> parseScriptArguments(CommandLine commandLine, String[] args)
CommandLine.parse(String...)
with the given arguments.
Subclasses may override if any action should be taken immediately before or after parsing.
commandLine
- The CommandLine instance for this script instance.args
- The argument array.CommandLine
objects that result from parsing the user inputpublic void runRunnableSubcommand(List<CommandLine> parsedCommands) throws Exception
CommandLine
object in the list) implements Runnable or Callable,
then run it.
This method will not run the main script runScriptBody()
method; that will be called from {@link #run()}
.parsedCommands
- the list of CommandLine
objects returns from the CommandLine.parse
methodException
- if the Callable throws an exceptionpublic void printErrorMessage(String message)
message
- the error message to printpublic Object handleParameterException(CommandLine.ParameterException pe, String[] args)
parseScriptArguments(CommandLine, String[])
then this method gets called to report the problem.
The default behavior is to show the exception message using printErrorMessage(String)
,
then call printHelpMessage(CommandLine)
.
The return value becomes the return value for the Script.run which will be the exit code
if we've been called from the command line.pe
- The ParameterException that occurredargs
- The argument arraypublic Object handleExecutionException(CommandLine commandLine, String[] args, Exception ex)
runRunnableSubcommand(List)
, or runScriptBody()
then this gets called to report the problem.
The default behavior is to throw a new ExecutionException
wrapping the specified exception.commandLine
- The CommandLine instanceargs
- The argument arrayex
- The Exception that occurredCommandLine.ExecutionException
- wrapping the specified exception by defaultpublic Object printHelpMessage(CommandLine commandLine)
usageHelp
attribute is annotated as true appears in the arguments.
then the script body is not run and this printHelpMessage
method is called instead.
The default behavior is to print the CommandLine.usage(PrintStream)
to System.err
.
The return value becomes the return value for the Script.run which will be the exit code
if we've been called from the command line.commandLine
- The CommandLine instancenull
by default).public Object printHelpMessage(CommandLine commandLine, PrintStream stream)
usageHelp
attribute is annotated as true appears in the arguments.
then the script body is not run and this printHelpMessage
method is called instead.
The default behavior is to print the CommandLine.usage(PrintStream)
to the specified stream.
The return value becomes the return value for the Script.run which will be the exit code
if we've been called from the command line.commandLine
- The CommandLine instancestream
- the stream to print the usage help message tonull
by default).public Object printVersionHelpMessage(CommandLine commandLine)
versionHelp
attribute is annotated as true appears in the arguments.
then the script body is not run and this printVersionHelpMessage method is called instead.
The default behavior is to print the CommandLine.printVersionHelp(PrintStream)
to System.out
.
The return value becomes the return value for the Script.run which will be the exit code
if we've been called from the command line.commandLine
- The CommandLine instancenull
by default).