命名

如果需要修改選項長名稱,可以使用 OptionNameModifier 介面完成,這是一個簡單的 Function<String, String>。在此介面中,原始選項名作為輸入,修改後的名稱作為輸出。

修飾符可以在 CommandRegistration 中按 OptionSpec 定義,或透過全域性 Bean 或配置屬性設定為預設。在 OptionSpec 中手動定義的修飾符優先於全域性定義的修飾符。預設情況下未定義全域性修飾符。

可以在 CommandRegistration 中為選項定義一個修飾符。

CommandRegistration.builder()
	.withOption()
		.longNames("arg1")
		.nameModifier(name -> "x" + name)
		.and()
	.build();

新增一個型別為 OptionNameModifier 的*單例 Bean*,它將成為全域性預設修飾符。

@Bean
OptionNameModifier sampleOptionNameModifier() {
	return name -> "x" + name;
}

也可以僅新增配置屬性 spring.shell.option.naming.case-type,它會根據定義的型別自動配置一個修飾符。

noop 表示不進行任何操作,camelsnakekebabpascal 分別啟用內建的 camelCasesnake_casekebab-casePascalCase 修飾符。

如果直接建立 CommandRegistration Bean,透過配置屬性設定的全域性預設修飾符僅在使用預配置的 Builder 例項時有效。詳情請參閱 [using-shell-commands-programmaticmodel]
spring:
  shell:
     option:
       naming:
         case-type: noop
         # case-type: camel
         # case-type: snake
         # case-type: kebab
         # case-type: pascal

例如,像這樣在一個帶註解的方法中定義的選項。

@ShellMethod(key = "option-naming-sample")
public void optionNamingSample(
	@ShellOption("from_snake") String snake,
	@ShellOption("fromCamel") String camel,
	@ShellOption("from-kebab") String kebab,
	@ShellOption("FromPascal") String pascal
) {}

預設情況下,該命令的幫助會直接顯示來自 @ShellOption 的名稱。

OPTIONS
       --from_snake String
       [Mandatory]

       --fromCamel String
       [Mandatory]

       --from-kebab String
       [Mandatory]

       --FromPascal String
       [Mandatory]

定義 spring.shell.option.naming.case-type=kebab,會新增預設修飾符,然後選項名稱看起來像這樣。

OPTIONS
       --from-snake String
       [Mandatory]

       --from-camel String
       [Mandatory]

       --from-kebab String
       [Mandatory]

       --from-pascal String
       [Mandatory]