public interface CommandProvider
CommandProvider
implementation must enumerate the options and arguments the command accepts by annotating itself with
the relevant Command
, Argument
and Option
annotations.
@Provides @Command(name = "echo", description = @TranslatableText(type = EchoMessages.class, id = EchoMessages.ECHO_COMMAND), arguments = { @Argument(type = String.class, name = "text", description = @TranslatableText(type = EchoMessages.class, id = EchoMessages.TEXT_ARGUMENT)) }) class Echo implements CommandProvider { public void execute(final Map<String, Object> values) throws Exception { final String text = values.get("text"); System.out.println(text); } }In the above example the
Command
, TranslatableText
and Argument
annotations are used to declaratively
define how the command line arguments should be parsed.
The text
argument is parsed by the runtime and is passed an entry in the Map
passed to the execute(Map)
method.
@Provides @Command(name = "echo", description = @TranslatableText(type = EchoMessages.class, id = EchoMessages.ECHO_COMMAND), processArgs = true }) class Echo implements CommandProvider { public int execute(final String...args) throws Exception { for ( String arg: args ) { System.out.print(arg); System.out.print(' '); } System.out.println(); return 0; } }In the above example the command takes care of parsing the command line itself, in this case echoing each argument supplied
Modifier and Type | Method and Description |
---|---|
default int |
execute(Iterable<String> args)
Execute the command, passing it the command line arguments to parse itself
|
default void |
execute(Map<String,Object> values)
Execute the command
|
default int |
execute(String... args)
Execute the command, passing it the command line arguments to parse itself
|
default void execute(Map<String,Object> values) throws Exception
values
- The values for the options and arguments of the commandException
- if an error occurs during execution of the commanddefault int execute(String... args) throws Exception
args
- The command line arguments to this commandException
- if an error occurs during execution of the commandSystem.exit(int)
,
Runtime.exit(int)
default int execute(Iterable<String> args) throws Exception
args
- The command line arguments to this commandException
- if an error occurs during execution of the commandSystem.exit(int)
,
Runtime.exit(int)