このバージョンはまだ開発中であり、まだ安定しているとは見なされていません。最新の安定バージョンについては、Spring Shell 3.4.1 を使用してください!

コマンドの可用性

アプリケーションの内部状態により、登録されたコマンドが必ずしも意味を成すとは限りません。例: download コマンドがあるかもしれませんが、ユーザーが リモートサーバーで connect を使用した場合にのみ機能します。これで、ユーザーが download コマンドを使用しようとすると、シェルはコマンドが存在するが、その時点では使用できないことを説明する必要があります。Spring Shell を使用すると、コマンドが使用できない理由の簡単な説明を提供することさえできます。

プログラマティック

With programmatic registration you can use the availabilityProvider method which takes an AvailabilityProvider.

private boolean connected;

@Bean
public AbstractCommand connect() {
	return org.springframework.shell.core.command.Command.builder().name("connect").execute(ctx -> {
		CommandOption connectedOption = ctx.getOptionByName("connected");
		this.connected = Boolean.parseBoolean(connectedOption.value());
	});
}

@Bean
public AbstractCommand download() {
	return org.springframework.shell.core.command.Command.builder()
		.name("download")
		.availabilityProvider(
				() -> connected ? Availability.available() : Availability.unavailable("you are not connected"))
		.execute(ctx -> {
			// do something
		});
}

アノテーション

With annotation based commands you can use the availabilityProvider attribute to specify the name of the AvailabilityProvider bean to use.

@Component
class MyCommands {

	private boolean connected;

	@Command(name = "connect")
	public void connect(String user, String password) {
		connected = true;
	}

	@Command(name = "download", availabilityProvider = "downloadAvailability")
	public void download() {
		// do something
	}

	@Bean
	public AvailabilityProvider downloadAvailability() {
		return () -> connected ? Availability.available() : Availability.unavailable("you are not connected");
	}

}

The connect method is used to connect to the server (details omitted), altering the state of the command through the connected boolean when done. The download command will be unavailable until the user has connected. The availability provider returns an instance of Availability, constructed with one of the two factory methods. If the command is not available, an explanation has to be provided. Now, if the user tries to invoke the command while not being connected, here is what happens:

shell:>download
Command 'download' exists but is not currently available because you are not connected.
The reason provided when the command is not available should read nicely if appended after “because”. You should not start the sentence with a capital or add a final period
Spring Shell は、コマンドの書き方やクラスの編成方法に多くの制約を課していません。ただし、関連するコマンドを同じクラスに配置することは、多くの場合良い方法であり、可用性インジケーターはそれによって恩恵を受けることができます。