This version is still in development and is not considered stable yet. For the latest stable version, please use Spring Shell 3.3.3!spring-doc.cn

Writing

When something needs to get written into your console you can always use JDK’s System.out which then goes directly into JDK’s own streams. Other recommended way is to use JLine’s Terminal and get writer instance from there.spring-doc.cn

If using target endpoints, i.e. consumer which is not expected to return anything given CommandContext contains reference to Terminal and writer can be accessed from there.spring-doc.cn

CommandRegistration.builder()
	.command("example")
	.withTarget()
		.consumer(ctx -> {
			ctx.getTerminal().writer().println("hi");
			ctx.getTerminal().writer().flush();
		})
		.and()
	.build();

If using @Command you can get access to CommandContext and get Terminal from there.spring-doc.cn

@Command
public void example(CommandContext ctx) {
	ctx.getTerminal().writer().println("hi");
	ctx.getTerminal().writer().flush();
}

It’s possible to autowire Terminal to get access to its writer.spring-doc.cn

@Autowired
Terminal terminal;

@ShellMethod
public void example() {
	terminal.writer().println("hi");
	terminal.writer().flush();
}