Class PicocliRunner

java.lang.Object
io.micronaut.configuration.picocli.PicocliRunner

public class PicocliRunner extends Object
Utility class with convenience methods for running picocli-based commands with a micronaut application context.
Since:
1.0
Author:
Remko Popma
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <C extends Callable<T>, T>
    T
    call(Class<C> cls, io.micronaut.context.ApplicationContext ctx, 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 Callable<T>, T>
    T
    call(Class<C> cls, 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(Class<?> clazz, 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 int
    execute(Class<?> clazz, Collection<String> environments, String... args)
    Same than execute(Class, String...), but with a list of customEnvironments to be applied.
    static <R extends Runnable>
    void
    run(Class<R> cls, io.micronaut.context.ApplicationContext ctx, 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 Runnable>
    void
    run(Class<R> cls, 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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • PicocliRunner

      public PicocliRunner()
  • Method Details

    • call

      public static <C extends Callable<T>, T> T call(Class<C> cls, 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.

      The ApplicationContext is closed before this method returns.

      Type Parameters:
      C - The callable type
      T - The callable return type
      Parameters:
      cls - the Callable command class
      args - the command line arguments
      Returns:
      null if 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 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.

      The caller is responsible for closing the context.

      Type Parameters:
      C - The callable type
      T - The callable return type
      Parameters:
      cls - the Callable command class
      ctx - the ApplicationContext that injects dependencies into the command
      args - the command line arguments
      Returns:
      null if 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 a CommandLine.Command, CommandLine.Option or CommandLine.Parameters annotation
      picocli.CommandLine.ExecutionException - if the Callable throws an exception
    • run

      public static <R extends Runnable> void run(Class<R> cls, 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.

      The ApplicationContext is closed before this method returns.

      Type Parameters:
      R - The runnable type
      Parameters:
      cls - the Runnable command class
      args - 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 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.

      The caller is responsible for closing the context.

      Type Parameters:
      R - The runnable type
      Parameters:
      cls - the Runnable command class
      ctx - the ApplicationContext that injects dependencies into the command
      args - the command line arguments
      Throws:
      picocli.CommandLine.InitializationException - if the specified command object does not have a CommandLine.Command, CommandLine.Option or CommandLine.Parameters annotation
      picocli.CommandLine.ExecutionException - if the Runnable throws an exception
    • execute

      public static int execute(Class<?> clazz, 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.

      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);
       }
       
      Parameters:
      clazz - the Runnable or Callable command class
      args - the command line arguments
      Returns:
      the exit code returned by CommandLine.execute(String...)
    • execute

      public static int execute(Class<?> clazz, Collection<String> environments, String... args)
      Same than execute(Class, String...), but with a list of customEnvironments to be applied.
      Parameters:
      clazz - the Runnable or Callable command class
      environments - List of environments to be applied when building ApplicationContext
      args - the command line arguments
      Returns:
      the exit code returned by CommandLine.execute(String...)
      See Also: