Completion

Spring Shell 可以同时为交互式 Shell 和命令行提供完成建议。然而,它们之间存在差异,因为当 Shell 处于交互式模式时,我们有一个 Shell 的活动实例,这意味着更方便地提供更多以编程方式提供完成提示的方法。当 Shell 纯粹作为命令行工具运行时,只能通过与 OS 级 Shell(如 bash)集成来完成完成。

Spring Shell can provide completion proposals for both interactive shell and a command-line. There are differences however as when shell is in interactive mode we have an active instance of a shell meaning it’s easier to provide more programmatic ways to provide completion hints. When shell is purely run as a command-line tool a completion can only be accomplished with integration into OS level shell’s like bash.

Interactive

完成提示是使用 functioninterface 样式的方法计算的,该方法使用 CompletionContext 并返回 CompletionProposal 实例的列表。CompletionContext 为你提供有关当前上下文(如命令注册和选项)的各种信息。

Hints for completions are calculated with function or interface style methods which takes CompletionContext and returns a list of CompletionProposal instances. CompletionContext gives you various information about a current context like command registration and option.

通用的解析器可以注册为一个 bean,如果这些解析器对于所有命令和场景都是有用的。(例如,现有的完成实现 RegistrationOptionsCompletionResolver 处理选项名称的完成)。

Generic resolvers can be registered as a beans if those are useful for all commands and scenarious. For example existing completion implementation RegistrationOptionsCompletionResolver handles completions for a option names.

Unresolved include directive in modules/ROOT/pages/completion.adoc - include::../../test/java/org/springframework/shell/docs/CompletionSnippets.java[]

使用基于构建器的命令注册的选项值可以针对每个选项定义。

Option values with builder based command registration can be defined per option.

Unresolved include directive in modules/ROOT/pages/completion.adoc - include::../../test/java/org/springframework/shell/docs/CompletionSnippets.java[]

使用基于注释的命令注册的选项值通过 ValueProvider 界面处理,该界面可以使用 @ShellOption 注释定义。

Option values with annotation based command registration are handled via ValueProvider interface which can be defined with @ShellOption annotation.

Unresolved include directive in modules/ROOT/pages/completion.adoc - include::../../test/java/org/springframework/shell/docs/CompletionSnippets.java[]

基于注释的命令的实际 ValueProvider 需要注册为 Bean

Actual ValueProvider with annotation based command needs to be registered as a Bean.

Unresolved include directive in modules/ROOT/pages/completion.adoc - include::../../test/java/org/springframework/shell/docs/CompletionSnippets.java[]

Command-Line

命令行补全目前仅支持 bash ,并且在内置 completion 命令 Completion 中有记录。

Command-line completion currently only support bash and is documented in a built-in completion command Completion.