Package com.github.hypfvieh.cli.parser
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
Base class of every command line.
- Seit:
- 1.0.0 - 2022-05-05
- Autor:
- David M., Markus S.
-
Konstruktorübersicht
KonstruktorenKonstruktorBeschreibungDefault constructor with long prefix--and short prefix-. -
Methodenübersicht
Modifizierer und TypMethodeBeschreibungaddOption(CmdArgOption<?> _option) Add an option to the supported options.addOptions(CmdArgOption<?>... _options) Adds multiple options to the supported options.getArgumentHelp(String _mainClassName) Creates a multiline String containing all options and descriptions configured.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 System.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.withHelpFormatter(IUsageFormatter _formatter) Setup a different help formatter.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 different usage formatter.
-
Konstruktordetails
-
AbstractBaseCommandLine
public AbstractBaseCommandLine()Default constructor with long prefix--and short prefix-.
-
-
Methodendetails
-
self
A reference to ourselves to allow chaining with subclasses.- Gibt zurück:
- this
-
registerConverter
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
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
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
Prints some debug statements to the configured logger.- Gibt zurück:
- this
-
hasOption
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
Checks if the given option is already present.- Parameter:
_option- option to check- Gibt zurück:
- true if present, false otherwise
-
getOptions
Returns a unmodifiable Map of all configured options.- Gibt zurück:
- unmodifiable Map, never null
-
getOption
Returns a option using its name.- Parameter:
_optionName- option name- Gibt zurück:
- option, maybe null
-
getKnownArgs
Returns a unmodifiable Map of all successfully parsed, known arguments.- Gibt zurück:
- 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.- Gibt zurück:
- unmodifiable Map, never null
-
getUnknownTokens
Returns a unmodifiable list of all unknown option arguments.- Gibt zurück:
- 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.- 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
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
Creates the "usage" String using the configured formatter.- Parameter:
_mainClassName- name of program or main class- Gibt zurück:
- usage String
-
getArgumentHelp
Creates a multiline String containing all options and descriptions configured.
Indented to be used in "help" like commands.
Uses the configuredhelpFormatter.- Parameter:
_mainClassName- name of program or main class- Gibt zurück:
- usage String
-
withUsageFormatter
Setup a different usage formatter.- Parameter:
_formatter- formatter, null will be ignored- Gibt zurück:
- this
-
withHelpFormatter
Setup a different help formatter.- Parameter:
_formatter- formatter, null will be ignored- Gibt zurück:
- this
-
withFailOnUnknownArg
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
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
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
Defines the prefix for short-option names.Default: -
- Parameter:
_prefix- prefix for short options- Gibt zurück:
- this
-
withLongOptPrefix
Defines the prefix for long-option names.Default: --
- Parameter:
_prefix- prefix for long options- Gibt zurück:
- this
-
withExceptionType
Set a RuntimeException based exception class thrown when command line parsing fails.Default:
CommandLineException- Parameter:
_exceptionType- class, never null- Gibt zurück:
- this
-
getShortOptPrefix
Returns the current short option name prefix.- Gibt zurück:
- String
-
getLongOptPrefix
Returns the current long option name prefix.- Gibt zurück:
- String
-
getLongOptPattern
Returns the current pattern to parse long option names.- Gibt zurück:
- Pattern
-
getShortOptPattern
Returns the current pattern to parse short option names.- Gibt zurück:
- Pattern
-
getExceptionType
Returns the class of the current configured exception.- Gibt zurück:
- Class, never null
-
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
Tries to find aCmdArgOptionusing 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
-