Package software.amazon.smithy.cli
Class Cli
- java.lang.Object
-
- software.amazon.smithy.cli.Cli
-
public final class Cli extends java.lang.ObjectThis 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.StringDEBUGstatic java.lang.StringFORCE_COLORstatic java.lang.StringHELPstatic java.lang.StringLOGGINGstatic java.lang.StringNO_COLORstatic java.lang.StringSTACKTRACE
-
Method Summary
Modifier and Type Method Description voidaddCommand(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.voidrun(java.lang.String[] args)Execute the command line using the given arguments.static voidsetStderr(java.util.function.Consumer<java.lang.String> printer)Configures a custom STDERR printer.static voidsetStdout(java.util.function.Consumer<java.lang.String> printer)Configures a custom STDOUT printer.static voidsetUseAnsiColors(boolean useAnsiColors)Explicitly configures whether or not to use ANSI colors.static voidstderr(java.lang.Object message)Write a line of text to the configured STDERR.static voidstdout(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.
-
-