Class DefaultCommandParser

java.lang.Object
org.springframework.shell.core.command.DefaultCommandParser
All Implemented Interfaces:
CommandParser

public class DefaultCommandParser extends Object implements CommandParser
Default implementation of CommandParser. Supports options in the long form of --key=value or --key value as well in the short form of -k=value or -k value. Options and arguments can be specified in any order. Arguments are 0-based indexed among other arguments. Option values and arguments can be quoted with double or single quotes to include whitespace, and quote characters can be escaped with a backslash inside quoted values (e.g. greet "she said \" and left"). Unbalanced quotes are rejected with an IllegalArgumentException.
 CommandSyntax  ::= CommandName [SubCommandName]* [Option | Argument]*
 CommandName    ::= String
 SubCommandName ::= String
 Option         ::= ShortOption | LongOption
 ShortOption    ::= '-' Char ['='|' ']? String
 LongOption     ::= '--' String ['='|' ']? String
 Argument       ::= String

 Example: mycommand mysubcommand --optionA=value1 arg1 -b=value2 arg2 --optionC value3 -d value4
 
Since:
4.0.0
Author:
Mahmoud Ben Hassine, David Pilar
  • Constructor Details

    • DefaultCommandParser

      public DefaultCommandParser(CommandRegistry commandRegistry)
  • Method Details

    • parse

      public ParsedInput parse(String input)
      Description copied from interface: CommandParser
      Parse raw input into a ParsedInput.
      Specified by:
      parse in interface CommandParser
      Parameters:
      input - the raw input
      Returns:
      the parsed input
    • tokenize

      protected List<String> tokenize(String input)
      Split the input into words on whitespace, keeping quoted sections together. This is a character-by-character state machine tracking the currently open quote (none, double or single). A backslash makes the following character part of the current word without any special meaning, so an escaped quote neither opens nor closes a quoted section. Words are kept verbatim (quotes and escape sequences included), quote removal and escape resolution happen later in unquoteAndUnescapeQuoted(String).
      Parameters:
      input - the raw input line
      Returns:
      the list of words
      Throws:
      IllegalArgumentException - if the input contains an unbalanced quote
      Since:
      4.0.4