Class AbstractOperand<T>

java.lang.Object
org.refcodes.cli.AbstractOperand<T>
Type Parameters:
T - The generic type of the AbstractOperand's value.
All Implemented Interfaces:
Cloneable, Comparable<Operand<?>>, MatchCountAccessor, Operand<T>, ParsedArgsAccessor, Synopsisable, Term, org.refcodes.exception.ExceptionAccessor<ArgsSyntaxException>, org.refcodes.graphical.VisibleAccessor, org.refcodes.graphical.VisibleAccessor.VisibleBuilder<Term>, org.refcodes.graphical.VisibleAccessor.VisibleMutator, org.refcodes.graphical.VisibleAccessor.VisibleProperty, org.refcodes.mixin.AliasAccessor, org.refcodes.mixin.Clonable, org.refcodes.mixin.DescriptionAccessor, org.refcodes.mixin.Resetable, org.refcodes.mixin.TypeAccessor<T>, org.refcodes.mixin.ValueAccessor<T>, org.refcodes.schema.Schemable
Direct Known Subclasses:
AbstractDirective, AbstractOption, NoneOperand, Operation, StringOperand

public abstract class AbstractOperand<T> extends Object implements Operand<T>
The AbstractOperand is an abstract implementation of an Operand providing the boiler plate when implementing the Operand interface.

As the Operand stands for an arbitrary value parsed from the command line arguments, disambiguation may occur when parsing the command line arguments. The arguments are parsed from right to left to prevent parsing errors in case of ambiguous arguments: An Operand matches any argument not having an option prefix (short option and long option prefix).

  • Field Details

    • _matchCount

      protected int _matchCount
    • _description

      protected String _description
    • _exception

      protected ArgsSyntaxException _exception
    • _isVisible

      protected boolean _isVisible
  • Constructor Details

    • AbstractOperand

      public AbstractOperand(Class<T> aType, String aAlias, String aDescription)
      Constructs an Operand with the given arguments.
      Parameters:
      aType - The type of the value returned by the getValue() method.
      aAlias - The Operand's name, used for syntax creation.
      aDescription - The description to be used (without any line breaks).
    • AbstractOperand

      public AbstractOperand(Class<T> aType, String aAlias, String aDescription, Consumer<? extends Operand<T>> aConsumer)
      Constructs an Operand with the given arguments.
      Parameters:
      aType - The type of the value returned by the getValue() method.
      aAlias - The Operand's name, used for syntax creation.
      aDescription - The description to be used (without any line breaks).
      aConsumer - The Consumer being invoked in case this Operand participated in successfully parsing the command line arguments.
    • AbstractOperand

      public AbstractOperand(org.refcodes.struct.Relation<String,T> aProperty, Class<T> aType)
      Constructs an Operand with the given arguments.
      Parameters:
      aProperty - The key (= alias) and the value for the operand.
      aType - The type of the value returned by the getValue() method.
    • AbstractOperand

      public AbstractOperand(org.refcodes.struct.Relation<String,T> aProperty, Class<T> aType, Consumer<? extends Operand<T>> aConsumer)
      Constructs an Operand with the given arguments.
      Parameters:
      aProperty - The key (= alias) and the value for the operand.
      aType - The type of the value returned by the getValue() method.
      aConsumer - The Consumer being invoked in case this Option participated in successfully parsing the command line arguments.
  • Method Details

    • clone

      public Object clone() throws CloneNotSupportedException
      Specified by:
      clone in interface org.refcodes.mixin.Clonable
      Overrides:
      clone in class Object
      Throws:
      CloneNotSupportedException
    • compareTo

      public int compareTo(Operand<?> obj)
      Specified by:
      compareTo in interface Comparable<T>
    • getAlias

      public String getAlias()
      Retrieves the name (alias) of the parameter value which can be the name of the operand (Operand) or the option argument (Option), depending on the sub-type inheriting from this interface. The parameter name is merely used for constructing the command line arguments syntax String via Operand.toSyntax(CliContext) and the command line arguments detail description when creating a command line tool's help output. Attention: The alias can also be used as a key when putting an Operand's key/value-pair into a dictionary such as is done by the ApplicationProperties type provided by the refcodes-properties-ext-runime artifact!
      Specified by:
      getAlias in interface org.refcodes.mixin.AliasAccessor
      Specified by:
      getAlias in interface Operand<T>
      Returns:
      The name of the parameter, e.g the name of the operand or the name of the option argument.
    • getType

      public Class<T> getType()
      Specified by:
      getType in interface org.refcodes.mixin.TypeAccessor<T>
    • getValue

      public T getValue()
      Specified by:
      getValue in interface Operand<T>
      Specified by:
      getValue in interface org.refcodes.mixin.ValueAccessor<T>
    • parseArgs

      public Operand<?>[] parseArgs(String[] aArgs, String[] aKeywords, CliContext aCliCtx) throws ArgsSyntaxException
      Parses the given command line arguments using the provided keyword set and CLI context.

      This method represents the core parsing operation. It evaluates this Term or, if this term is an inner node, delegates parsing to its child terms.

      The aKeywords parameter defines reserved tokens such as option names that must not be interpreted as operand values.

      This method does not validate whether all arguments have been consumed. Superfluous arguments must be handled by the caller.

      Specified by:
      parseArgs in interface Term
      Parameters:
      aArgs - The command line arguments to parse
      aKeywords - Reserved option and keyword tokens
      aCliCtx - The CliContext controlling parsing and rendering
      Returns:
      The evaluated operands
      Throws:
      ArgsSyntaxException - If the arguments do not match the syntax
    • getMatchCount

      public int getMatchCount()
      Determines the number of args being matched by the Term.parseArgs(String[], CliContext) (and similar) method(s) of the according syntax. This way we can determine which part of the syntax (tree) had most matches.
      Specified by:
      getMatchCount in interface MatchCountAccessor
      Specified by:
      getMatchCount in interface Term
      Returns:
      The number of args matching the according syntax (tree).
    • reset

      public void reset()
      Specified by:
      reset in interface org.refcodes.mixin.Resetable
    • getParsedArgs

      public String[] getParsedArgs()
      Retrieves the (command line) args which represent this Operand after being "parsed" (provided with command line args).
      Specified by:
      getParsedArgs in interface Operand<T>
      Specified by:
      getParsedArgs in interface ParsedArgsAccessor
      Returns:
      The according args from the command line args.
    • toSchema

      public CliSchema toSchema()
      Specified by:
      toSchema in interface org.refcodes.schema.Schemable
      Specified by:
      toSchema in interface Term
    • toString

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

      protected void approve()
      In case this Term participated in successful parsing command line arguments, then this Term gets approved by the CliArgsParser. An approval my trigger actions such as notifying registered lambda expressions upon approval,
    • setParsedArgs

      protected void setParsedArgs(String[] aArgs)
      Sets the command line argument(s) representing the Operand and its value as parsed by the parseArgs(String[], String[], CliContext) method.
      Parameters:
      aArgs - The command line arguments representing this Operand with its value.
    • setValue

      protected void setValue(T aValue)
      Sets the value for the Operand as parsed by the parseArgs(String[], String[], CliContext) method.
      Parameters:
      aValue - The value to be set for this Operand.
    • toType

      protected abstract T toType(String aArg) throws ParseArgsException
      Double dispatch hook to be implemented by subclasses of the AbstractOperand for converting a command line argument to the required Operand's type. In case conversion failed, then an according exception is to be thrown.
      Parameters:
      aArg - The command line argument to be converted to an instance of the given type T.
      Returns:
      An instance of type T from the provided command line argument.
      Throws:
      ParseArgsException - Thrown in case the provided command line arguments do not respect the required syntax or cannot be converted to the required type.
    • getDescription

      public String getDescription()
      Specified by:
      getDescription in interface org.refcodes.mixin.DescriptionAccessor
    • getException

      public ArgsSyntaxException getException()
      Retrieves the ArgsSyntaxException exception in case invoking Term.parseArgs(String[], CliContext) failed. The exception (and the according suppressed ones alongside the causes) does not necessarily have been thrown by the Term.parseArgs(String[], CliContext) method depending on the semantics of the Term handing the exception (the AnyCondition provides any exceptions it caught even though it did not throw them as of its semantics).
      Specified by:
      getException in interface org.refcodes.exception.ExceptionAccessor<ArgsSyntaxException>
      Specified by:
      getException in interface Term
      Returns:
      The ArgsSyntaxException exception occurring while parsing the arguments.
    • setVisible

      public void setVisible(boolean isVisible)

      Visibility in this context means displaying or hiding this Term's existence to the user (defaults to true).

      Specified by:
      setVisible in interface Term
      Specified by:
      setVisible in interface org.refcodes.graphical.VisibleAccessor.VisibleMutator
    • isVisible

      public boolean isVisible()

      Visibility in this context means displaying or hiding this Term's existence to the user (defaults to true).

      Specified by:
      isVisible in interface Term
      Specified by:
      isVisible in interface org.refcodes.graphical.VisibleAccessor
    • toArgsDiff

      protected static String[] toArgsDiff(String[] aArgs, List<? extends Operand<?>> aArgsSubset)
      Creates the difference between the provided set and the provided Lists therein found argument arrays subset (as of Operand.getParsedArgs()).
      Parameters:
      aArgs - The set to be used for the diff operation.
      aArgsSubset - The subset to be used for the diff operation being the lists containing the Operand instances whose command line arguments are to be diffed.
      Returns:
      The difference between the set and the subset.
    • toArgsDiff

      protected static String[] toArgsDiff(String[] aArgs, Operand<?>[] aArgsSubset)
      Creates the difference between the provided set and the provided Lists therein found argument arrays subset (as of Operand.getParsedArgs()).
      Parameters:
      aArgs - The set to be used for the diff operation.
      aArgsSubset - The subset to be used for the diff operation being the lists containing the Operand instances whose command line arguments are to be diffed.
      Returns:
      The difference between the set and the subset.
    • toArgsDiff

      protected static String[] toArgsDiff(String[] aArgs, String[] aArgsSubset)
      Creates the difference between the provided set and the provided subset.
      Parameters:
      aArgs - The set to be used for the diff operation.
      aArgsSubset - The subset to be used for the diff operation.
      Returns:
      The difference between the set and the subset.
    • toParsedArgs

      protected static String[] toParsedArgs(Operand<?>[]... aOperands)
      Takes all Operand instances found in the provided Lists and adds all therein found argument arrays (as of Operand.getParsedArgs()) to the result.
      Parameters:
      aOperands - The lists containing the Operand instances whose command line arguments are to be added to the result.
      Returns:
      All the command line arguments detected in the provided Operands Lists.