public final class Cli
extends java.lang.Object
Note: The argument parser does not support setting an argument using foo=bar.
The following options are automatically added to each command:
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.
Modifier and Type | Field and 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 |
Constructor and Description |
---|
Cli(java.lang.String applicationName)
Creates a new CLI with the given name.
|
Cli(java.lang.String applicationName,
java.lang.ClassLoader classLoader)
Creates a new CLI with the given name.
|
Modifier and Type | Method and 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.
|
void |
setConfigureLogging(boolean configureLogging)
Set to true to configure logging for the CLI.
|
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.
|
public static final java.lang.String HELP
public static final java.lang.String NO_COLOR
public static final java.lang.String FORCE_COLOR
public static final java.lang.String DEBUG
public static final java.lang.String STACKTRACE
public static final java.lang.String LOGGING
public Cli(java.lang.String applicationName)
applicationName
- Name of the CLI application.public Cli(java.lang.String applicationName, java.lang.ClassLoader classLoader)
applicationName
- Name of the CLI application.classLoader
- ClassLoader to use when invoking commands.public void addCommand(Command command)
command
- Command to add.public void setConfigureLogging(boolean configureLogging)
configureLogging
- Set to true to configure logging.public void run(java.lang.String[] args)
args
- Arguments to parse.public static void setStdout(java.util.function.Consumer<java.lang.String> printer)
printer
- Consumer responsible for writing to STDOUT.public static void setStderr(java.util.function.Consumer<java.lang.String> printer)
printer
- Consumer responsible for writing to STDERR.public static java.util.function.Consumer<java.lang.String> getStdout()
public static java.util.function.Consumer<java.lang.String> getStderr()
public static void stdout(java.lang.Object message)
message
- Message to write.public static void stderr(java.lang.Object message)
message
- Message to write.public static void setUseAnsiColors(boolean useAnsiColors)
useAnsiColors
- Set to true or false to enable/disable.