組織命令

當您的 shell 開始提供大量功能時,您最終可能會有很多命令,這可能會讓您的使用者感到困惑。透過輸入 help,他們會看到一長串令人望而生畏的命令列表,這些命令以字母順序組織,但這可能不總是顯示可用命令的最佳方式。

為了減輕這種可能的混淆,Spring Shell 提供了將命令分組在一起的能力,並具有合理的預設值。相關的命令隨後將最終歸在同一個群組中(例如,使用者管理命令),並一起顯示在說明畫面和其他地方。

預設情況下,命令會根據它們實作的類別進行分組,將 camelCase 類別名稱轉換為單獨的單字(因此 URLRelatedCommands 變成 URL 相關命令)。這是一個合理的預設值,因為相關的命令通常已經在同一個類別中,因為它們需要使用相同的協作物件。

但是,如果這種行為不適合您,您可以按照優先順序,透過以下方式覆寫命令的群組

  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() {}
}