組織命令
當你的 Shell 開始提供大量功能時,你可能會遇到大量的命令,這可能會讓你的使用者感到困惑。透過輸入 help,他們會看到一個令人望而生畏的命令列表,按字母順序排列,這可能並非總是顯示可用命令的最佳方式。
為了緩解這種可能的困惑,Spring Shell 提供了將命令分組的功能,並帶有合理的預設值。相關的命令將最終歸入同一組(例如,User Management Commands),並在幫助螢幕和其他地方一起顯示。
預設情況下,命令根據其實現的類進行分組,將駝峰式類名轉換為單獨的單詞(因此 URLRelatedCommands 變為 URL Related Commands)。這是一個明智的預設值,因為相關的命令通常無論如何都在同一個類中,因為它們需要使用相同的協作物件。
但是,如果此行為不適合你,你可以透過以下方式(按優先順序順序)覆蓋命令的組
-
在
@ShellMethod註解中指定group()。 -
在定義命令的類上放置
@ShellCommandGroup。這會將組應用於該類中定義的所有命令(除非被覆蓋,如前所述)。 -
在定義命令的包(透過
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() {}
}