Defining a command registration is a first step to introducing the structure of a command and its options and parameters. This is loosely decoupled from what happens later, such as parsing command-line input and running actual target code. Essentially, it is the definition of a command API that is shown to a user.
A command in a spring-shell
structure is defined as an array of commands. This yields a
structure similar to the following example:
command1 sub1
command2 sub1 subsub1
command2 sub2 subsub1
command2 sub2 subsub2
Note
|
We do not currently support mapping commands to an explicit parent if sub-commands are defined.
For example, command1 sub1 and command1 sub1 subsub1 cannot both be registered.
|
Spring Shell has been designed to work on two modes: interactive (which essentially
is a REPL
where you have an active shell instance throughout a series of commands) and
non-interactive (where commands are executed one by one from a command line).
Differentation between these modes is mostly around limitations about what can be done in each mode. For example, it would not be feasible to show what was a previous stacktrace of a command if the shell is no longer active. Generally, whether the shell is still active dictates the available information.
Also, being on an active REPL
session may provide more information about what the user has been
doing within an active session.
Options can be defined as long and short, where the prefixing is --
and -
, respectively.
The following examples show long and short options:
link:../../test/java/org/springframework/shell/docs/CommandRegistrationSnippets.java[role=include]
link:../../test/java/org/springframework/shell/docs/CommandRegistrationSnippets.java[role=include]
The target defines the execution target of a command. It can be a method in a POJO,
a Consumer
, or a Function
.
Using a Method
in an existing POJO is one way to define a target.
Consider the following class:
link:../../test/java/org/springframework/shell/docs/CommandTargetSnippets.java[role=include]
Given the existing class shown in the preceding listing, you can then register its method:
link:../../test/java/org/springframework/shell/docs/CommandTargetSnippets.java[role=include]
Using a Function
as a target gives a lot of flexibility to handle what
happens in a command execution, because you can handle many things manually by using
a CommandContext
given to a Function
. The return type from a Function
is
then what gets printed into the shell as a result. Consider the following example:
link:../../test/java/org/springframework/shell/docs/CommandTargetSnippets.java[role=include]
Using a Consumer
is basically the same as using a Function
, with the difference being
that there is no return type. If you need to print something into a shell,
you can get a reference to a Terminal
from a context and print something
through it. Consider the following example:
link:../../test/java/org/springframework/shell/docs/CommandTargetSnippets.java[role=include]