Package org.evoludo.util
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 classHandle different categories of options.static interfaceInterface to process command line argumentsstatic interfaceThe interface for keys of command line options.static classSimple key for command line options. -
Field Summary
FieldsModifier and TypeFieldDescription(package private) final CLOption.CategoryThe category of the command line option.(package private) StringThe default argument for option (if applicable).private CLOption.CLODelegateThe delegate for parsing arguments, reporting settings and retrieving customized descriptions.(package private) Stringthe short description of the command line option.(package private) final intUnique identifier of command line option (currently unused).(package private) booleanThe flag to indicate if keys were inherited from another option.(package private) booleantrueif option was set on command line.(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.private static intCounter to assign every option a unique identifier. -
Constructor Summary
ConstructorsConstructorDescriptionCLOption(String name, String defaultArg, String description, CLOption.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, CLOption.Argument type, String description, CLOption.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, CLOption.Category category, String description, CLOption.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, CLOption.Category category, CLOption.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, CLOption.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, String defaultArg, CLOption.Category category, String description, CLOption.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, CLOption.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, CLOption.Category category, String description, CLOption.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, CLOption.Category category, CLOption.CLODelegate delegate) Creates command line option with the namename(no arguments) with categorycategoryand thedelegateto process the argument and retrieve the description.CLOption(String name, CLOption.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.voidClear all keys from the option.intstatic intCompare two strings and return the index of the first character that differs.getArg()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.voidinheritKeysFrom(CLOption option) Inherit keys from another option.booleanCheck if no argument was set.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.booleanParses the default argument for this option.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
-
uniqueID
private static int uniqueIDCounter to assign every option a unique identifier. -
ID
final int IDUnique identifier of command line option (currently unused). -
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 but no HTML or other formatting. -
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. -
isSet
boolean isSettrueif option was set on command line. -
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 implementCLOption.CLODelegate.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 implementCLOption.CLODelegate.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.Note: on the command line option names need to be preceded by
--, e.g.--help.- Parameters:
name- the name of the command line optiondescription- short description of command line optiondelegate- delegate for processing command line argument
-
CLOption
public CLOption(String name, CLOption.Category category, String description, CLOption.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.Note: on the command line option names need to be preceded by
--, e.g.--help.- 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
-
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.Note: on the command line option names need to be preceded by
--, e.g.--help.- 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
-
CLOption
public CLOption(String name, String defaultArg, CLOption.Category category, String description, CLOption.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.Note: on the command line option names need to be preceded by
--, e.g.--help.- 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
-
CLOption
public CLOption(String name, String defaultArg, CLOption.Argument type, CLOption.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.Note:
- on the command line option names need to be preceded by
--, e.g.--help. delegatemust implementCLOption.CLODelegate.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, CLOption.Category category, CLOption.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 implementCLOption.CLODelegate.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, CLOption.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.Note: on the command line option names need to be preceded by
--, e.g.--help.- 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
-
CLOption
public CLOption(String name, String defaultArg, CLOption.Argument type, CLOption.Category category, String description, CLOption.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.Note: on the command line options need to be preceded by
--, e.g.--help.- 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
-
-
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
-
parseDefault
public boolean parseDefault()Parses the default argument for this option. Typically called ifparse()failed.- Returns:
trueon successful parsing of default argument
-
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
Compare two strings and return the index of the first character that differs.- Parameters:
a- the first stringb- the second string- Returns:
- the index of the first differing character
-
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
Strips the key from the argument. If the key is not found, the argument is returned unchanged.- Parameters:
key- the name of the key to striparg- the argument to strip the key from- Returns:
- the argument without the key
-
getKeys
Gets all keys of this option.- Returns:
- the key collection
-
inheritKeysFrom
Inherit keys from another option. This is useful if options share the same keys.- Parameters:
option- the option to inherit keys from
-
getDescriptionKey
Get the description of the key with nameaKey.- Parameters:
aKey- the name of the key- Returns:
- the description of the key
-
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
Get the name of the option.- Returns:
- the name
-
getType
Get the type of the option.- Returns:
- the type
-
getArg
Get the option argument. If no argument was set, the default argument is returned.- Returns:
- the argument
-
isDefault
public boolean isDefault()Check if no argument was set.- Returns:
trueif no argument set.
-
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.- Returns:
- description of option and arguments.
-
setDescription
Set short description of option.- Parameters:
descr- description of option
-
compareTo
- Specified by:
compareToin interfaceComparable<CLOption>
-