Execution

本节介绍如何在交互模式下设置 Spring Shell。

This section describes how to set up a Spring Shell to work in interactive mode.

Interaction Mode

版本 2.1.x 引入了内置支持,用于区分交互式模式与非交互式模式。这使得在不需定制的情况下,更方便地将 Shell 作为简单的命令行工具使用。

Version 2.1.x introduced built-in support to distinguish between interactive and non-interactive modes. This makes it easier to use the shell as a simple command-line tool without requiring customization.

如果在从命令行中启动或运行 Shell 时传递了任何命令行选项,当前将进入交互模式。当用 Native Support 编译 Shell 应用程序时,此操作尤为有效。

Currently, interactive mode is entered if any command line options are passed when starting or running a shell from a command line. This works especially well when a shell application is compiled with Native Support.

某些命令在交互式模式下或(相反地)在非交互式模式下运行时可能没有任何有用的意义。例如,内置 exit 命令在非交互式模式下没有意义,因为它用于退出交互式模式。

Some commands may not have any useful meanings when they run in interactive mode or (conversely) in non-interactive mode. For example, a built-in exit command would have no meaning in non-interactive mode, because it is used to exit interactive mode.

@ShellMethod 注释有一个称为 interactionMode 的字段,你可以用它告诉 Shell 何时某个特定的命令可用。

The @ShellMethod annotation has a field called interactionMode that you can use to inform shell about when a particular command is available.

Shell Runners

ShellApplicationRunner 是一个主界面,Boot 的 ApplicationArguments 将传递给它,它的默认实现会选择使用哪个 ShellRunner。只能有一个 ShellApplicationRunner,但如果出于某种原因需要重新定义它,则可以重新定义它。

ShellApplicationRunner is a main interface where Boot’s ApplicationArguments are passed and its default implementation makes a choice which ShellRunner is used. There can be only one ShellApplicationRunner but it can be redefined if needed for some reason.

存在三个 ShellRunner 实现,它们分别名为 InteractiveShellRunnerNonInteractiveShellRunnerScriptShellRunner。默认情况下,它们已启用,但如果需要的话,可以使用属性 spring.shell.interactive.enabledspring.shell.noninteractive.enabledspring.shell.script.enabled 禁用它们。

Three ShellRunner implementation exists, named InteractiveShellRunner, NonInteractiveShellRunner and ScriptShellRunner. These are enabled on default but can be disable if needed using properties spring.shell.interactive.enabled, spring.shell.noninteractive.enabled and spring.shell.script.enabled respecively.