此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Shell 3.3.3! |
组织命令
当你的 shell 开始提供大量功能时,你最终可能会遇到
使用大量命令,这可能会让您的用户感到困惑。通过键入help
,
他们会看到一个令人生畏的命令列表,按字母顺序排列,
这可能并不总是显示可用命令的最佳方式。
为了减少这种可能的混淆, Spring Shell 提供了将命令分组在一起的能力。
具有合理的默认值。然后,相关命令将位于同一组中(例如User Management Commands
)
并一起显示在帮助屏幕和其他地方。
默认情况下,命令根据它们在其中实现的类进行分组。
将 camelCase 类名转换为单独的单词(因此URLRelatedCommands
成为URL Related Commands
).
这是一个明智的默认值,因为相关命令通常已经在类中了。
,因为他们需要使用相同的协作对象。
但是,如果此行为不适合您,则可以将组替换为 命令,按优先级顺序排列:
-
指定
group()
在@ShellMethod
注解。 -
放置
@ShellCommandGroup
在定义命令的类上。这适用 该类中定义的所有命令的组(除非被覆盖,如前所述)。 -
放置
@ShellCommandGroup
在包上(通过package-info.java
) 其中定义了命令。这适用于 package 中(除非在方法或类级别被覆盖,如前所述)。
下面的清单显示了一个示例:
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() {}
}