帮助
运行 shell 应用程序通常意味着用户处于图形界面受限的环境中。此外,虽然在手机时代我们几乎总是保持连接,但访问 web 浏览器或任何其他富 UI 应用程序(如 PDF 阅读器)可能并不总是可行的。因此,shell 命令正确地自我记录是很重要的,而这就是 help
命令的作用所在。
输入 help
+ ENTER
会列出 shell 已知的所有命令(包括不可用的命令)及其简要描述,类似于以下内容:
my-shell:>help
AVAILABLE COMMANDS
Built-In Commands
exit: Exit the shell.
help: Display help about available commands
stacktrace: Display the full stacktrace of the last error.
clear: Clear the shell screen.
quit: Exit the shell.
history: Display or save the history of previously run commands
completion bash: Generate bash completion script
version: Show version info
script: Read and execute commands from a file.
输入 help <命令>
会显示关于该命令的更详细信息,包括可用参数、参数类型、是否必填以及其他细节。
以下列表展示了应用于自身的 help
命令:
my-shell:>help help
NAME
help - Display help about available commands
SYNOPSIS
help --command String
OPTIONS
--command or -C String
The command to obtain help for.
[Optional]
帮助是模板化的,可以根据需要进行自定义。设置在 spring.shell.command.help
下,你可以使用 enabled
来禁用命令,使用 grouping-mode
并选择 group
或 flat
来通过扁平化结构隐藏组,使用 command-template
来定义命令帮助的输出模板,使用 commands-template
来定义命令列表的输出。
如果设置了 spring.shell.command.help.grouping-mode=flat
,则帮助会显示:
my-shell:>help help
AVAILABLE COMMANDS
exit: Exit the shell.
help: Display help about available commands
stacktrace: Display the full stacktrace of the last error.
clear: Clear the shell screen.
quit: Exit the shell.
history: Display or save the history of previously run commands
completion bash: Generate bash completion script
version: Show version info
script: Read and execute commands from a file.
help
和 help <commmand>
的输出都是通过模板生成的,可以更改默认实现。
选项 spring.shell.command.help.commands-template
默认为 classpath:template/help-commands-default.stg
,并传递 GroupsInfoModel
作为模型。
选项 spring.shell.command.help.command-template
默认为 classpath:template/help-command-default.stg
,并传递 CommandInfoModel
作为模型。
表 1. GroupsInfoModel 变量
Key | Description |
---|---|
showGroups | 如果启用了显示组,则为 true 。否则为 false 。 |
groups | 命令变量(参见 GroupCommandInfoModel 变量)。 |
commands | 命令变量(参见 CommandInfoModel 变量)。 |
hasUnavailableCommands | 如果有不可用的命令,则为 true 。否则为 false 。 |
表 2. GroupCommandInfoModel 变量
键 | 说明 |
---|---|
group | 如果设置,为组的名称。否则,为空。 |
commands | 如果设置,为命令。否则,为空。类型是多值,参见 CommandInfoModel 变量。 |
表 3. CommandInfoModel 变量
Key | 描述 |
---|---|
name | 命令的名称(如果设置)。否则为 null。类型是字符串,包含完整命令。 |
names | 命令的名称(如果设置)。否则为 null。类型是多值,本质上是 name 的拆分。 |
aliases | 可能的别名(如果设置)。类型是多值字符串。 |
description | 命令的描述(如果设置)。否则为 null。 |
parameters | 参数变量(如果设置)。否则为空。类型是多值,参见 CommandParameterInfoModel 变量。 |
availability | 可用性变量(参见 CommandAvailabilityInfoModel 变量)。 |
表 4. CommandParameterInfoModel 变量
Key | 描述 |
---|---|
type | 如果设置了参数类型,则为该类型。否则,为 null。 |
arguments | 如果设置了参数,则为这些参数。否则,为 null。类型是包含字符串的多值。 |
required | 如果必填,则为 true 。否则,为 false。 |
description | 如果设置了参数描述,则为该描述。否则,为 null。 |
defaultValue | 如果设置了参数的默认值,则为该默认值。否则,为 null。 |
hasDefaultValue | 如果存在 defaultValue,则为 true 。否则,为 false。 |
表 5. CommandAvailabilityInfoModel 变量
键 | 说明 |
---|---|
available | 如果可用,则为 true 。否则,为 false 。 |
reason | 如果设置了不可用的原因,则显示该原因。否则,为 null。 |