指挥可用性

注册命令并不总是合理,因为应用程序的内部状态。 例如,可能存在一个下载但只有用户使用后才有效连接遥控器 服务器。现在,如果用户尝试使用下载命令,壳应该会解释 该命令存在,但当时无法使用。 Spring Shell 允许你这样做,甚至允许你简要说明原因 指令无法使用。spring-doc.cadn.net.cn

编程

通过程序化注册,你可以使用可用性方法提供商<可用性>.spring-doc.cadn.net.cn

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可用性提供者.spring-doc.cadn.net.cn

@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 方法,返回可用性. 请考虑以下例子:spring-doc.cadn.net.cn

@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");
	}
}

连接用于连接服务器的方法(细节省略),改变状态 通过连接布尔值,完成后再用。 这下载命令在用户连接之前被标记为不可用,感谢存在感 一个恰好命名为下载命令方法,其中可用性名字后缀。 该方法返回一个实例可用性,采用两种工厂方法之一制造。 如果无法使用该命令,则需提供说明。 现在,如果用户在未连接的情况下尝试调用该命令,会发生以下情况:spring-doc.cadn.net.cn

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.

当前无法使用命令的信息也被用于集成帮助中。参见帮助spring-doc.cadn.net.cn

当命令不可用时,如果在“Because”后面加上原因,应该能清晰阅读。spring-doc.cadn.net.cn

句子开头不应大写或加句号spring-doc.cadn.net.cn

如果你不适合用命令方法名称来命名可用性方法,你 可以通过使用@ShellMethodAvailability注解:spring-doc.cadn.net.cn

@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关于可用性方法的注释,指定其控制命令的名称:spring-doc.cadn.net.cn

@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");
}

该项的默认值@ShellMethodAvailability.value()属性为。这部特别节目 通配卡匹配所有命令名称。这使得开启或关闭同一类的所有命令变得容易 采用单一可用性方法:*spring-doc.cadn.net.cn

@ShellComponent
public class Toggles {

	@ShellMethodAvailability
	public Availability availabilityOnWeekdays() {
		return Calendar.getInstance().get(DAY_OF_WEEK) == SUNDAY
			? Availability.available()
			: Availability.unavailable("today is not Sunday");
	}

	@ShellMethod
	public void foo() {}

	@ShellMethod
	public void bar() {}
}
Spring Shell 对如何编写命令和如何组织类没有太多限制。 不过,通常将相关命令和可用性指示器放在同一类别是个好习惯 我可以从中受益。