For the latest stable version, please use Spring Shell 3.4.0!

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.

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.

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.

@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.

@Autowired
Terminal terminal;

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