Class Option<T,I extends Option<?,I>>
java.lang.Object
eu.svjatoslav.commons.cli_helper.parameter_parser.Option<T,I>
- Type Parameters:
T
- the type of object returned bygetValue()
I
- the actual subclass type, used to enable fluent method chaining
- Direct Known Subclasses:
DirectoryOption
,DirectoryOptions
,FileOption
,FileOptions
,FloatOption
,IntegerOption
,NullOption
,StringOption
,StringOptions
Represents a command-line option (a flag or switch), which can be configured with:
- whether it's mandatory
- the number of parameters accepted
- a textual description
- aliases (the actual CLI flags, e.g., "--help" or "-h")
-
Field Summary
FieldsModifier and TypeFieldDescriptionfinal String
Human-readable purpose of this option (e.g., "Input image path").protected boolean
Whether this option is mandatory (i.e., must appear at least once on the CLI).The list of parameters provided on the command line for this option (if any). -
Constructor Summary
ConstructorsConstructorDescriptionOption
(boolean mandatory, ParameterCount parameterCount, String description, String... aliases2) Fully-custom constructor for an option.Option
(String description, ParameterCount parameterCount, String... aliases2) Simpler constructor that defaults to a non-mandatory option. -
Method Summary
Modifier and TypeMethodDescriptionaddAliases
(String... aliasArray) Adds additional aliases to this option for user convenience.boolean
addParameter
(String parameterString) Adds a parameter to this option, validating it againstisValid(String)
.abstract String
Describes the kind or format of the expected parameter(s), e.g.Returns this option's aliases as a comma-separated string (e.g.getHelp()
Returns a help message that includes the aliases, whether the option is mandatory, the format, and a brief description.abstract T
getValue()
Returns the parsed value (usually constructed fromparameters
).boolean
boolean
abstract boolean
Checks if a single parameter string is valid for this option.boolean
matchesAlias
(String alias) Checks if a given alias matches this option.boolean
Called when no more arguments can be added to this option, giving it a chance to verify correct usage (e.g., check if required parameters are missing).Sets this option as mandatory.protected void
setPresent
(boolean present) Marks this option as present or not present.
-
Field Details
-
description
Human-readable purpose of this option (e.g., "Input image path"). For a description of the parameter format (e.g., "file", "integer"), seedescribeFormat()
. -
parameters
The list of parameters provided on the command line for this option (if any). -
mandatory
protected boolean mandatoryWhether this option is mandatory (i.e., must appear at least once on the CLI).
-
-
Constructor Details
-
Option
public Option(boolean mandatory, ParameterCount parameterCount, String description, String... aliases2) Fully-custom constructor for an option.- Parameters:
mandatory
-true
if the option is required,false
otherwiseparameterCount
- the number of parameters required: NONE, ONE, or ONE_OR_MOREdescription
- a textual description of the optionaliases2
- zero or more aliases (e.g. "-f", "--file")
-
Option
Simpler constructor that defaults to a non-mandatory option. You can make it mandatory later by callingsetMandatory()
.- Parameters:
description
- a textual description of the optionparameterCount
- the number of parameters required: NONE, ONE, or ONE_OR_MOREaliases2
- zero or more aliases
-
-
Method Details
-
addAliases
Adds additional aliases to this option for user convenience.- Parameters:
aliasArray
- additional aliases (e.g., "-f", "--file")- Returns:
- this option (for method chaining)
-
addParameter
Adds a parameter to this option, validating it againstisValid(String)
.- Parameters:
parameterString
- the parameter string to add- Returns:
true
if the parameter was valid and added, otherwisefalse
-
describeFormat
Describes the kind or format of the expected parameter(s), e.g. "File", "Integer", etc.- Returns:
- a short string describing the parameter format
-
getAliasesAsString
Returns this option's aliases as a comma-separated string (e.g. "-f, --file"). -
getHelp
Returns a help message that includes the aliases, whether the option is mandatory, the format, and a brief description.- Returns:
- a descriptive help string
-
getValue
Returns the parsed value (usually constructed fromparameters
).- Returns:
- the parsed value as the generic type T.
- Throws:
RuntimeException
- if the parameters are insufficient or invalid in a subclass.
-
isMandatory
public boolean isMandatory()- Returns:
true
if this option is mandatory.
-
isPresent
public boolean isPresent()- Returns:
true
if this option was present on the command line.
-
setPresent
protected void setPresent(boolean present) Marks this option as present or not present.- Parameters:
present
-true
if it was found in the CLI arguments, elsefalse
.
-
matchesAlias
Checks if a given alias matches this option.- Parameters:
alias
- the alias to check- Returns:
true
if the alias is registered for this option
-
noMoreArguments
public boolean noMoreArguments()Called when no more arguments can be added to this option, giving it a chance to verify correct usage (e.g., check if required parameters are missing).- Returns:
true
if the usage so far is valid; otherwisefalse
.
-
setMandatory
Sets this option as mandatory.- Returns:
- this option, for method chaining
-
isValid
Checks if a single parameter string is valid for this option.- Parameters:
value
- the parameter string to test- Returns:
true
if valid;false
otherwise
-