Class Option<E>

java.lang.Object
snaq.util.jclap.Option<E>
Type Parameters:
E - the return type of the option
Direct Known Subclasses:
BooleanOption, DoubleOption, EnumeratedOption, FloatOption, IntegerOption, LocalDateOption, LongOption, StringOption

public abstract class Option<E>
extends Object
Base implementation of a command-line option.
Author:
Giles Winstanley
  • Field Details

  • Constructor Details

    • Option

      protected Option​(String shortName, String longName, String description, boolean requiresValue, int minCount, int maxCount)
      Creates a new Option instance.
      Parameters:
      shortName - short name of the option (must be non-null; e.g. -?)
      longName - long name of the option (e.g. --help)
      description - helpful description of the option (printed for usage message)
      requiresValue - whether this option requires a subsequently-specified value
      minCount - minimum number of occurrences required for this option
      maxCount - maximum number of occurrences required for this option
    • Option

      protected Option​(String shortName, String longName, String description, boolean requiresValue, boolean mandatory, boolean allowMany)
      Creates a new Option instance.
      Parameters:
      shortName - short name of the option (must be non-null; e.g. -?)
      longName - long name of the option (e.g. --help)
      description - helpful description of the option (printed for usage message)
      requiresValue - whether this option requires a subsequently-specified value
      mandatory - whether this option must be specified
      allowMany - whether this option can take more than one value (i.e. be specified more than once)
  • Method Details

    • equals

      public boolean equals​(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getType

      public abstract Class<E> getType()
      Returns the class type of value this option can take.
      Returns:
      Class instance
    • getValues

      public List<E> getValues()
      Returns:
      list of parsed values
    • addValue

      protected void addValue​(E value) throws OptionException
      Adds the specified value to the list of those parsed.
      Parameters:
      value - the value to add to this option
      Throws:
      OptionException - if a single-value option already has a value
    • parseValue

      protected abstract E parseValue​(String arg, Locale locale) throws OptionException
      Parses the argument string for an option value, optionally using the specified locale for reference (e.g. for date parsing).
      Parameters:
      arg - string argument from which a value is to be parsed
      locale - locale as specified when initializing the CLAParser instance
      Returns:
      Value of the parsed argument string
      Throws:
      OptionException - if a problem occurs while parsing the argument string
    • getShortName

      public String getShortName()
      Returns:
      short name of this option
    • getLongName

      public String getLongName()
      Returns:
      long name of this option
    • getDescription

      public String getDescription()
      Returns:
      description text of this option
    • requiresValue

      public boolean requiresValue()
      Returns:
      whether this option requires a value
    • getMinCount

      public int getMinCount()
      Returns:
      minimum acceptable value count for this option
    • getMaxCount

      public int getMaxCount()
      Returns:
      maximum acceptable value count for this option
    • isMandatory

      public boolean isMandatory()
      Returns:
      whether this option is mandatory
    • isAllowMany

      public boolean isAllowMany()
      Returns:
      whether this option may have multiple values set
    • setMinMaxCounts

      public void setMinMaxCounts​(int minCount, int maxCount)
      Sets the minimum/maximum value counts for this option.
      Parameters:
      minCount - minimum count for the option
      maxCount - maximum count for the option
    • setHidden

      public Option<E> setHidden()
      Sets the flag to hide this option from the usage message.
      Returns:
      reference to option
    • isHidden

      public boolean isHidden()
      Returns:
      whether this option is hidden from the usage message