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

文字列入力

The string input component asks a user for simple text input, optionally masking values if the content contains something sensitive. The input can also be required (at least 1 char).
The following listing shows an example:

public class ComponentCommands {

	@Command(name = "component string", description = "String input", group = "Components")
	public String stringInput(boolean mask) {
		StringInput component = new StringInput(getTerminal(), "Enter value", "myvalue");
		ResourceLoader resourceLoader = null; // getResourceLoader();
		TemplateExecutor templateExecutor = null; // getTemplateExecutor();
		component.setResourceLoader(resourceLoader);
		component.setTemplateExecutor(templateExecutor);
		if (mask) {
			component.setMaskCharacter('*');
		}
		StringInputContext context = component.run(StringInputContext.empty());
		return "Got value " + context.getResultValue();
	}

}

次のスクリーンキャストは、文字列入力コンポーネントからの一般的な出力を示しています。

コンテキストオブジェクトは StringInputContext です。次の表に、そのコンテキスト変数を示します。

表 1: StringInputContext テンプレート変数
キー 説明

defaultValue

設定されている場合のデフォルト値。それ以外の場合は null。

maskedInput

マスクされた入力値

maskedResultValue

マスクされた結果値

maskCharacter

マスク文字 (設定されている場合)。それ以外の場合は null。

hasMaskCharacter

true if a mask character is set. Otherwise, false.

required

true if the input is required. Otherwise, false.

model

親コンテキスト変数 ( TextComponentContext テンプレート変数を参照)。