This version is still in development and is not considered stable yet. For the latest stable version, please use Spring Shell 3.4.1!

String Input

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();
	}

}

The following screencast shows typical output from a string input component:

The context object is StringInputContext. The following table lists its context variables:

Table 1. StringInputContext Template Variables
Key Description

defaultValue

The default value, if set. Otherwise, null.

maskedInput

The masked input value

maskedResultValue

The masked result value

maskCharacter

The mask character, if set. Otherwise, null.

hasMaskCharacter

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

required

true if the input is required. Otherwise, false.

model

The parent context variables (see TextComponentContext Template Variables).