Organizing Commands

当你的 shell 开始提供大量功能时,你可能会得到大量命令,这可能会使用户感到困惑。通过键入 help,他们将看到按字母顺序排列的令人望而生畏的命令列表,这可能并不是显示可用命令的最佳方式。

When your shell starts to provide a lot of functionality, you may end up with a lot of commands, which could be confusing for your users. By typing help, they would see a daunting list of commands, organized in alphabetical order, which may not always be the best way to show the available commands.

为了减轻这种可能的困惑,Spring Shell 提供了将命令分组在一起的功能,具有合理的默认值。相关的命令随后将出现在同一组中(例如 User Management Commands),并一起显示在帮助屏幕和其他位置。

To alleviate this possible confusion, Spring Shell provides the ability to group commands together, with reasonable defaults. Related commands would then end up in the same group (for example, User Management Commands) and be displayed together in the help screen and other places.

默认情况下,命令将根据其实现的类进行分组,将驼峰式类名转换为单独的单词(因此 URLRelatedCommands 将变为 URL Related Commands)。这是一个明智的默认值,因为相关的命令通常已经存在于类中,因为它们需要使用相同的协作对象。

By default, commands are grouped according to the class they are implemented in, turning the camelCase class name into separate words (so URLRelatedCommands becomes URL Related Commands). This is a sensible default, as related commands are often already in the class anyway, because they need to use the same collaborating objects.

但是,如果此行为不适合你,则可以按照以下方式(按优先级排列)覆盖命令组:

If, however, this behavior does not suit you, you can override the group for a command in the following ways, in order of priority:

  1. Specify a group() in the @ShellMethod annotation.

  2. Place a @ShellCommandGroup on the class in which the command is defined. This applies the group for all commands defined in that class (unless overridden, as explained earlier).

  3. Place a @ShellCommandGroup on the package (through package-info.java) in which the command is defined. This applies to all the commands defined in the package (unless overridden at the method or class level, as explained earlier).

下面的清单显示了一个示例:

The following listing shows an example:

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