短いフォーマット
短い形式の POSIX オプションは通常、長い形式の単なるシノニムです。以下に示すように、オプション --arg
は -a
と同等です。
プログラマティック
アノテーション
従来のアノテーション
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);
}
type が boolean であることを意味するフラグとして定義されている場合、形式を組み合わせた短いオプションは強力です。そうすることで、フラグの存在を -abc
、-abc true
、または -abc false
として定義できます。
プログラマティック
アノテーション
従来のアノテーション
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);
}