For the latest stable version, please use Spring Shell 3.3.3! |
Short Format
Short style POSIX option is usually just a synonym to long format. As
shown below option --arg
is equal to -a
.
-
Programmatic
-
Annotation
-
Legacy Annotation
CommandRegistration stringWithShortOption() {
return CommandRegistration.builder()
.command("example")
.withTarget()
.function(ctx -> {
String arg = ctx.hasMappedOption("arg") ? ctx.getOptionValue("arg") : null;
return String.format("Hi arg='%s'", arg);
})
.and()
.withOption()
.longNames("arg")
.shortNames('a')
.required()
.and()
.build();
}
@Command(command = "example")
String stringWithShortOption(
@Option(longNames = "arg", shortNames = 'a', required = true) String arg) {
return String.format("Hi '%s'", arg);
}
@ShellMethod(key = "example")
String stringWithShortOption(
@ShellOption(value = { "--arg", "-a" }) String arg) {
return String.format("Hi '%s'", arg);
}
Short option with combined format is powerful if type is defined as a flag
which means type is a boolean. That way you can define a presence of a flags
as -abc
, -abc true
or -abc false
.
-
Programmatic
-
Annotation
-
Legacy Annotation
CommandRegistration multipleBooleans() {
return CommandRegistration.builder()
.command("example")
.withTarget()
.function(ctx -> {
Boolean a = ctx.hasMappedOption("a") ? ctx.getOptionValue("a") : null;
Boolean b = ctx.hasMappedOption("b") ? ctx.getOptionValue("b") : null;
Boolean c = ctx.hasMappedOption("c") ? ctx.getOptionValue("c") : null;
return String.format("Hi a='%s' b='%s' c='%s'", a, b, c);
})
.and()
.withOption()
.shortNames('a')
.type(boolean.class)
.defaultValue("false")
.and()
.withOption()
.shortNames('b')
.type(boolean.class)
.defaultValue("false")
.and()
.withOption()
.shortNames('c')
.type(boolean.class)
.defaultValue("false")
.and()
.build();
}
@Command(command = "example")
public String multipleBooleans(
@Option(shortNames = 'a') boolean a,
@Option(shortNames = 'b') boolean b,
@Option(shortNames = 'c') boolean c) {
return String.format("Hi a='%s' b='%s' c='%s'", a, b, c);
}
@ShellMethod(key = "example")
public String multipleBooleans(
@ShellOption(value = "-a") boolean a,
@ShellOption(value = "-b") boolean b,
@ShellOption(value = "-c") boolean c)
{
return String.format("Hi a='%s' b='%s' c='%s'", a, b, c);
}