幫助

執行 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。