组织命令

当你的 Shell 开始提供大量功能时,你可能会遇到大量的命令,这可能会让你的用户感到困惑。通过输入 help,他们会看到一个令人望而生畏的命令列表,按字母顺序排列,这可能并非总是显示可用命令的最佳方式。

为了缓解这种可能的困惑,Spring Shell 提供了将命令分组的功能,并带有合理的默认值。相关的命令将最终归入同一组(例如,User Management Commands),并在帮助屏幕和其他地方一起显示。

默认情况下,命令根据其实现的类进行分组,将驼峰式类名转换为单独的单词(因此 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() {}
}
© . This site is unofficial and not affiliated with VMware.