Skip to content

Commit 92e962e

Browse files
committed
Clarify command parsing rules and how to customize them
1 parent a8b774b commit 92e962e

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

spring-shell-core/src/main/java/org/springframework/shell/core/command/CommandParser.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@
1616
package org.springframework.shell.core.command;
1717

1818
/**
19-
* Interface parsing arguments for a {@link Command}. A command is always identified by a
20-
* set of words like {@code command subcommand1 subcommand2} and remaining part of it are
21-
* options which this interface intercepts and translates into format we can understand.
19+
* Interface for parsing input for a {@link Command}. A command is always identified by a
20+
* set of words like {@code command subcommand1 subcommand2} and where the remaining part
21+
* is expected to be a set of arguments and/or key/value pairs of options. The
22+
* {@link ParsedInput} API holds the parsed result.
2223
*
2324
* @author Janne Valkealahti
2425
* @author Piotr Olaszewski
2526
* @author Mahmoud Ben Hassine
27+
* @since 4.0.0
28+
* @see ParsedInput
2629
*/
2730
public interface CommandParser {
2831

spring-shell-docs/modules/ROOT/pages/commands/syntax.adoc

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,26 @@ For example:
4141
$>mycommand mysubcommand --option1=value1 arg1 -o2=value2 arg2
4242
----
4343

44-
If your command defines subcommands and a subcommand is used without options, then arguments must be separated using "--" (POSIX style):
44+
IMPORTANT: Options cannot be specified using the `--option value` or `-o value` syntax. Only the `=` syntax is supported. Moreover, short options cannot be grouped (e.g., `-abc` is not supported).
45+
46+
If your command does not have options, then arguments must be separated using "--" (POSIX style) to avoid ambiguity between arguments and subcommands:
47+
48+
[source,shell]
49+
----
50+
$>mycommand -- arg1 arg2
51+
----
52+
53+
Similarly, if your command defines subcommands that are used without options, then arguments must also be separated using "--" (POSIX style) to avoid ambiguity between arguments and subcommands:
4554

4655
[source,shell]
4756
----
4857
$>mycommand mysubcommand -- arg1 arg2
4958
----
5059

51-
This is required to avoid ambiguity between options and arguments.
60+
TIP: To avoid ambiguity, named options should be preferred over positional arguments whenever possible, especially when subcommands are involved (see https://clig.dev/#arguments-and-flags[Command Line Interface Guidelines]).
61+
62+
== Customizing parsing rules
63+
64+
Spring Shell 4 provides a new API called `CommandParser` that allows you to customize the command parsing rules.
65+
If the default parsing rules do not fit your needs, you can implement your own parser by implementing the `CommandParser` interface.
66+
Once the custom parser is implemented, you can register it as a bean in the application context, and Spring Shell will use it for parsing commands.

0 commit comments

Comments
 (0)