Class CLOption
Object
CLOption
- All Implemented Interfaces:
Comparable<CLOption>
Command line option and argument.
- Author:
- Christoph Hauert
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumTypes of command line options: REQUIRED required argument.static interfaceThe interface for keys of command line options.static classSimple key for command line options. -
Field Summary
FieldsModifier and TypeFieldDescription(package private) final CLOCategoryThe category of the command line option.(package private) StringThe default argument for option (if applicable).private CLODelegateThe delegate for parsing arguments, reporting settings and retrieving customized descriptions.(package private) Stringthe short description of the command line option.(package private) booleanThe flag to indicate if keys were inherited from another option.(package private) HashMap<String, CLOption.Key> The list of valid keys (if applicable).(package private) final StringThe name of the command line option (required).(package private) StringThe argument provided on the command line (if any).(package private) final CLOption.ArgumentThe type of the command line option with no, optional, or required argument. -
Constructor Summary
ConstructorsConstructorDescriptionCLOption(String name, String defaultArg, String description, CLODelegate delegate) Creates command line option with the namename(with required argument), which defaults todefaultArg, and brief descriptiondescriptionas well as the delegatedelegateto process the argument and optionally retrieve the description.CLOption(String name, String defaultArg, CLOCategory category, String description, CLODelegate delegate) Creates command line option with the namename(with required argument), which defaults todefaultArg, of catgeorycategoryand brief descriptiondescriptionas well as the delegatedelegateto process the argument and optionally retrieve the description.CLOption(String name, String description, CLODelegate delegate) Creates command line option with the namename(no arguments) and short descriptiondescriptionas well as the delegatedelegateto process the argument and optionally retrieve the description.CLOption(String name, String defaultArg, CLOption.Argument type, String description, CLODelegate delegate) Creates command line option with the namenameof typetype, which defaults todefaultArg, and brief descriptiondescriptionas well as the delegatedelegateto process the argument and optionally retrieve the description.CLOption(String name, String defaultArg, CLOption.Argument type, CLOCategory category, String description, CLODelegate delegate) Creates command line option with the namenameof typetype, which defaults todefaultArg, in categorycategoryand brief descriptiondescriptionas well as the delegatedelegateto process the argument and optionally retrieve the description.CLOption(String name, String defaultArg, CLOption.Argument type, CLOCategory category, CLODelegate delegate) Creates command line option with the namenameof typetype, which defaults todefaultArg, and catgeorycategoryas well as the delegatedelegateto process the argument and retrieve the description.CLOption(String name, String defaultArg, CLOption.Argument type, CLODelegate delegate) Creates command line option with the namenameof typetype, which defaults todefaultArg, as well as the delegatedelegateto process the argument and retrieve the description.CLOption(String name, CLOCategory category, String description, CLODelegate delegate) Creates command line option with the namename(no arguments) with categorycategoryand brief descriptiondescriptionas well as the delegatedelegateto process the argument and optionally retrieve the description.CLOption(String name, CLOCategory category, CLODelegate delegate) Creates command line option with the namename(no arguments) with categorycategoryand thedelegateto process the argument and retrieve the description.CLOption(String name, CLODelegate delegate) Creates command line option with the namenameand thedelegateto process the argument and provide the description. -
Method Summary
Modifier and TypeMethodDescriptionaddKey(CLOption.Key key) AddCLOption.Keykeyto option.voidaddKeys(CLOption.Key[] chain) Add allCLOption.Keys in the arraychainto this option.private StringConstruct the user-facing representation of the argument when keys are defined; mirrors previous loop logic but extracted to reduce complexity.private StringBuilderBuild the base description string, using either the stored description or asking the delegate and appending key descriptions if applicable.private StringBuilderbuildCurrent(StringBuilder sb, String currentArg) Append the current and default argument description to the provided builder.private CLOption.KeybuildKey(StringBuilder sb, CLOption.Key key, String[] args, int pos) Append the resolved key value and remaining arguments to the builder.voidClear all keys from the option.intstatic intCompare two strings and return the index of the first character that differs.booleangetArg()Get the option argument.Return the default argument for command line option.Retrieve short description of option and include the default as well as the current arguments.Get the description of all keys of this option.getDescriptionKey(String aKey) Get the description of the key with nameaKey.Get the key with nameaKey.getKeys()Gets all keys of this option.getName()Get the name of the option.getType()Get the type of the option.inthashCode()voidinheritKeysFrom(CLOption option) Inherit keys from another option.booleanisSet()Check if option was set on command line (regardless of whether an argument was provided).booleanisValidKey(String aKey) Check if the key with nameaKeyis a valid key for this option.booleanisValidKey(CLOption.Key key) Check ifkeyis a valid key for this option.Returns the key that best matchesname.Returns the key that best matchesnamewith at leastmincharacters matching.booleanparse()Parses the option and its argument, if applicable, through the delegate.private booleanparse(boolean isOptional) Delegate parsing helper that handles optional vs required arguments.Remove key with nameaKeyfrom the option's key collection.removeKey(CLOption.Key key) Remove key from the option's key collection.voidreset()Reset option.voidSet the argument for the command line option.voidsetDefault(String defaultArg) Set the default argument.voidsetDescription(String descr) Set short description of option.static StringStrips the key from the argument.static StringstripKey(CLOption.Key key, String arg) Strips the name of the key from the argument.
-
Field Details
-
name
The name of the command line option (required). -
type
The type of the command line option with no, optional, or required argument. -
category
The category of the command line option. Used to structure the help screen. -
description
String descriptionthe short description of the command line option. May include newline's'\n'for basic formatting.Note: the description string may contain any UTF-8 characters as well as HTML character entities. If necessary they will be escaped and converted to UTF-8 for display in XML documents.
-
optionArg
String optionArgThe argument provided on the command line (if any). -
defaultArg
String defaultArgThe default argument for option (if applicable). -
keys
HashMap<String, CLOption.Key> keysThe list of valid keys (if applicable). -
inheritedKeys
boolean inheritedKeysThe flag to indicate if keys were inherited from another option. Iftruethe keys will not be printed as part of the description. -
delegate
The delegate for parsing arguments, reporting settings and retrieving customized descriptions.
-
-
Constructor Details
-
CLOption
Creates command line option with the namenameand thedelegateto process the argument and provide the description.Note:
- on the command line option names need to be preceded by
--, e.g.--help. delegatemust implementCLODelegate.getDescription()to provide option description.
- Parameters:
name- the name of the command line optiondelegate- delegate for processing command line argument
- on the command line option names need to be preceded by
-
CLOption
Creates command line option with the namename(no arguments) with categorycategoryand thedelegateto process the argument and retrieve the description.Note:
- on the command line option names need to be preceded by
--, e.g.--help. delegatemust implementCLODelegate.getDescription()to provide option description.
- Parameters:
name- the name of the command line optioncategory- the category of optiondelegate- delegate for processing command line argument
- on the command line option names need to be preceded by
-
CLOption
Creates command line option with the namename(no arguments) and short descriptiondescriptionas well as the delegatedelegateto process the argument and optionally retrieve the description.Notes:
- on the command line option names need to be preceded by
--, e.g.--help. - the description string may contain any UTF-8 characters as well as HTML character entities. If necessary they will be escaped and converted to UTF-8 for display in XML documents.
- Parameters:
name- the name of the command line optiondescription- short description of command line optiondelegate- delegate for processing command line argument
- on the command line option names need to be preceded by
-
CLOption
Creates command line option with the namename(no arguments) with categorycategoryand brief descriptiondescriptionas well as the delegatedelegateto process the argument and optionally retrieve the description.Notes:
- on the command line option names need to be preceded by
--, e.g.--help. - the description string may contain any UTF-8 characters as well as HTML character entities. If necessary they will be escaped and converted to UTF-8 for display in XML documents.
- Parameters:
name- the name of the command line optioncategory- the category of optiondescription- short description of command line optiondelegate- delegate for processing command line argument
- on the command line option names need to be preceded by
-
CLOption
Creates command line option with the namename(with required argument), which defaults todefaultArg, and brief descriptiondescriptionas well as the delegatedelegateto process the argument and optionally retrieve the description.Notes:
- on the command line option names need to be preceded by
--, e.g.--help. - the description string may contain any UTF-8 characters as well as HTML character entities. If necessary they will be escaped and converted to UTF-8 for display in XML documents.
- Parameters:
name- the name of the command line optiondefaultArg- the default argument if option is not specified on command linedescription- short description of command line optiondelegate- delegate for processing command line argument
- on the command line option names need to be preceded by
-
CLOption
public CLOption(String name, String defaultArg, CLOCategory category, String description, CLODelegate delegate) Creates command line option with the namename(with required argument), which defaults todefaultArg, of catgeorycategoryand brief descriptiondescriptionas well as the delegatedelegateto process the argument and optionally retrieve the description.Notes:
- on the command line option names need to be preceded by
--, e.g.--help. - the description string may contain any UTF-8 characters as well as HTML character entities. If necessary they will be escaped and converted to UTF-8 for display in XML documents.
- Parameters:
name- the name of the command line optiondefaultArg- the default argument if option is not specified on command linecategory- the category of optiondescription- short description of command line optiondelegate- delegate for processing command line argument
- on the command line option names need to be preceded by
-
CLOption
Creates command line option with the namenameof typetype, which defaults todefaultArg, as well as the delegatedelegateto process the argument and retrieve the description.Notes:
- on the command line option names need to be preceded by
--, e.g.--help. delegatemust implementCLODelegate.getDescription()to provide option description.
- Parameters:
name- the name of the command line optiondefaultArg- the default argument if option is not specified on command linetype- of command line option (whether argument required)delegate- delegate for processing command line argument
- on the command line option names need to be preceded by
-
CLOption
public CLOption(String name, String defaultArg, CLOption.Argument type, CLOCategory category, CLODelegate delegate) Creates command line option with the namenameof typetype, which defaults todefaultArg, and catgeorycategoryas well as the delegatedelegateto process the argument and retrieve the description.Note:
- on the command line option names need to be preceded by
--, e.g.--help. delegatemust implementCLODelegate.getDescription()to provide option description.
- Parameters:
name- the name of the command line optiondefaultArg- the default argument if option is not specified on command linetype- of command line option (whether argument required)category- the category of optiondelegate- delegate for processing command line argument
- on the command line option names need to be preceded by
-
CLOption
public CLOption(String name, String defaultArg, CLOption.Argument type, String description, CLODelegate delegate) Creates command line option with the namenameof typetype, which defaults todefaultArg, and brief descriptiondescriptionas well as the delegatedelegateto process the argument and optionally retrieve the description.Notes:
- on the command line option names need to be preceded by
--, e.g.--help. - the description string may contain any UTF-8 characters as well as HTML character entities. If necessary they will be escaped and converted to UTF-8 for display in XML documents.
- Parameters:
name- the name of the command line optiondefaultArg- default argument if option is not specified on command linetype- of command line option (whether argument required)description- short description of command line optiondelegate- delegate for processing command line argument
- on the command line option names need to be preceded by
-
CLOption
public CLOption(String name, String defaultArg, CLOption.Argument type, CLOCategory category, String description, CLODelegate delegate) Creates command line option with the namenameof typetype, which defaults todefaultArg, in categorycategoryand brief descriptiondescriptionas well as the delegatedelegateto process the argument and optionally retrieve the description.Notes:
- on the command line option names need to be preceded by
--, e.g.--help. - the description string may contain any UTF-8 characters as well as HTML character entities. If necessary they will be escaped and converted to UTF-8 for display in XML documents.
- Parameters:
name- name of command line optiondefaultArg- default argument if option is not specified on command linetype- of command line option (whether argument required)category- the category of command line optiondescription- short description of command line optiondelegate- delegate for processing command line argument
- on the command line option names need to be preceded by
-
-
Method Details
-
setDefault
Set the default argument. This argument is parsed by the delegate if option is not specified on command line or if the parsing of the provided argument failed. If the parsing ofdefaultArgfails, the option settings are undefined.- Parameters:
defaultArg- default argument for command line option
-
getDefault
Return the default argument for command line option.- Returns:
- default argument
-
setArg
Set the argument for the command line option.- Parameters:
arg- the argument
-
parse
public boolean parse()Parses the option and its argument, if applicable, through the delegate. If this option was not specified on command line, the default argument is passed to the delegate.- Returns:
trueon successful parsing of argument
-
parse
private boolean parse(boolean isOptional) Delegate parsing helper that handles optional vs required arguments.- Parameters:
isOptional-trueif the option argument is optional- Returns:
trueif parsing succeeds
-
addKeys
Add allCLOption.Keys in the arraychainto this option. Note, this ignores keys starting with '-', except if '-' is the key. If needed, those keys can still be added by callingaddKey(Key)or#addKey(String, String).- Parameters:
chain- the array ofCLOption.Keys to be added- See Also:
-
addKey
- Parameters:
key- the name of the keytitle- the description of the key- Returns:
- the previous value associated with
key, ornullif there was no mapping forkey. (Anullreturn can also indicate that the map previously associatednullwithkey.)
-
addKey
AddCLOption.Keykeyto option.- Parameters:
key- theCLOption.Keyto be added- Returns:
- the previous value associated with
key, ornullif there was no mapping forkey. (Anullreturn can also indicate that the map previously associatednullwithkey.)
-
getKey
Get the key with nameaKey. Returnsnullif the option has no keys or no key with the nameaKey.- Parameters:
aKey- the name of the key- Returns:
- the key with name
aKeyornullif no such key exists
-
match
Returns the key that best matchesname. If several keys are equally good matches the first match is returned. Ifnameperfectly matches one key, i.e.name.startsWith(key.getName())istrue, then a better match must match at least one more character ofname.- Parameters:
keyname- the name of the key to match- Returns:
- matching
Keyornullif no match was found
-
match
Returns the key that best matchesnamewith at leastmincharacters matching. If several keys are equally good matches the first match is returned. Ifnameperfectly matches one key, i.e.name.startsWith(key.getName())istrue, then a better match must match at least one more character ofname.- Parameters:
keyname- the name of the key to matchmin- minimum number of characters that must match- Returns:
- matching
Keyornullif no match was found
-
removeKey
Remove key from the option's key collection. Returnsnullif the option has no keys or thekeyis not part of the collection.- Parameters:
key- the key to remove- Returns:
- the key that was removed or
nullif no such key exists
-
removeKey
Remove key with nameaKeyfrom the option's key collection.- Parameters:
aKey- the name of the key to remove- Returns:
- the key that was removed or
nullif no such key exists
-
clearKeys
public void clearKeys()Clear all keys from the option. -
isValidKey
Check ifkeyis a valid key for this option.- Parameters:
key- the key to check- Returns:
trueifkeyis a valid key
-
isValidKey
Check if the key with nameaKeyis a valid key for this option. This test is very lenient and passes ifaKeyand one of the keys start at least with one identical character. This allows abbreviating keys as well as appending options.- Parameters:
aKey- the name of the key to check- Returns:
trueif the nameaKeyis valid- See Also:
-
differAt
-
stripKey
Strips the name of the key from the argument. If the key is not found, the argument is returned unchanged.- Parameters:
key- the key to striparg- the argument to strip the key from- Returns:
- the argument without the key
-
stripKey
-
getKeys
-
inheritKeysFrom
Inherit keys from another option. This is useful if options share the same keys.- Parameters:
option- the option to inherit keys from
-
getDescriptionKey
-
getDescriptionKey
Get the description of all keys of this option. Minimal formatting is applied with the name of the key and a brief description of the key separated by '\n'. No HTML or other formatting can be applied.- Returns:
- the description of all keys
-
reset
public void reset()Reset option. Clear argument, if applicable, and mark as notisSet. -
getName
-
getType
-
getArg
Get the option argument. If no argument was set, the default argument is returned.- Returns:
- the argument
-
isSet
public boolean isSet()Check if option was set on command line (regardless of whether an argument was provided).- Returns:
trueif option set
-
getDescription
Retrieve short description of option and include the default as well as the current arguments. If no description was provided at initialization, the delegate is queried for an up-to-date description.Note: the description string may contain any UTF-8 characters. If necessary they will be escaped for display in HTML or XML documents.
- Returns:
- description of option and arguments.
-
buildCurrent
Append the current and default argument description to the provided builder.- Parameters:
sb- builder to append tocurrentArg- textual representation of the current argument- Returns:
- the supplied builder for chaining
-
buildBaseDescription
Build the base description string, using either the stored description or asking the delegate and appending key descriptions if applicable.- Returns:
- builder containing the base description text
-
buildArgKeys
Construct the user-facing representation of the argument when keys are defined; mirrors previous loop logic but extracted to reduce complexity.- Returns:
- textual representation of the key-based arguments
-
buildKey
Append the resolved key value and remaining arguments to the builder.- Parameters:
sb- builder to append tokey- resolved keyargs- tokenized argument listpos- position of the key token withinargs- Returns:
- the key that was appended (for chaining)
-
setDescription
Set short description of option.- Parameters:
descr- description of option
-
compareTo
- Specified by:
compareToin interfaceComparable<CLOption>
-
equals
-
hashCode
-