Class CommandParser

java.lang.Object
com.codingchili.core.context.CommandParser

public class CommandParser extends Object
  • Constructor Details

    • CommandParser

      public CommandParser(String... commandline)
      Parameters:
      commandline - a list starting with the command followed by properties. example: adduser --name foo --admin
  • Method Details

    • setPrefix

      public CommandParser setPrefix(String prefix)
      Parameters:
      prefix - a prefix that prepends the command, removed when parsed. defaults to COMMAND_PREFIX in CoreStrings.COMMAND_PREFIX
      Returns:
      fluent
    • parse

      public void parse(String... line)
      Parses the given command line and discards any prior state.
      Parameters:
      line - a commandline with properties.
    • getCommand

      public Optional<String> getCommand()
      Returns:
      the name of the command that was passed in the commandline.
    • addProperty

      public void addProperty(String key, String value)
      Programmatically adds a property to the parsed command line.
      Parameters:
      key - the key of the property, example '--name'
      value - the value of the property, prefixed with the key.
    • hasProperty

      public boolean hasProperty(String name)
      Parameters:
      name - checks if the parsed commandline specifies the given property
      Returns:
      true if the command contains the property
    • getValue

      public Optional<String> getValue(String name)
      Parameters:
      name - retrieves the property value by its name, excluding the prefix.
      Returns:
      a property value if given, if multiple given then the first is returned.
    • isMulti

      public boolean isMulti(String name)
      Parameters:
      name - the name of the property to check if it has multiple values.
      Returns:
      true if the given property has multiple values.
    • getAllValues

      public List<String> getAllValues(String name)
      Parameters:
      name - retrieves all property values for the given property name, excluding the prefix.
      Returns:
      all property values for the given property name.