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

Exit Code Mappings

Default behaviour of an exit codes is as:spring-doc.cn

  • Errors from a command option parsing will result to a code of 2spring-doc.cn

  • Any generic error will result to a code of 1spring-doc.cn

  • Obviously in any other case will result to a code of 0spring-doc.cn

Every CommandRegistration can define its own mappings between Exception and exit code. Spring shell uses a similar approach to Spring Boot regarding exit code and simply integrate into that.spring-doc.cn

Assuming there is an exception shown below which would be thrown from a command:spring-doc.cn

static class MyException extends RuntimeException {

	private final int code;

	MyException(String msg, int code) {
		super(msg);
		this.code = code;
	}

	public int getCode() {
		return code;
	}
}

It is possible to define a mapping function between Throwable and exit code. You can also just configure a class to exit code which is just a syntactic sugar within configurations.spring-doc.cn

CommandRegistration.builder()
	.withExitCode()
		.map(MyException.class, 3)
		.map(t -> {
			if (t instanceof MyException) {
				return ((MyException) t).getCode();
			}
			return 0;
		})
		.and()
	.build();
Exit codes cannot be customized with annotation based configuration