帮助

运行 shell 应用程序通常意味着用户处于图形界面受限的环境中。此外,尽管在移动电话时代我们几乎总是连接着网络,但访问网页浏览器或任何其他富 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 <command> 会显示关于命令的更详细信息,包括可用参数、它们的类型、是否强制以及其他详细信息。

以下列表显示了 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(接受 groupflat)来决定是否通过扁平化结构隐藏分组,使用 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.

来自 helphelp <command> 的输出都是模板化的,具有可以修改的默认实现。

选项 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 变量
描述

showGroups

如果启用显示分组,则为 true。否则为 false。

groups

命令变量(参见 GroupCommandInfoModel 变量)。

commands

命令变量(参见 CommandInfoModel 变量)。

hasUnavailableCommands

如果存在不可用命令,则为 true。否则为 false。

表 2. GroupCommandInfoModel 变量
描述

group

分组名称,如果已设置。否则为空。

commands

命令,如果已设置。否则为空。类型为多值,参见 CommandInfoModel 变量

表 3. CommandInfoModel 变量
描述

name

命令名称,如果已设置。否则为 null。类型为字符串,包含完整命令。

names

命令名称列表,如果已设置。否则为 null。类型为多值,本质上是 name 的分割结果。

aliases

可能的别名列表,如果已设置。类型为包含字符串的多值。

description

命令描述,如果已设置。否则为 null。

parameters

参数变量列表,如果已设置。否则为空。类型为多值,参见 CommandParameterInfoModel 变量

availability

可用性变量(参见 CommandAvailabilityInfoModel 变量)。

表 4. CommandParameterInfoModel 变量
描述

type

参数类型,如果已设置。否则为 null。

arguments

参数的参数(arguments),如果已设置。否则为 null。类型为包含字符串的多值。

required

如果强制,则为 true。否则为 false。

description

参数描述,如果已设置。否则为 null。

defaultValue

参数的默认值,如果已设置。否则为 null。

hasDefaultValue

如果存在默认值,则为 true。否则为 false。

表 5. CommandAvailabilityInfoModel 变量
描述

available

如果可用,则为 true。否则为 false。

reason

如果不可用,则提供原因,如果已设置。否则为 null。