|
该版本仍在开发中,尚未被视为稳定。最新稳定版本请使用Spring Shell 3.4.1! |
指挥可用性
注册命令并不总是有意义,因为应用程序的内部状态。例如,可能存在下载但只有用户使用后才有效连接遥控器 服务器。 现在,如果用户尝试使用下载命令,shell 应该解释命令存在,但当时不可用。Spring Shell 允许你这样做,甚至让你简要说明命令不可用的原因。
编程
通过程序化注册,你可以使用可用性方法提供商<可用性>.
private boolean connected;
@Bean
public CommandRegistration connect(
CommandRegistration.BuilderSupplier builder) {
return builder.get()
.command("connect")
.withOption()
.longNames("connected")
.required()
.type(boolean.class)
.and()
.withTarget()
.consumer(ctx -> {
boolean connected = ctx.getOptionValue("connected");
this.connected = connected;
})
.and()
.build();
}
@Bean
public CommandRegistration download(
CommandRegistration.BuilderSupplier builder) {
return builder.get()
.command("download")
.availability(() -> {
return connected
? Availability.available()
: Availability.unavailable("you are not connected");
})
.withTarget()
.consumer(ctx -> {
// do something
})
.and()
.build();
}
注解
通过基于注释的命令,你可以使用@CommandAvailability䋰可用性提供者.
@Command
class MyCommands {
private boolean connected;
@Command(command = "connect")
public void connect(String user, String password) {
connected = true;
}
@Command(command = "download")
@CommandAvailability(provider = "downloadAvailability")
public void download(
) {
// do something
}
@Bean
public AvailabilityProvider downloadAvailability() {
return () -> connected
? Availability.available()
: Availability.unavailable("you are not connected");
}
}
遗产注释
命令表示可用性有三种可能方式。它们都使用无 arg 方法,返回可用性. 请考虑以下例子:
@ShellComponent
public class MyCommands {
private boolean connected;
@ShellMethod("Connect to the server.")
public void connect(String user, String password) {
// do something
connected = true;
}
@ShellMethod("Download the nuclear codes.")
public void download() {
// do something
}
public Availability downloadAvailability() {
return connected
? Availability.available()
: Availability.unavailable("you are not connected");
}
}
这连接方法用于连接服务器(细节省略),通过连接布尔值,完成后再用。 这下载命令标记为不可用,直到用户连接,这都要归功于存在一个名为下载命令方法,其中可用性在名称中加上后缀。该方法返回的实例是可用性,用两种工厂方法之一构建。如果无法使用该命令,必须提供解释。现在,如果用户在未连接的情况下尝试调用该命令,会发生以下情况:
shell:>download
Command 'download' exists but is not currently available because you are not connected.
Details of the error have been omitted. You can use the stacktrace command to print the full stacktrace.
当前不可用命令的信息也被用于集成帮助中。参见帮助。
|
当命令不可用时,如果在“Because”后面加上原因,应该能清晰阅读。 句子开头不应大写或加句号 |
如果以命令方法的名称命名可用性方法不适合你,你可以通过使用@ShellMethodAvailability注解:
@ShellMethod("Download the nuclear codes.")
@ShellMethodAvailability("availabilityCheck") (1)
public void download() {
}
public Availability availabilityCheck() { (1)
return connected
? Availability.available()
: Availability.unavailable("you are not connected");
}
| 1 | 名字必须匹配 |
最后,通常同一类中的多个命令共享相同的内部状态,因此,它们应当作为一个组可用或不可用。而不是必须插入@ShellMethodAvailability在所有命令方法上,Spring Shell 允许你翻转作并放置@ShellMethodAvailabilty关于可用性方法的注释,指定其控制命令的名称:
@ShellMethod("Download the nuclear codes.")
public void download() {
}
@ShellMethod("Disconnect from the server.")
public void disconnect() {
}
@ShellMethodAvailability({"download", "disconnect"})
public Availability availabilityCheck() {
return connected
? Availability.available()
: Availability.unavailable("you are not connected");
}
|
该项的默认值
|
| Spring Shell 对如何编写命令以及如何组织类没有太多限制。然而,通常将相关命令放在同一类中是个好习惯,可用性指示器可以从中受益。 |