Class CommandLine
CmdArgOptions.
Example of working with a command-line with two options and a custom exception type:
CommandLine commandLine = new CommandLine()
.setExceptionType(MyApplicationException.class)
.addOption(Option.builder(String.class)
.name("req1")
.required(true)
.defaultValue("default")
.build())
.addOption(Option.builder()
.name("opt1")
.required(false)
.build())
.parse();
- Since:
- 1.0.0 - 2022-04-29
- Author:
- David M., Markus S.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiongetArg(char _optionName) Returns an option value using the options short name.<T> TReturns an option value using the options short name and converting the value to the given type.<T> TReturns an option value using the options short name and converting the value to the given type.<T> TgetArg(CmdArgOption<T> _option) Returns the value associated with argument option.<T> TgetArg(CmdArgOption<T> _option, T _default) Returns the value associated with argument option.getArg(CharSequence _optionName) Returns an option value using the options name.<T> TgetArg(CharSequence _optionName, Class<T> _type) Returns an option value using the options name and converting the value to the given type.<T> TgetArg(CharSequence _optionName, Class<T> _type, T _default) Returns an option value using the options name and converting the value to the given type.intgetArgCount(CmdArgOption<?> _option) Returns the number of occurrences of the given option.<T> List<T>Returns all option values using the options short name and converting the values to the given type.<T> List<T>getArgs(CmdArgOption<T> _option) Returns the value associated with argument option.<T> List<T>getArgs(CmdArgOption<T> _option, T _default) Returns the value associated with argument option.<T> List<T>getArgs(CharSequence _optionName, Class<T> _type) Returns all option values using the options name and converting the values to the given type.booleanhasArg(char _arg) Checks if the given option was at least used once in the command line.booleanhasArg(CmdArgOption<?> _option) Checks if the given option was at least used once in the command line.booleanChecks if the given option was at least used once in the command line.Parses the given arguments.protected CommandLineself()Reference to ourselves for chaining.Methods inherited from class com.github.hypfvieh.cli.parser.AbstractBaseCommandLine
addOption, addOptions, getDupArgs, getExceptionType, getKnownArgs, getLogger, getLongOptPattern, getLongOptPrefix, getOption, getOptions, getShortOptPattern, getShortOptPrefix, getUnknownArgs, getUnknownTokens, getUsage, hasArg, hasOption, hasOption, isFailOnDupArg, isFailOnUnknownArg, isFailOnUnknownToken, isParsed, logResults, printUsage, printUsage, registerConverter, setParsed, withExceptionType, withFailOnDupArg, withFailOnUnknownArg, withFailOnUnknownToken, withLongOptPrefix, withShortOptPrefix, withUsageFormatter
-
Constructor Details
-
CommandLine
public CommandLine()
-
-
Method Details
-
self
Reference to ourselves for chaining.- Specified by:
selfin classAbstractBaseCommandLine<CommandLine>- Returns:
- this
-
parse
Parses the given arguments.- Parameters:
_args- arguments to read- Returns:
- this
-
getArg
Returns the value associated with argument option.If no value is present, the default value of that option is returned (and might by
null).
If the option does not support values or option was not set, null is returned.
If the option is a repeatable option, the value of the first occurrence is returned.- Type Parameters:
T- type of option value- Parameters:
_option- option- Returns:
- value, maybe
null
-
getArg
Returns the value associated with argument option.If no value is present, the given default value is used.
If the given default is alsonull, the default of that option is returned (and might bynull). If the option does not support values or if the option was not set,nullis returned.
- Type Parameters:
T- type of option value- Parameters:
_option- option_default- default to use when no value present (overrides default specified in option)- Returns:
- value, maybe
null
-
getArgs
Returns the value associated with argument option.If no value is present, the default value of that option is returned (and might by
null). If the option does not support values or if the option was not set,nullis returned.
- Type Parameters:
T- type of option value- Parameters:
_option- option- Returns:
- List, maybe empty or
null
-
getArgs
Returns the value associated with argument option.If no value is present, the given default value is used.
If the given default is alsonull, the default of that option is returned (and might bynull). If the option does not support values or if the option was not set,nullis returned.
- Type Parameters:
T- type of option value- Parameters:
_option- option_default- default to use when no value present (overrides default specified in option)- Returns:
- List, maybe empty or
null
-
getArg
Returns an option value using the options name.- Parameters:
_optionName- option name- Returns:
- value or null if option has no value
- Throws:
RuntimeException- if option is unknown or command line was not parsed before
-
getArg
Returns an option value using the options short name.- Parameters:
_optionName- option short name- Returns:
- value or null if option has no value
- Throws:
RuntimeException- if option is unknown or command line was not parsed before
-
getArg
Returns an option value using the options name and converting the value to the given type.
Will use the configured converter to convert the value.
If given type is not the same as the type specified inCmdArgOptionan exception is thrown.- Type Parameters:
T- data type of argument- Parameters:
_optionName- option short name_type- expected value type_default- default to use if option not set (and not required)- Returns:
- value or null if option has no value
- Throws:
RuntimeException- if option is unknown or command line was not parsedRuntimeException- if type class is not the type defined inCmdArgOption- Since:
- 1.0.1 - 2022-05-24
-
getArg
Returns an option value using the options name and converting the value to the given type.
Will use the configured converter to convert the value.
If given type is not the same as the type specified inCmdArgOptionan exception is thrown.- Type Parameters:
T- data type of argument- Parameters:
_optionName- option short name_type- expected value type- Returns:
- value or null if option has no value
- Throws:
RuntimeException- if option is unknown or command line was not parsedRuntimeException- if type class is not the type defined inCmdArgOption- Since:
- 1.0.1 - 2022-05-24
-
getArg
Returns an option value using the options short name and converting the value to the given type.
Will use the configured converter to convert the value.
If given type is not the same as the type specified inCmdArgOptionan exception is thrown.- Type Parameters:
T- type of option value- Parameters:
_optionName- option short name_type- expected value type- Returns:
- value or null if option has no value
- Throws:
RuntimeException- if option is unknown or command line was not parsedRuntimeException- if type class is not the type defined inCmdArgOption- Since:
- 1.0.1 - 2022-05-24
-
getArg
Returns an option value using the options short name and converting the value to the given type.
Will use the configured converter to convert the value.
If given type is not the same as the type specified inCmdArgOptionan exception is thrown.- Type Parameters:
T- data type of argument- Parameters:
_optionName- option short name_type- expected value type_default- default to use if option not set (and not required)- Returns:
- value or null if option has no value
- Throws:
RuntimeException- if option is unknown or command line was not parsedRuntimeException- if type class is not the type defined inCmdArgOption- Since:
- 1.0.1 - 2022-05-24
-
getArgs
Returns all option values using the options name and converting the values to the given type.
Will use the configured converter to convert the value.
If given type is not the same as the type specified inCmdArgOptionan exception is thrown.- Type Parameters:
T- data type of argument- Parameters:
_optionName- option short name_type- expected value type- Returns:
- value or null if option has no value
- Throws:
RuntimeException- if option is unknown or command line was not parsedRuntimeException- if type class is not the type defined inCmdArgOption- Since:
- 1.0.1 - 2022-05-24
-
getArgs
Returns all option values using the options short name and converting the values to the given type.
Will use the configured converter to convert the value.
If given type is not the same as the type specified inCmdArgOptionan exception is thrown.- Type Parameters:
T- data type of argument- Parameters:
_optionName- option short name_type- expected value type- Returns:
- value or null if option has no value
- Throws:
RuntimeException- if option is unknown or command line was not parsedRuntimeException- if type class is not the type defined inCmdArgOption- Since:
- 1.0.1 - 2022-05-24
-
hasArg
Checks if the given option was at least used once in the command line.- Parameters:
_option- option to check- Returns:
- true if it was used at least once, false otherwise
-
hasArg
Checks if the given option was at least used once in the command line.Will only check if the string argument was found as option name, will not check with short name.
- Parameters:
_option- option to check- Returns:
- true if it was used at least once, false otherwise
-
hasArg
public boolean hasArg(char _arg) Checks if the given option was at least used once in the command line.Will only check if the char argument was found as short option.
- Parameters:
_option- option to check- Returns:
- true if it was used at least once, false otherwise
-
getArgCount
Returns the number of occurrences of the given option.If the option was never set, 0 is returned.
- Parameters:
_option- option- Returns:
- number of occurrences
-