Class Option<T,I extends Option<?,I>>

java.lang.Object
eu.svjatoslav.commons.cli_helper.parameter_parser.Option<T,I>
Type Parameters:
T - the type of object returned by getValue()
I - the actual subclass type, used to enable fluent method chaining
Direct Known Subclasses:
DirectoryOption, DirectoryOptions, FileOption, FileOptions, FloatOption, IntegerOption, NullOption, StringOption, StringOptions

public abstract class Option<T,I extends Option<?,I>> extends Object
Represents a command-line option (a flag or switch), which can be configured with:
  • whether it's mandatory
  • the number of parameters accepted
  • a textual description
  • aliases (the actual CLI flags, e.g., "--help" or "-h")
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    final String
    Human-readable purpose of this option (e.g., "Input image path").
    protected boolean
    Whether this option is mandatory (i.e., must appear at least once on the CLI).
    final List<String>
    The list of parameters provided on the command line for this option (if any).
  • Constructor Summary

    Constructors
    Constructor
    Description
    Option(boolean mandatory, ParameterCount parameterCount, String description, String... aliases2)
    Fully-custom constructor for an option.
    Option(String description, ParameterCount parameterCount, String... aliases2)
    Simpler constructor that defaults to a non-mandatory option.
  • Method Summary

    Modifier and Type
    Method
    Description
    addAliases(String... aliasArray)
    Adds additional aliases to this option for user convenience.
    boolean
    addParameter(String parameterString)
    Adds a parameter to this option, validating it against isValid(String).
    abstract String
    Describes the kind or format of the expected parameter(s), e.g.
    Returns this option's aliases as a comma-separated string (e.g.
    Returns a help message that includes the aliases, whether the option is mandatory, the format, and a brief description.
    abstract T
    Returns the parsed value (usually constructed from parameters).
    boolean
     
    boolean
     
    abstract boolean
    isValid(String value)
    Checks if a single parameter string is valid for this option.
    boolean
    Checks if a given alias matches this option.
    boolean
    Called when no more arguments can be added to this option, giving it a chance to verify correct usage (e.g., check if required parameters are missing).
    Sets this option as mandatory.
    protected void
    setPresent(boolean present)
    Marks this option as present or not present.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • description

      public final String description
      Human-readable purpose of this option (e.g., "Input image path"). For a description of the parameter format (e.g., "file", "integer"), see describeFormat().
    • parameters

      public final List<String> parameters
      The list of parameters provided on the command line for this option (if any).
    • mandatory

      protected boolean mandatory
      Whether this option is mandatory (i.e., must appear at least once on the CLI).
  • Constructor Details

    • Option

      public Option(boolean mandatory, ParameterCount parameterCount, String description, String... aliases2)
      Fully-custom constructor for an option.
      Parameters:
      mandatory - true if the option is required, false otherwise
      parameterCount - the number of parameters required: NONE, ONE, or ONE_OR_MORE
      description - a textual description of the option
      aliases2 - zero or more aliases (e.g. "-f", "--file")
    • Option

      public Option(String description, ParameterCount parameterCount, String... aliases2)
      Simpler constructor that defaults to a non-mandatory option. You can make it mandatory later by calling setMandatory().
      Parameters:
      description - a textual description of the option
      parameterCount - the number of parameters required: NONE, ONE, or ONE_OR_MORE
      aliases2 - zero or more aliases
  • Method Details

    • addAliases

      public I addAliases(String... aliasArray)
      Adds additional aliases to this option for user convenience.
      Parameters:
      aliasArray - additional aliases (e.g., "-f", "--file")
      Returns:
      this option (for method chaining)
    • addParameter

      public boolean addParameter(String parameterString)
      Adds a parameter to this option, validating it against isValid(String).
      Parameters:
      parameterString - the parameter string to add
      Returns:
      true if the parameter was valid and added, otherwise false
    • describeFormat

      public abstract String describeFormat()
      Describes the kind or format of the expected parameter(s), e.g. "File", "Integer", etc.
      Returns:
      a short string describing the parameter format
    • getAliasesAsString

      public String getAliasesAsString()
      Returns this option's aliases as a comma-separated string (e.g. "-f, --file").
    • getHelp

      public String getHelp()
      Returns a help message that includes the aliases, whether the option is mandatory, the format, and a brief description.
      Returns:
      a descriptive help string
    • getValue

      public abstract T getValue()
      Returns the parsed value (usually constructed from parameters).
      Returns:
      the parsed value as the generic type T.
      Throws:
      RuntimeException - if the parameters are insufficient or invalid in a subclass.
    • isMandatory

      public boolean isMandatory()
      Returns:
      true if this option is mandatory.
    • isPresent

      public boolean isPresent()
      Returns:
      true if this option was present on the command line.
    • setPresent

      protected void setPresent(boolean present)
      Marks this option as present or not present.
      Parameters:
      present - true if it was found in the CLI arguments, else false.
    • matchesAlias

      public boolean matchesAlias(String alias)
      Checks if a given alias matches this option.
      Parameters:
      alias - the alias to check
      Returns:
      true if the alias is registered for this option
    • noMoreArguments

      public boolean noMoreArguments()
      Called when no more arguments can be added to this option, giving it a chance to verify correct usage (e.g., check if required parameters are missing).
      Returns:
      true if the usage so far is valid; otherwise false.
    • setMandatory

      public I setMandatory()
      Sets this option as mandatory.
      Returns:
      this option, for method chaining
    • isValid

      public abstract boolean isValid(String value)
      Checks if a single parameter string is valid for this option.
      Parameters:
      value - the parameter string to test
      Returns:
      true if valid; false otherwise