Class CliContext

java.lang.Object
org.refcodes.cli.CliContext
All Implemented Interfaces:
ArgumentEscapeCodeAccessor, CliMetrics, KeywordEscapeCodeAccessor, SyntaxMetricsAccessor, org.refcodes.mixin.EscapeCodesStatusAccessor, org.refcodes.mixin.ResetEscapeCodeAccessor

public class CliContext extends Object implements CliMetrics
The CliContext describes the context in which an ArgsParser is running. The CliContext may depend on the operating system or user settings applied, when running the ArgsParser implementations.
  • Constructor Details

    • CliContext

      public CliContext()
      Constructs an accordingly preconfigured CliContext instance.
    • CliContext

      public CliContext(boolean isEscapeCodesEnabled)
      Constructs an accordingly configured CliContext instance.
      Parameters:
      isEscapeCodesEnabled - Determines whether using escape codes when rendering a Term node's syntax.
    • CliContext

      public CliContext(CliMetrics aCliMetrics)
      Constructs an accordingly configured CliContext instance.
      Parameters:
      aCliMetrics - The CliMetrics describing the CliContext.
    • CliContext

      public CliContext(CliMetrics aCliMetrics, SyntaxMetrics aSyntaxMetrics)
      Constructs an accordingly configured CliContext instance.
      Parameters:
      aCliMetrics - The CliMetrics describing the CliContext.
      aSyntaxMetrics - When being provided, then it overrides the SyntaxMetrics provided by the CliMetrics.
    • CliContext

      public CliContext(CliMetrics aCliMetrics, SyntaxMetrics aSyntaxMetrics, boolean isEscapeCodesEnabled)
      Constructs an accordingly configured CliContext instance.
      Parameters:
      aCliMetrics - The CliMetrics describing the CliContext.
      aSyntaxMetrics - When being provided, then it overrides the SyntaxMetrics provided by the CliMetrics.
      isEscapeCodesEnabled - Overrides the according EscapeCodesStatusAccessor.isEscapeCodesEnabled() attribute. This is useful when using the CliContext instance for generating exception massages which must not have ANSI escape codes.
    • CliContext

      public CliContext(String aArgumentEscapeCode, String aKeywordEscapeCode, String aResetEscapeCode)
      Constructs an accordingly configured CliContext instance.
      Parameters:
      aArgumentEscapeCode - The escape code to use when rendering (option's) arguments
      aKeywordEscapeCode - The escape code to be used when rendering keywords (options.)
      aResetEscapeCode - The escape code resetting any previously applied escape codes.
    • CliContext

      public CliContext(SyntaxMetrics aSyntaxMetrics)
      Constructs an accordingly configured CliContext instance.
      Parameters:
      aSyntaxMetrics - The SyntaxMetrics describing the syntax notation to be used such as the SyntaxNotation definitions.
    • CliContext

      public CliContext(SyntaxMetrics aSyntaxMetrics, boolean isEscapeCodesEnabled)
      Constructs an accordingly configured CliContext instance.
      Parameters:
      aSyntaxMetrics - The SyntaxMetrics describing the syntax notation to be used such as the SyntaxNotation definitions.
      isEscapeCodesEnabled - Determines whether using escape codes when rendering a Term node's syntax.
    • CliContext

      public CliContext(SyntaxMetrics aSyntaxMetrics, Character aShortOptionPrefix, String aLongOptionPrefix, boolean isEscapeCodesEnabled, String aArgumentEscapeCode, String aKeywordEscapeCode, String aResetEscapeCode)
      Constructs an accordingly configured CliContext instance.
      Parameters:
      aSyntaxMetrics - The SyntaxMetrics describing the syntax notation to be used such as the SyntaxNotation definitions.
      aShortOptionPrefix - The short option prefix to be used when parsing Option elements.
      aLongOptionPrefix - The long option prefix to be used when parsing Option elements.
      isEscapeCodesEnabled - Determines whether using escape codes when rendering a Term node's syntax.
      aArgumentEscapeCode - The escape code to use when rendering (option's) arguments
      aKeywordEscapeCode - The escape code to be used when rendering options.
      aResetEscapeCode - The escape code resetting any previously applied escape codes.
    • CliContext

      public CliContext(SyntaxMetrics aSyntaxMetrics, String aArgumentEscapeCode, String aKeywordEscapeCode, String aResetEscapeCode)
      Constructs an accordingly configured CliContext instance.
      Parameters:
      aSyntaxMetrics - The SyntaxMetrics describing the syntax notation to be used such as the SyntaxNotation definitions.
      aArgumentEscapeCode - The escape code to use when rendering (option's) arguments
      aKeywordEscapeCode - The escape code to be used when rendering options.
      aResetEscapeCode - The escape code resetting any previously applied escape codes.
  • Method Details

    • getArgumentEscapeCode

      public String getArgumentEscapeCode()
      Retrieves the argument Escape-Code from the argument Escape-Code property.
      Specified by:
      getArgumentEscapeCode in interface ArgumentEscapeCodeAccessor
      Returns:
      The argument Escape-Code stored by the argument Escape-Code property.
    • getKeywordEscapeCode

      public String getKeywordEscapeCode()
      Retrieves the keyword (e.g. short- and long-options) Escape-Code from the option Escape-Code property.
      Specified by:
      getKeywordEscapeCode in interface KeywordEscapeCodeAccessor
      Returns:
      The keyword (e.g. short- and long-options) Escape-Code stored by the option Escape-Code property.
    • getResetEscapeCode

      public String getResetEscapeCode()
      Specified by:
      getResetEscapeCode in interface org.refcodes.mixin.ResetEscapeCodeAccessor
    • getSyntaxMetrics

      public SyntaxMetrics getSyntaxMetrics()
      Retrieves the SyntaxMetrics from the SyntaxMetrics property.
      Specified by:
      getSyntaxMetrics in interface SyntaxMetricsAccessor
      Returns:
      The SyntaxMetrics stored by the decorator (enabled) tags property.
    • isEscapeCodesEnabled

      public boolean isEscapeCodesEnabled()
      Specified by:
      isEscapeCodesEnabled in interface org.refcodes.mixin.EscapeCodesStatusAccessor
    • isOption

      public boolean isOption(String aArg)
      Determines whether the given argument is an option in terms of this context, e.g. whether it starts either with a short option prefix (as of ShortOptionPrefixAccessor.getShortOptionPrefix() or a long option prefix (as of LongOptionPrefixAccessor.getLongOptionPrefix()).
      Parameters:
      aArg - The argument to be tested whether it matches the option pattern as of this context.
      Returns:
      True in case the argument may be a short option or a long option, e.g. it is prefixed accordingly.
    • toKeywords

      public String[] toKeywords(Term aTerm)
      Determines all keywords (e.g. short- and long-options) found in the provided Term node and its children.
      Parameters:
      aTerm - The element (with its children if applicable) from which to determine the options.
      Returns:
      An array containing all found options.
    • toArgumentEscapeCode

      public String toArgumentEscapeCode()
      Constructs the argument escape code depending on the escape code status (as of isEscapeCodesEnabled()) and whether the argument escape code is not null or null (as of getArgumentEscapeCode()). In case escape codes are enabled and the argument escape code is set, then the argument escape code is returned, else an empty String.
      Returns:
      The argument escape code or an empty String depending on the escape code status and the argument escape code's value.
    • toExpandOptions

      public String[] toExpandOptions(String[] aArgs)
      Expands the provided args' short options and returns the resulting args. A short option prefix may be used to denote multiple short options, given a short option prefix being used is "-", then the argument "-xyz" is expanded to be "-x", "-y" and "-z".
      Parameters:
      aArgs - The args to be expanded.
      Returns:
      The expanded args.
    • toKeywordEscapeCode

      public String toKeywordEscapeCode()
      Constructs the keyword escape code depending on the escape code status (as of isEscapeCodesEnabled()) and whether the keyword escape code is not null (as of getKeywordEscapeCode()). In case escape codes are enabled and the keyword escape code is set, then the keyword escape code is returned, else an empty String.
      Returns:
      The keyword escape code or an empty String depending on the escape code status and the keyword escape code's value.
    • toResetEscapeCode

      public String toResetEscapeCode()
      Constructs the reset escape code depending on the escape code status (as of isEscapeCodesEnabled()) and whether the reset escape code is not null or null (as of getResetEscapeCode()). In case escape codes are enabled and the reset escape code is set, then the reset escape code is returned, else an empty String.
      Returns:
      The reset escape code or an empty String depending on the escape code status and the reset escape code's value.
    • builder

      public static CliContext.Builder builder()
      Creates a CliContext.Builder used to fluently construct CliContext instances.
      Returns:
      The CliContext.Builder for constructing CliContext instances.