Package software.amazon.smithy.cli
Class Cli
- java.lang.Object
-
- software.amazon.smithy.cli.Cli
-
public final class Cli extends java.lang.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 Summary
Fields Modifier and Type Field Description static java.lang.String
DEBUG
static java.lang.String
FORCE_COLOR
static java.lang.String
HELP
static java.lang.String
LOGGING
static java.lang.String
NO_COLOR
static java.lang.String
STACKTRACE
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addCommand(Command command)
Adds a subcommand to the CLI.static java.util.function.Consumer<java.lang.String>
getStderr()
Gets the stderr consumer.static java.util.function.Consumer<java.lang.String>
getStdout()
Gets the stdout consumer.void
run(java.lang.String[] args)
Execute the command line using the given arguments.static void
setStderr(java.util.function.Consumer<java.lang.String> printer)
Configures a custom STDERR printer.static void
setStdout(java.util.function.Consumer<java.lang.String> printer)
Configures a custom STDOUT printer.static void
setUseAnsiColors(boolean useAnsiColors)
Explicitly configures whether or not to use ANSI colors.static void
stderr(java.lang.Object message)
Write a line of text to the configured STDERR.static void
stdout(java.lang.Object message)
Write a line of text to the configured STDOUT.
-
-
-
Field Detail
-
HELP
public static final java.lang.String HELP
- See Also:
- Constant Field Values
-
NO_COLOR
public static final java.lang.String NO_COLOR
- See Also:
- Constant Field Values
-
FORCE_COLOR
public static final java.lang.String FORCE_COLOR
- See Also:
- Constant Field Values
-
DEBUG
public static final java.lang.String DEBUG
- See Also:
- Constant Field Values
-
STACKTRACE
public static final java.lang.String STACKTRACE
- See Also:
- Constant Field Values
-
LOGGING
public static final java.lang.String LOGGING
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
Cli
public Cli(java.lang.String applicationName)
Creates a new CLI with the given name.- Parameters:
applicationName
- Name of the CLI application.
-
Cli
public Cli(java.lang.String applicationName, java.lang.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 Detail
-
addCommand
public void addCommand(Command command)
Adds a subcommand to the CLI.- Parameters:
command
- Command to add.
-
run
public void run(java.lang.String[] args)
Execute the command line using the given arguments.- Parameters:
args
- Arguments to parse.
-
setStdout
public static void setStdout(java.util.function.Consumer<java.lang.String> printer)
Configures a custom STDOUT printer.- Parameters:
printer
- Consumer responsible for writing to STDOUT.
-
setStderr
public static void setStderr(java.util.function.Consumer<java.lang.String> printer)
Configures a custom STDERR printer.- Parameters:
printer
- Consumer responsible for writing to STDERR.
-
getStdout
public static java.util.function.Consumer<java.lang.String> getStdout()
Gets the stdout consumer.- Returns:
- Returns the stdout consumer.
-
getStderr
public static java.util.function.Consumer<java.lang.String> getStderr()
Gets the stderr consumer.- Returns:
- Returns the stderr consumer.
-
stdout
public static void stdout(java.lang.Object message)
Write a line of text to the configured STDOUT.- Parameters:
message
- Message to write.
-
stderr
public static void stderr(java.lang.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.
-
-