public class PicocliRunner
extends java.lang.Object
Constructor and Description |
---|
PicocliRunner() |
Modifier and Type | Method and Description |
---|---|
static <C extends java.util.concurrent.Callable<T>,T> |
call(java.lang.Class<C> cls,
io.micronaut.context.ApplicationContext ctx,
java.lang.String... args)
Obtains an instance of the specified
Callable command class from the specified context,
injecting any beans from the specified context as required,
then parses the specified command line arguments, populating fields and methods annotated
with picocli @Option and @Parameters
annotations, and finally calls the command and returns the result. |
static <C extends java.util.concurrent.Callable<T>,T> |
call(java.lang.Class<C> cls,
java.lang.String... args)
Instantiates a new
ApplicationContext for the Environment.CLI environment,
obtains an instance of the specified Callable command class from the context,
injecting any beans as required,
then parses the specified command line arguments, populating fields and methods annotated
with picocli @Option and @Parameters
annotations, and finally calls the command and returns the result. |
static int |
execute(java.lang.Class<?> clazz,
java.lang.String... args)
Instantiates a new
ApplicationContext for the Environment.CLI environment,
obtains an instance of the specified Callable or Runnable command
class from the context, injecting any beans as required,
then parses the specified command line arguments, populating fields and methods annotated
with picocli @Option and @Parameters
annotations, and finally executes the command
and returns the resulting exit code. |
static <R extends java.lang.Runnable> |
run(java.lang.Class<R> cls,
io.micronaut.context.ApplicationContext ctx,
java.lang.String... args)
Obtains an instance of the specified
Runnable command class from the specified context,
injecting any beans from the specified context as required,
then parses the specified command line arguments, populating fields and methods annotated
with picocli @Option and @Parameters
annotations, and finally runs the command. |
static <R extends java.lang.Runnable> |
run(java.lang.Class<R> cls,
java.lang.String... args)
Instantiates a new
ApplicationContext for the Environment.CLI environment,
obtains an instance of the specified Runnable command class from the context,
injecting any beans as required,
then parses the specified command line arguments, populating fields and methods annotated
with picocli @Option and @Parameters
annotations, and finally runs the command. |
public static <C extends java.util.concurrent.Callable<T>,T> T call(java.lang.Class<C> cls, java.lang.String... args)
ApplicationContext
for the Environment.CLI
environment,
obtains an instance of the specified Callable
command class from the context,
injecting any beans as required,
then parses the specified command line arguments, populating fields and methods annotated
with picocli @Option
and @Parameters
annotations, and finally calls the command and returns the result.
The ApplicationContext
is closed before this method returns.
C
- The callable typeT
- The callable return typecls
- the Callable command classargs
- the command line argumentsnull
if an error occurred while parsing the command line options,
or if help was requested and printed. Otherwise returns the result of calling the Callablepublic static <C extends java.util.concurrent.Callable<T>,T> T call(java.lang.Class<C> cls, io.micronaut.context.ApplicationContext ctx, java.lang.String... args)
Callable
command class from the specified context,
injecting any beans from the specified context as required,
then parses the specified command line arguments, populating fields and methods annotated
with picocli @Option
and @Parameters
annotations, and finally calls the command and returns the result.
The caller is responsible for closing the context.
C
- The callable typeT
- The callable return typecls
- the Callable command classctx
- the ApplicationContext that injects dependencies into the commandargs
- the command line argumentsnull
if an error occurred while parsing the command line options,
or if help was requested and printed. Otherwise returns the result of calling the Callablepicocli.CommandLine.InitializationException
- if the specified command object does not have
a CommandLine.Command
, CommandLine.Option
or CommandLine.Parameters
annotationpicocli.CommandLine.ExecutionException
- if the Callable throws an exceptionpublic static <R extends java.lang.Runnable> void run(java.lang.Class<R> cls, java.lang.String... args)
ApplicationContext
for the Environment.CLI
environment,
obtains an instance of the specified Runnable
command class from the context,
injecting any beans as required,
then parses the specified command line arguments, populating fields and methods annotated
with picocli @Option
and @Parameters
annotations, and finally runs the command.
The ApplicationContext
is closed before this method returns.
R
- The runnable typecls
- the Runnable command classargs
- the command line argumentspublic static <R extends java.lang.Runnable> void run(java.lang.Class<R> cls, io.micronaut.context.ApplicationContext ctx, java.lang.String... args)
Runnable
command class from the specified context,
injecting any beans from the specified context as required,
then parses the specified command line arguments, populating fields and methods annotated
with picocli @Option
and @Parameters
annotations, and finally runs the command.
The caller is responsible for closing the context.
R
- The runnable typecls
- the Runnable command classctx
- the ApplicationContext that injects dependencies into the commandargs
- the command line argumentspicocli.CommandLine.InitializationException
- if the specified command object does not have
a CommandLine.Command
, CommandLine.Option
or CommandLine.Parameters
annotationpicocli.CommandLine.ExecutionException
- if the Runnable throws an exceptionpublic static int execute(java.lang.Class<?> clazz, java.lang.String... args)
ApplicationContext
for the Environment.CLI
environment,
obtains an instance of the specified Callable
or Runnable
command
class from the context, injecting any beans as required,
then parses the specified command line arguments, populating fields and methods annotated
with picocli @Option
and @Parameters
annotations, and finally executes
the command
and returns the resulting exit code.
The ApplicationContext
is closed before this method returns.
This is equivalent to:
try (ApplicationContext context = ApplicationContext.builder(clazz, Environment.CLI).start()) {
return new CommandLine(clazz, new MicronautFactory(context)).execute(args);
}
Applications that need to customize picocli behavior by calling one of the setter
methods on the CommandLine
instance should use code like the above instead of this method.
For example:
// example of customizing picocli parser before invoking a command
try (ApplicationContext context = ApplicationContext.builder(clazz, Environment.CLI).start()) {
return new CommandLine(clazz, new MicronautFactory(context)).
setUsageHelpAutoWidth(true).
setCaseInsensitiveEnumValuesAllowed(true).
execute(args);
}
clazz
- the Runnable or Callable command classargs
- the command line argumentsCommandLine.execute(String...)