Class AbstractOptionParser<T>

java.lang.Object
com.github.rvesse.airline.parser.AbstractParser<T>
com.github.rvesse.airline.parser.options.AbstractOptionParser<T>
All Implemented Interfaces:
OptionParser<T>
Direct Known Subclasses:
AbstractNameValueOptionParser, ClassicGetOptParser, JdbcStyleOptionParser, ListValueOptionParser, MaybePairValueOptionParser, StandardOptionParser

public abstract class AbstractOptionParser<T> extends AbstractParser<T> implements OptionParser<T>
Abstract option parser that provides some useful helper methods to derived classes
  • Field Details

    • SHORT_OPTIONS_PREFIX

      private static final Pattern SHORT_OPTIONS_PREFIX
  • Constructor Details

    • AbstractOptionParser

      public AbstractOptionParser()
  • Method Details

    • findOption

      protected final OptionMetadata findOption(ParseState<T> state, List<OptionMetadata> options, String name)
      Tries to find an option with the given name
      Parameters:
      state - Current parser state
      options - Allowed options
      name - Name
      Returns:
      Option if found, null otherwise
    • findOption

      protected final OptionMetadata findOption(ParseState<T> state, List<OptionMetadata> options, String name, OptionMetadata defaultValue)
      Tries to find an option with the given name
      Parameters:
      state - Current parser state
      options - Allowed options
      name - Name
      defaultValue - Default value to return if nothing found
      Returns:
      Option if found, defaultValue otherwise
    • hasShortNamePrefix

      protected boolean hasShortNamePrefix(String name)
      Return true if the option has a short name prefix i.e. starts with a single - character. This does not mean that it actually is a short option since definitions of what are considered a short option will vary by concrete implementation
      Parameters:
      name - Option name
      Returns:
      True if a short name prefix, false otherwise
    • noValueForOption

      protected void noValueForOption(ParseState<T> state, OptionMetadata option)
      Reports that no/insufficient values were provided for an option
      Parameters:
      state - Parser state
      option - Option whose value(s) are missing
    • isSeparatorOrOption

      protected boolean isSeparatorOrOption(ParseState<T> state, List<OptionMetadata> allowedOptions, String argsSeparator, boolean shortForm, String peekedToken)
      Checks whether the next value is an arguments separator or option

      This helps option parsers ensure they are not overly greedy when trying to consume options and their values when they should instead be raising an error because no value is provided for an option.

      Parameters:
      state - Parse State
      allowedOptions - Allowed options
      argsSeparator - Arguments separator
      shortForm - Whether to test only for short form, if true only consider the first character of peekedToken
      peekedToken - The peeked token to check whether it is the arguments separator or an option
      Returns:
      True if the peeked token represents an arguments separator or an option