Package com.github.hypfvieh.cli.parser
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
Base class of every command line.
- Since:
- 1.0.0 - 2022-05-05
- Author:
- David M., Markus S.
-
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor with long prefix--and short prefix-. -
Method Summary
Modifier and TypeMethodDescriptionaddOption(CmdArgOption<?> _option) Add an option to the supported options.addOptions(CmdArgOption<?>... _options) Adds multiple options to the supported options.Map<CmdArgOption<?>,String> Returns a unmodifiable Map of all parsed, known but duplicated arguments.Class<? extends RuntimeException>Returns the class of the current configured exception.Map<CmdArgOption<?>,String> Returns a unmodifiable Map of all successfully parsed, known arguments.protected org.slf4j.LoggerReturns the logger instance.protected PatternReturns the current pattern to parse long option names.Returns the current long option name prefix.CmdArgOption<?>getOption(CharSequence _optionName) Returns a option using its name.Map<String,CmdArgOption<?>> Returns a unmodifiable Map of all configured options.protected PatternReturns the current pattern to parse short option names.Returns the current short option name prefix.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 a unmodifiable list of all unknown option arguments.Creates the "usage" String using the configured formatter.protected booleanhasArg(Function<B, CmdArgOption<?>> _argFunction) Tries to find aCmdArgOptionusing the given function and checks if the option was used in the parsed command line.booleanhasOption(CmdArgOption<?> _option) Checks if the given option is already present.booleanhasOption(CharSequence _optionName) Checks if there is any option with the given name.booleanReturns true when command line parsing fails on duplicated arguments.booleanReturns true when command line parsing fails on unknown arguments.booleanReturns true when command line parsing fails on unknown tokens.protected booleanisParsed()Signals if the command line has been parsed.protected BPrints some debug statements to the configured logger.voidPrints the "usage" of the program to stdout.voidprintUsage(String _mainClassName, OutputStream _output) Prints the "usage" of the program to the given output.<T> BregisterConverter(Class<T> _type, IValueConverter<T> _converter) Registers a converter to convert an option argument String to a specific java object type.protected abstract Bself()A reference to ourselves to allow chaining with subclasses.protected voidsetParsed(boolean _b) Set the parsed state.withExceptionType(Class<? extends RuntimeException> _exceptionType) Set a RuntimeException based exception class thrown when command line parsing fails.withFailOnDupArg(boolean _failOnDupArg) Specifies if command line parsing should fail when an duplicate argument was found.withFailOnUnknownArg(boolean _failOnUnknownArg) Specifies if command line parsing should fail when an unknown argument was found.withFailOnUnknownToken(boolean _failOnUnknownToken) Specifies if command line parsing should fail when an unknown token was found.withLongOptPrefix(String _prefix) Defines the prefix for long-option names.withShortOptPrefix(String _prefix) Defines the prefix for short-option names.withUsageFormatter(IUsageFormatter _formatter) Setup a differentIUsageFormatter.
-
Constructor Details
-
AbstractBaseCommandLine
public AbstractBaseCommandLine()Default constructor with long prefix--and short prefix-.
-
-
Method Details
-
self
A reference to ourselves to allow chaining with subclasses.- Returns:
- this
-
registerConverter
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
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
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
Prints some debug statements to the configured logger.- Returns:
- this
-
hasOption
Checks if there is any option with the given name.- Parameters:
_optionName- option name to check- Returns:
- true if present, false otherwise
-
hasOption
Checks if the given option is already present.- Parameters:
_option- option to check- Returns:
- true if present, false otherwise
-
getOptions
Returns a unmodifiable Map of all configured options.- Returns:
- unmodifiable Map, never null
-
getOption
Returns a option using its name.- Parameters:
_optionName- option name- Returns:
- option, maybe null
-
getKnownArgs
Returns a unmodifiable Map of all successfully parsed, known arguments.- Returns:
- unmodifiable Map, never null
-
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
Returns a unmodifiable list of all unknown option arguments.- Returns:
- unmodifiable list, never null
-
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
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
Creates the "usage" String using the configured formatter.- Parameters:
_mainClassName- name of program or main class- Returns:
- usage String
-
withUsageFormatter
Setup a differentIUsageFormatter.- Parameters:
_formatter- formatter, null will be ignored- Returns:
- this
-
withFailOnUnknownArg
Specifies if command line parsing should fail when an unknown argument was found.Default: true
- Parameters:
_failOnUnknownArg- true to enable- Returns:
- this
-
withFailOnUnknownToken
Specifies if command line parsing should fail when an unknown token was found.Default: true
- Parameters:
_failOnUnknownToken- true to enable- Returns:
- this
-
withFailOnDupArg
Specifies if command line parsing should fail when an duplicate argument was found.Default: true
- Parameters:
_failOnDupArg- true to enable- Returns:
- this
-
withShortOptPrefix
Defines the prefix for short-option names.Default: -
- Parameters:
_prefix- prefix for short options- Returns:
- this
-
withLongOptPrefix
Defines the prefix for long-option names.Default: --
- Parameters:
_prefix- prefix for long options- Returns:
- this
-
withExceptionType
Set a RuntimeException based exception class thrown when command line parsing fails.Default:
CommandLineException- Parameters:
_exceptionType- class, never null- Returns:
- this
-
getShortOptPrefix
Returns the current short option name prefix.- Returns:
- String
-
getLongOptPrefix
Returns the current long option name prefix.- Returns:
- String
-
getLongOptPattern
Returns the current pattern to parse long option names.- Returns:
- Pattern
-
getShortOptPattern
Returns the current pattern to parse short option names.- Returns:
- Pattern
-
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
Tries to find aCmdArgOptionusing 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
-