Class CommandLine
Parses a command line into an annotated command and runs it.
A command is a class annotated CommandLine.Command that implements Runnable or
java.util.concurrent.Callable<Integer>. Its fields annotated CommandLine.Option and
CommandLine.Parameters receive the arguments:
@Command(name = "greet", mixinStandardHelpOptions = true, version = "greet 1.0",
description = "Prints a greeting.")
public class Greet implements Runnable {
@Option(names = {"-n", "--count"}, description = "How many times.",
defaultValue = "1")
private int count;
@Parameters(paramLabel = "<name>", description = "Who to greet.")
private String name;
public void run() {
for (int i = 0; i < count; i++) {
System.out.println("Hello " + name);
}
}
}
The names follow picocli, so a picocli command usually needs only its imports changed. Unlike picocli nothing is read by reflection: the build reads the annotations and generates the parser's bindings, so a mistake in a declaration is a build error, and the command runs the same on the JVM and as a native executable.
The build also writes main. With one top-level command it runs that
command; with several, they become the subcommands of one named after the
application. In a backend that also has controllers, the server is the
serve subcommand and the command line with no arguments still starts it.
Exit codes: 0 for success, the command's own code from a Callable, 1 when
the command throws, 2 for a command line that does not parse.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic @interfaceMarks a command class.static interfaceImplemented by aRunnablecommand that has an exit code to report.static interfaceConverts the text of an argument to a type the build does not know.static @interfaceMarks a field that receives an option.static classA command line that does not parse.static @interfaceMarks a field that receives positional parameters.static @interfaceMarks a field of a subcommand that receives its enclosing command.static interfaceStarts the server of a backend that also has commands.static @interfaceMarks aCommandSpecfield that receives the command's own model. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe exit code for a command that threw.static final intWhatserve(String, CommandSpec[], CommandLine.Server, String[])answers when the server ran rather than a command.static final intThe exit code for a command line that does not parse. -
Constructor Summary
ConstructorsConstructorDescriptionCommandLine(CommandSpec spec) Creates a command line for the commandspecdescribes. -
Method Summary
Modifier and TypeMethodDescriptionintParsesargs, runs the command they name, and answers the exit code.The model of the top-level command.static intrun(String appName, CommandSpec[] commands, String[] args) The entry point the build generates for an application whose top-level commands arecommands: runs the command line and answers the exit code.static intserve(String appName, CommandSpec[] commands, CommandLine.Server server, String[] args) The entry point the build generates for a backend.setErr(PrintStream err) Where errors are printed.setOut(PrintStream out) Where help, version and the command's output are printed.voidusage()Prints the command's usage help toSystem.out.static voidusage(CommandSpec command, PrintStream stream) Prints the usage help ofcommandtostream.
-
Field Details
-
USAGE_ERROR
public static final int USAGE_ERRORThe exit code for a command line that does not parse.- See Also:
-
SERVED
public static final int SERVEDWhatserve(String, CommandSpec[], CommandLine.Server, String[])answers when the server ran rather than a command. Every int a command can return is a valid exit code, negative ones included, so the marker is the one value whose exit status is indistinguishable from 0 anyway (Integer.MIN_VALUE & 0xffis 0).- See Also:
-
EXECUTION_ERROR
public static final int EXECUTION_ERRORThe exit code for a command that threw.- See Also:
-
-
Constructor Details
-
CommandLine
Creates a command line for the commandspecdescribes.
-
-
Method Details
-
getCommandSpec
The model of the top-level command. -
setOut
Where help, version and the command's output are printed. Defaults toSystem.out. -
setErr
Where errors are printed. Defaults toSystem.err. -
execute
Parsesargs, runs the command they name, and answers the exit code. Never throws: a command line that does not parse prints the error and the usage and answersUSAGE_ERROR; a command that throws prints the error and answersEXECUTION_ERROR. -
usage
public void usage()Prints the command's usage help toSystem.out. -
usage
Prints the usage help ofcommandtostream. -
run
The entry point the build generates for an application whose top-level commands are
commands: runs the command line and answers the exit code.One command is the top level itself; several become the subcommands of a group named
appName. -
serve
public static int serve(String appName, CommandSpec[] commands, CommandLine.Server server, String[] args) throws Exception The entry point the build generates for a backend. With no arguments it starts the server, as a backend always has. Otherwise the server is the
servesubcommand besidecommands.When there are no commands, only
-h/--helpis recognized, and any other argument starts the server as before, so an existing deployment that passes arguments a backend used to ignore keeps working.Returns
the exit code, or
SERVEDwhen the server ran and the process should end the way a server's does.- Throws:
Exception
-