Klasse AbstractBaseCommandLine<B extends AbstractBaseCommandLine<?>>

java.lang.Object
com.github.hypfvieh.cli.parser.AbstractBaseCommandLine<B>
Typparameter:
B - concrete command line implementation
Bekannte direkte Unterklassen:
CommandLine

public abstract class AbstractBaseCommandLine<B extends AbstractBaseCommandLine<?>> extends Object
Base class of every command line.
Seit:
1.0.0 - 2022-05-05
Autor:
David M., Markus S.
  • Konstruktordetails

    • AbstractBaseCommandLine

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

    • self

      protected abstract B self()
      A reference to ourselves to allow chaining with subclasses.
      Gibt zurück:
      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.
      Typparameter:
      T - type
      Parameter:
      _type - java class to convert to
      _converter - converter instance
      Gibt zurück:
      this
    • setParsed

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

      public B addOption(CmdArgOption<?> _option)
      Add an option to the supported options.
      Parameter:
      _option - option, never null
      Gibt zurück:
      this
      Löst aus:
      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.
      Parameter:
      _options - options to add
      Gibt zurück:
      this
      Löst aus:
      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.
      Gibt zurück:
      this
    • hasOption

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

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

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

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

      public Map<CmdArgOption<?>,String> getKnownArgs()
      Returns a unmodifiable Map of all successfully parsed, known arguments.
      Gibt zurück:
      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.
      Gibt zurück:
      unmodifiable Map, never null
    • getUnknownTokens

      public List<String> getUnknownTokens()
      Returns a unmodifiable list of all unknown option arguments.
      Gibt zurück:
      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.
      Gibt zurück:
      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.
      Parameter:
      _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.
      Parameter:
      _mainClassName - name of program or main class
      Gibt zurück:
      usage String
    • getArgumentHelp

      public String getArgumentHelp(String _mainClassName)
      Creates a multiline String containing all options and descriptions configured.

      Indented to be used in "help" like commands.
      Uses the configured helpFormatter.

      Parameter:
      _mainClassName - name of program or main class
      Gibt zurück:
      usage String
    • withUsageFormatter

      public B withUsageFormatter(IUsageFormatter _formatter)
      Setup a different usage formatter.
      Parameter:
      _formatter - formatter, null will be ignored
      Gibt zurück:
      this
    • withHelpFormatter

      public B withHelpFormatter(IUsageFormatter _formatter)
      Setup a different help formatter.
      Parameter:
      _formatter - formatter, null will be ignored
      Gibt zurück:
      this
    • withFailOnUnknownArg

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

      Default: true

      Parameter:
      _failOnUnknownArg - true to enable
      Gibt zurück:
      this
    • withFailOnUnknownToken

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

      Default: true

      Parameter:
      _failOnUnknownToken - true to enable
      Gibt zurück:
      this
    • withFailOnDupArg

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

      Default: true

      Parameter:
      _failOnDupArg - true to enable
      Gibt zurück:
      this
    • withShortOptPrefix

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

      Default: -

      Parameter:
      _prefix - prefix for short options
      Gibt zurück:
      this
    • withLongOptPrefix

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

      Default: --

      Parameter:
      _prefix - prefix for long options
      Gibt zurück:
      this
    • withExceptionType

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

      Default: CommandLineException

      Parameter:
      _exceptionType - class, never null
      Gibt zurück:
      this
    • getShortOptPrefix

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

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

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

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

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

      protected System.Logger getLogger()
      Returns the logger instance.
      Gibt zurück:
      Logger
    • isParsed

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

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

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

      public boolean isFailOnUnknownToken()
      Returns true when command line parsing fails on unknown tokens.
      Gibt zurück:
      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.
      Parameter:
      _argFunction - argument supplier (never null)
      Gibt zurück:
      true if command was found and it was found in parsed command line, false otherwise