Package ch.turic.cli

Class CmdParser

java.lang.Object
ch.turic.cli.CmdParser

public class CmdParser extends Object
A parameter parser that is to parse the command line and after that to query the command parameters.
  • Constructor Details

    • CmdParser

      public CmdParser()
  • Method Details

    • parse

      public static CmdParser parse(String[] parts, Set<String> parameters)
      Parse a string and build up the parsed structures.

      A line can have the form

      key1=value1 key2=value2 .... parameter ... key3=value3 ... parameter

      The keys have to be unique, so no one key can be used twice. The parameters that stand alone on the line without an associated key can be mixed between, before and after the keys.

      The keys, in case they are defined in the argument parameters are case-insensitive.

      Parameters:
      parts - the line that contains the keys and also the arguments
      parameters - a set of parameter names that are allowed on the line. In case this parameter is null, any parameter is allowed. If this set is empty, then no parameter is allowed. If the set contains the parameter names, then the line may use any non-ambiguous prefix of any parameter, and the parsed structure will contain the full parameter name even if the user typed a short prefix.
      Returns:
      the parsed structure object that can later be queried
      Throws:
      IllegalArgumentException - if the line is not properly formatted
    • get

      public Optional<String> get(String key)
      Get the value that was associated with the key on the parsed line.
      Parameters:
      key - the full key as it was defined in the possible keys set
      Returns:
      the optional value as it was on the line or Optional.empty() in case the key was not present on the line
    • get

      public Optional<String> get(int i)
      Get the i-th parameter (starting with 0) from the parsed line. In case i is larger than the index of the last parameter Optional.empty() is returned.
      Parameters:
      i - index of the parameter
      Returns:
      the parameter from the line or Optional.empty() if i is too large