組織命令

當您的 Shell 開始提供大量功能時,您可能會遇到大量命令,這可能會讓使用者感到困惑。透過輸入 help,他們會看到一個令人望而卻步的命令列表,按字母順序排列,這可能並非總是顯示可用命令的最佳方式。

為了減輕這種可能的困惑,Spring Shell 提供了將命令組合在一起的功能,並提供了合理的預設值。相關的命令將分組在一起(例如,使用者管理命令),並在幫助螢幕和其他地方一起顯示。

預設情況下,命令根據其實現的類進行分組,將駝峰式命名的類名轉換為獨立的單詞(例如,URLRelatedCommands 變為 URL Related Commands)。這是一個合理的預設行為,因為相關命令通常已經在同一個類中,因為它們需要使用相同的協作物件。

但是,如果此行為不適合您,可以按以下優先順序順序覆蓋命令的分組:

  1. @ShellMethod 註解中指定 group()

  2. 在定義命令的類上放置 @ShellCommandGroup。這將應用於該類中定義的所有命令(除非被覆蓋,如前所述)。

  3. 在定義命令的包上(透過 package-info.java)放置 @ShellCommandGroup。這將應用於包中定義的所有命令(除非在方法或類級別被覆蓋,如前所述)。

以下列表顯示了一個示例

public class UserCommands {
    @ShellMethod(value = "This command ends up in the 'User Commands' group")
    public void foo() {}

    @ShellMethod(value = "This command ends up in the 'Other Commands' group",
    	group = "Other Commands")
    public void bar() {}
}

...

@ShellCommandGroup("Other Commands")
public class SomeCommands {
	@ShellMethod(value = "This one is in 'Other Commands'")
	public void wizz() {}

	@ShellMethod(value = "And this one is 'Yet Another Group'",
		group = "Yet Another Group")
	public void last() {}
}