Class Cli

java.lang.Object
software.amazon.smithy.cli.Cli

public final class Cli extends Object
This class provides a very basic CLI abstraction.

Note: The argument parser does not support setting an argument using foo=bar.

The following options are automatically added to each command:

  • --help | -h: Prints subcommand help text.
  • --debug: Prints debug information, including exception stack traces.
  • --no-color: Explicitly disables ANSI colors.
  • --force-color: Explicitly enables ANSI colors.
  • --stacktrace: Prints the stacktrace of any CLI exception that is thrown.
  • --logging: Sets the log level to one of OFF, SEVERE, WARNING, INFO, FINE, ALL.

Why are we not using a library for this? Because parsing command line options isn't difficult, we don't need to take a dependency, this code uses no reflection to improve startup time. We can control exactly what CLI features are supported in case we want to migrate to a library or event a different language.

  • Field Details

  • Constructor Details

    • Cli

      public Cli(String applicationName)
      Creates a new CLI with the given name.
      Parameters:
      applicationName - Name of the CLI application.
    • Cli

      public Cli(String applicationName, ClassLoader classLoader)
      Creates a new CLI with the given name.
      Parameters:
      applicationName - Name of the CLI application.
      classLoader - ClassLoader to use when invoking commands.
  • Method Details

    • addCommand

      public void addCommand(Command command)
      Adds a subcommand to the CLI.
      Parameters:
      command - Command to add.
    • setConfigureLogging

      public void setConfigureLogging(boolean configureLogging)
      Set to true to configure logging for the CLI.
      Parameters:
      configureLogging - Set to true to configure logging.
    • run

      public void run(String[] args)
      Execute the command line using the given arguments.
      Parameters:
      args - Arguments to parse.
    • setStdout

      public static void setStdout(Consumer<String> printer)
      Configures a custom STDOUT printer.
      Parameters:
      printer - Consumer responsible for writing to STDOUT.
    • setStderr

      public static void setStderr(Consumer<String> printer)
      Configures a custom STDERR printer.
      Parameters:
      printer - Consumer responsible for writing to STDERR.
    • getStdout

      public static Consumer<String> getStdout()
      Gets the stdout consumer.
      Returns:
      Returns the stdout consumer.
    • getStderr

      public static Consumer<String> getStderr()
      Gets the stderr consumer.
      Returns:
      Returns the stderr consumer.
    • stdout

      public static void stdout(Object message)
      Write a line of text to the configured STDOUT.
      Parameters:
      message - Message to write.
    • stderr

      public static void stderr(Object message)
      Write a line of text to the configured STDERR.
      Parameters:
      message - Message to write.
    • setUseAnsiColors

      public static void setUseAnsiColors(boolean useAnsiColors)
      Explicitly configures whether or not to use ANSI colors.
      Parameters:
      useAnsiColors - Set to true or false to enable/disable.