|
This version is still in development and is not considered stable yet. For the latest stable version, please use Spring Shell 3.4.1! |
Command Syntax
Options and arguments
Command options and arguments can be defined using method parameters:
class Example2 {
@Command(name = "hi", description = "Say hi to a given name", group = "greetings",
help = "A command that greets the user with 'Hi ${name}!' with a configurable suffix. Example usage: hi -s=! John")
public void sayHi(
@Argument(index = 0, description = "the name of the person to greet",
defaultValue = "world") String name,
@Option(shortName = 's', longName = "suffix", description = "the suffix of the greeting message",
defaultValue = "!") char suffix) {
System.out.println("Hi " + name + suffix);
}
}
Options are defined using @Option annotation, while arguments are defined using @Argument annotation.
Options are named, while arguments are positional. Options can have short names (single character) and long names (multi-character).
Options can be validated using the Bean Validation API by adding validation annotations to the method parameters, see the Validating Command Options section for more details.
Parsing rules
Spring Shell follows the same parsing rules as Spring Boot, with enhanced capabilities following the POSIX style.
Options can be specified in the form -o=value and --option=value. Options and arguments can be specified in any order.
Arguments are 0-based indexed among other arguments:
CommandSyntax ::= CommandName [SubCommandName]* [Option | Argument]*
CommandName ::= String
SubCommandName ::= String
Option ::= ShortOption | LongOption
ShortOption ::= '-' Char ('=' String)?
LongOption ::= '--' String ('=' String)?
Argument ::= String
For example:
$>mycommand mysubcommand --option1=value1 arg1 -o2=value2 arg2
If your command defines subcommands and a subcommand is used without options, then arguments must be separated using "--" (POSIX style):
$>mycommand mysubcommand -- arg1 arg2
This is required to avoid ambiguity between options and arguments.