退出代码映射

退出代码的默认行为如下:

  • 命令选项解析错误将导致代码 2

  • 任何通用错误将导致代码 1

  • 显然,在任何其他情况下,都将导致代码 0

每个 CommandRegistration 都可以定义其自己的异常退出代码之间的映射。Spring Shell 使用与 Spring Boot 类似的方法来处理退出代码,并将其简单地集成进去。

假设一个命令会抛出如下所示的异常

static class MyException extends RuntimeException {

	private final int code;

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

	public int getCode() {
		return code;
	}
}

可以定义 Throwable 和退出代码之间的映射函数。您也可以直接配置一个退出代码的映射,这只是配置中的一个语法糖。

CommandRegistration.builder()
	.withExitCode()
		.map(MyException.class, 3)
		.map(t -> {
			if (t instanceof MyException) {
				return ((MyException) t).getCode();
			}
			return 0;
		})
		.and()
	.build();
退出代码无法通过基于注解的配置进行定制
© . This site is unofficial and not affiliated with VMware.