Package software.amazon.smithy.cli
Class Cli
java.lang.Object
software.amazon.smithy.cli.Cli
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 Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new CLI with the given name.Cli
(String applicationName, ClassLoader classLoader) Creates a new CLI with the given name. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addCommand
(Command command) Adds a subcommand to the CLI.Gets the stderr consumer.Gets the stdout consumer.void
Execute the command line using the given arguments.void
setConfigureLogging
(boolean configureLogging) Set to true to configure logging for the CLI.static void
Configures a custom STDERR printer.static void
Configures a custom STDOUT printer.static void
setUseAnsiColors
(boolean useAnsiColors) Explicitly configures whether or not to use ANSI colors.static void
Write a line of text to the configured STDERR.static void
Write a line of text to the configured STDOUT.
-
Field Details
-
HELP
- See Also:
-
NO_COLOR
- See Also:
-
FORCE_COLOR
- See Also:
-
DEBUG
- See Also:
-
STACKTRACE
- See Also:
-
LOGGING
- See Also:
-
-
Constructor Details
-
Cli
Creates a new CLI with the given name.- Parameters:
applicationName
- Name of the CLI application.
-
Cli
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
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
Execute the command line using the given arguments.- Parameters:
args
- Arguments to parse.
-
setStdout
Configures a custom STDOUT printer.- Parameters:
printer
- Consumer responsible for writing to STDOUT.
-
setStderr
Configures a custom STDERR printer.- Parameters:
printer
- Consumer responsible for writing to STDERR.
-
getStdout
Gets the stdout consumer.- Returns:
- Returns the stdout consumer.
-
getStderr
Gets the stderr consumer.- Returns:
- Returns the stderr consumer.
-
stdout
Write a line of text to the configured STDOUT.- Parameters:
message
- Message to write.
-
stderr
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.
-