Class PicocliRunner
java.lang.Object
io.micronaut.configuration.picocli.PicocliRunner
Utility class with convenience methods for running picocli-based commands with
a micronaut application context.
- Since:
- 1.0
- Author:
- Remko Popma
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <C extends Callable<T>,T>
TObtains an instance of the specifiedCallablecommand 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@Optionand@Parametersannotations, and finally calls the command and returns the result.static <C extends Callable<T>,T>
TInstantiates a newApplicationContextfor theEnvironment.CLIenvironment, obtains an instance of the specifiedCallablecommand class from the context, injecting any beans as required, then parses the specified command line arguments, populating fields and methods annotated with picocli@Optionand@Parametersannotations, and finally calls the command and returns the result.static intInstantiates a newApplicationContextfor theEnvironment.CLIenvironment, obtains an instance of the specifiedCallableorRunnablecommand class from the context, injecting any beans as required, then parses the specified command line arguments, populating fields and methods annotated with picocli@Optionand@Parametersannotations, and finallyexecutesthe command and returns the resulting exit code.static intexecute(Class<?> clazz, Collection<String> environments, String... args) Same thanexecute(Class, String...), but with a list of customEnvironments to be applied.static <R extends Runnable>
voidObtains an instance of the specifiedRunnablecommand 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@Optionand@Parametersannotations, and finally runs the command.static <R extends Runnable>
voidInstantiates a newApplicationContextfor theEnvironment.CLIenvironment, obtains an instance of the specifiedRunnablecommand class from the context, injecting any beans as required, then parses the specified command line arguments, populating fields and methods annotated with picocli@Optionand@Parametersannotations, and finally runs the command.
-
Constructor Details
-
PicocliRunner
public PicocliRunner()
-
-
Method Details
-
call
Instantiates a newApplicationContextfor theEnvironment.CLIenvironment, obtains an instance of the specifiedCallablecommand class from the context, injecting any beans as required, then parses the specified command line arguments, populating fields and methods annotated with picocli@Optionand@Parametersannotations, and finally calls the command and returns the result.The
ApplicationContextis closed before this method returns.- Type Parameters:
C- The callable typeT- The callable return type- Parameters:
cls- the Callable command classargs- the command line arguments- Returns:
nullif an error occurred while parsing the command line options, or if help was requested and printed. Otherwise returns the result of calling the Callable
-
call
public static <C extends Callable<T>,T> T call(Class<C> cls, io.micronaut.context.ApplicationContext ctx, String... args) Obtains an instance of the specifiedCallablecommand 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@Optionand@Parametersannotations, and finally calls the command and returns the result.The caller is responsible for closing the context.
- Type Parameters:
C- The callable typeT- The callable return type- Parameters:
cls- the Callable command classctx- the ApplicationContext that injects dependencies into the commandargs- the command line arguments- Returns:
nullif an error occurred while parsing the command line options, or if help was requested and printed. Otherwise returns the result of calling the Callable- Throws:
picocli.CommandLine.InitializationException- if the specified command object does not have aCommandLine.Command,CommandLine.OptionorCommandLine.Parametersannotationpicocli.CommandLine.ExecutionException- if the Callable throws an exception
-
run
Instantiates a newApplicationContextfor theEnvironment.CLIenvironment, obtains an instance of the specifiedRunnablecommand class from the context, injecting any beans as required, then parses the specified command line arguments, populating fields and methods annotated with picocli@Optionand@Parametersannotations, and finally runs the command.The
ApplicationContextis closed before this method returns.- Type Parameters:
R- The runnable type- Parameters:
cls- the Runnable command classargs- the command line arguments
-
run
public static <R extends Runnable> void run(Class<R> cls, io.micronaut.context.ApplicationContext ctx, String... args) Obtains an instance of the specifiedRunnablecommand 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@Optionand@Parametersannotations, and finally runs the command.The caller is responsible for closing the context.
- Type Parameters:
R- The runnable type- Parameters:
cls- the Runnable command classctx- the ApplicationContext that injects dependencies into the commandargs- the command line arguments- Throws:
picocli.CommandLine.InitializationException- if the specified command object does not have aCommandLine.Command,CommandLine.OptionorCommandLine.Parametersannotationpicocli.CommandLine.ExecutionException- if the Runnable throws an exception
-
execute
Instantiates a newApplicationContextfor theEnvironment.CLIenvironment, obtains an instance of the specifiedCallableorRunnablecommand class from the context, injecting any beans as required, then parses the specified command line arguments, populating fields and methods annotated with picocli@Optionand@Parametersannotations, and finallyexecutesthe command and returns the resulting exit code.The
ApplicationContextis 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
CommandLineinstance 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); }- Parameters:
clazz- the Runnable or Callable command classargs- the command line arguments- Returns:
- the exit code returned by
CommandLine.execute(String...)
-
execute
Same thanexecute(Class, String...), but with a list of customEnvironments to be applied.- Parameters:
clazz- the Runnable or Callable command classenvironments- List of environments to be applied when buildingApplicationContextargs- the command line arguments- Returns:
- the exit code returned by
CommandLine.execute(String...) - See Also:
-