Class AbstractBaseCommandLine<B extends AbstractBaseCommandLine<?>>

java.lang.Object
com.github.hypfvieh.cli.parser.AbstractBaseCommandLine<B>
Type Parameters:
B - concrete command line implementation
Direct Known Subclasses:
CommandLine

public abstract class AbstractBaseCommandLine<B extends AbstractBaseCommandLine<?>> extends Object
Base class of every command line.
Since:
1.0.0 - 2022-05-05
Author:
David M., Markus S.
  • Constructor Details

    • AbstractBaseCommandLine

      public AbstractBaseCommandLine()
      Default constructor with long prefix -- and short prefix -.
  • Method Details

    • self

      protected abstract B self()
      A reference to ourselves to allow chaining with subclasses.
      Returns:
      this
    • registerConverter

      public <T> B registerConverter(Class<T> _type, IValueConverter<T> _converter)
      Registers a converter to convert an option argument String to a specific java object type.
      Type Parameters:
      T - type
      Parameters:
      _type - java class to convert to
      _converter - converter instance
      Returns:
      this
    • setParsed

      protected void setParsed(boolean _b)
      Set the parsed state.
      Parameters:
      _b - true to signal that commandline was parsed
    • addOption

      public B addOption(CmdArgOption<?> _option)
      Add an option to the supported options.
      Parameters:
      _option - option, never null
      Returns:
      this
      Throws:
      RuntimeException - when option is not unique (short/long name was used by another option)
    • addOptions

      public B addOptions(CmdArgOption<?>... _options)
      Adds multiple options to the supported options.
      Parameters:
      _options - options to add
      Returns:
      this
      Throws:
      RuntimeException - when option is not unique (short/long name was used by another option)
    • logResults

      protected B logResults()
      Prints some debug statements to the configured logger.
      Returns:
      this
    • hasOption

      public boolean hasOption(CharSequence _optionName)
      Checks if there is any option with the given name.
      Parameters:
      _optionName - option name to check
      Returns:
      true if present, false otherwise
    • hasOption

      public boolean hasOption(CmdArgOption<?> _option)
      Checks if the given option is already present.
      Parameters:
      _option - option to check
      Returns:
      true if present, false otherwise
    • getOptions

      public Map<String,CmdArgOption<?>> getOptions()
      Returns a unmodifiable Map of all configured options.
      Returns:
      unmodifiable Map, never null
    • getOption

      public CmdArgOption<?> getOption(CharSequence _optionName)
      Returns a option using its name.
      Parameters:
      _optionName - option name
      Returns:
      option, maybe null
    • getKnownArgs

      public Map<CmdArgOption<?>,String> getKnownArgs()
      Returns a unmodifiable Map of all successfully parsed, known arguments.
      Returns:
      unmodifiable Map, never null
    • getUnknownArgs

      public Map<String,String> getUnknownArgs()
      Returns a unmodifiable Map of all parsed, but unknown arguments.
      The value of the map will represent the parsed value, or null if no value found.
      Returns:
      unmodifiable Map, never null
    • getUnknownTokens

      public List<String> getUnknownTokens()
      Returns a unmodifiable list of all unknown option arguments.
      Returns:
      unmodifiable list, never null
    • getDupArgs

      public Map<CmdArgOption<?>,String> getDupArgs()
      Returns a unmodifiable Map of all parsed, known but duplicated arguments.
      The value of the map will represent the parsed value, or null if no value found.
      Only arguments which are not repeatable will be added to the duplicate list.
      Returns:
      unmodifiable Map, never null
    • printUsage

      public void printUsage()
      Prints the "usage" of the program to stdout. Will try to find the main class using the current stack.
    • printUsage

      public void printUsage(String _mainClassName, OutputStream _output)
      Prints the "usage" of the program to the given output.
      Parameters:
      _mainClassName - name of program or main class
      _output - output stream to write to
    • getUsage

      public String getUsage(String _mainClassName)
      Creates the "usage" String using the configured formatter.
      Parameters:
      _mainClassName - name of program or main class
      Returns:
      usage String
    • withUsageFormatter

      public B withUsageFormatter(IUsageFormatter _formatter)
      Setup a different IUsageFormatter.
      Parameters:
      _formatter - formatter, null will be ignored
      Returns:
      this
    • withFailOnUnknownArg

      public B withFailOnUnknownArg(boolean _failOnUnknownArg)
      Specifies if command line parsing should fail when an unknown argument was found.

      Default: true

      Parameters:
      _failOnUnknownArg - true to enable
      Returns:
      this
    • withFailOnUnknownToken

      public B withFailOnUnknownToken(boolean _failOnUnknownToken)
      Specifies if command line parsing should fail when an unknown token was found.

      Default: true

      Parameters:
      _failOnUnknownToken - true to enable
      Returns:
      this
    • withFailOnDupArg

      public B withFailOnDupArg(boolean _failOnDupArg)
      Specifies if command line parsing should fail when an duplicate argument was found.

      Default: true

      Parameters:
      _failOnDupArg - true to enable
      Returns:
      this
    • withShortOptPrefix

      public B withShortOptPrefix(String _prefix)
      Defines the prefix for short-option names.

      Default: -

      Parameters:
      _prefix - prefix for short options
      Returns:
      this
    • withLongOptPrefix

      public B withLongOptPrefix(String _prefix)
      Defines the prefix for long-option names.

      Default: --

      Parameters:
      _prefix - prefix for long options
      Returns:
      this
    • withExceptionType

      public B withExceptionType(Class<? extends RuntimeException> _exceptionType)
      Set a RuntimeException based exception class thrown when command line parsing fails.

      Default: CommandLineException

      Parameters:
      _exceptionType - class, never null
      Returns:
      this
    • getShortOptPrefix

      public String getShortOptPrefix()
      Returns the current short option name prefix.
      Returns:
      String
    • getLongOptPrefix

      public String getLongOptPrefix()
      Returns the current long option name prefix.
      Returns:
      String
    • getLongOptPattern

      protected Pattern getLongOptPattern()
      Returns the current pattern to parse long option names.
      Returns:
      Pattern
    • getShortOptPattern

      protected Pattern getShortOptPattern()
      Returns the current pattern to parse short option names.
      Returns:
      Pattern
    • getExceptionType

      public Class<? extends RuntimeException> getExceptionType()
      Returns the class of the current configured exception.
      Returns:
      Class, never null
    • getLogger

      protected org.slf4j.Logger getLogger()
      Returns the logger instance.
      Returns:
      Logger
    • isParsed

      protected boolean isParsed()
      Signals if the command line has been parsed.
      Returns:
      true if it was parsed, false otherwise
    • isFailOnDupArg

      public boolean isFailOnDupArg()
      Returns true when command line parsing fails on duplicated arguments.
      Returns:
      boolean
    • isFailOnUnknownArg

      public boolean isFailOnUnknownArg()
      Returns true when command line parsing fails on unknown arguments.
      Returns:
      boolean
    • isFailOnUnknownToken

      public boolean isFailOnUnknownToken()
      Returns true when command line parsing fails on unknown tokens.
      Returns:
      boolean
    • hasArg

      protected boolean hasArg(Function<B,CmdArgOption<?>> _argFunction)
      Tries to find a CmdArgOption using the given function and checks if the option was used in the parsed command line.
      Parameters:
      _argFunction - argument supplier (never null)
      Returns:
      true if command was found and it was found in parsed command line, false otherwise