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 enum
Types of command line options: REQUIRED required argument.static class
Handle different categories of options.static interface
Interface to process command line argumentsstatic interface
The interface for keys of command line options.static class
Simple key for command line options. -
Field Summary
FieldsModifier and TypeFieldDescription(package private) final CLOption.Category
The category of the command line option.(package private) String
The default argument for option (if applicable).private CLOption.CLODelegate
The delegate for parsing arguments, reporting settings and retrieving customized descriptions.(package private) String
the short description of the command line option.(package private) final int
Unique identifier of command line option (currently unused).(package private) boolean
The flag to indicate if keys were inherited from another option.(package private) boolean
true
if option was set on command line.(package private) HashMap
<String, CLOption.Key> The list of valid keys (if applicable).(package private) final String
The name of the command line option (required).(package private) String
The argument provided on the command line (if any).(package private) final CLOption.Argument
The type of the command line option with no, optional, or required argument.private static int
Counter 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 descriptiondescription
as well as the delegatedelegate
to 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 namename
of typetype
, which defaults todefaultArg
, and brief descriptiondescription
as well as the delegatedelegate
to 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 namename
of typetype
, which defaults todefaultArg
, in categorycategory
and brief descriptiondescription
as well as the delegatedelegate
to 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 namename
of typetype
, which defaults todefaultArg
, and catgeorycategory
as well as the delegatedelegate
to process the argument and retrieve the description.CLOption
(String name, String defaultArg, CLOption.Argument type, CLOption.CLODelegate delegate) Creates command line option with the namename
of typetype
, which defaults todefaultArg
, as well as the delegatedelegate
to 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 catgeorycategory
and brief descriptiondescription
as well as the delegatedelegate
to 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 descriptiondescription
as well as the delegatedelegate
to 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 categorycategory
and brief descriptiondescription
as well as the delegatedelegate
to 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 categorycategory
and thedelegate
to process the argument and retrieve the description.CLOption
(String name, CLOption.CLODelegate delegate) Creates command line option with the namename
and thedelegate
to process the argument and provide the description. -
Method Summary
Modifier and TypeMethodDescriptionaddKey
(CLOption.Key key) AddCLOption.Key
key
to option.void
addKeys
(CLOption.Key[] chain) Add allCLOption.Key
s in the arraychain
to this option.void
Clear all keys from the option.int
static int
Compare 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.void
inheritKeysFrom
(CLOption option) Inherit keys from another option.boolean
Check if no argument was set.boolean
isSet()
Check if option was set on command line (regardless of whether an argument was provided).boolean
isValidKey
(String aKey) Check if the key with nameaKey
is a valid key for this option.boolean
isValidKey
(CLOption.Key key) Check ifkey
is a valid key for this option.Returns the key that best matchesname
.Returns the key that best matchesname
with at leastmin
characters matching.boolean
parse()
Parses the option and its argument, if applicable, through the delegate.boolean
Parses the default argument for this option.Remove key with nameaKey
from the option's key collection.removeKey
(CLOption.Key key) Remove key from the option's key collection.void
report
(PrintStream output) Print report for this option tooutput
through delegate.void
reset()
Reset option.void
Set the argument for the command line option.void
setDefault
(String defaultArg) Set the default argument.void
setDescription
(String descr) Set short description of option.static String
Strips the key from the argument.static String
stripKey
(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. Iftrue
the keys will not be printed as part of the description. -
isSet
boolean isSettrue
if 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 namename
and thedelegate
to process the argument and provide the description.Note:
- on the command line option names need to be preceded by
--
, e.g.--help
. delegate
must 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 categorycategory
and thedelegate
to process the argument and retrieve the description.Note:
- on the command line option names need to be preceded by
--
, e.g.--help
. delegate
must 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 descriptiondescription
as well as the delegatedelegate
to 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 categorycategory
and brief descriptiondescription
as well as the delegatedelegate
to 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 descriptiondescription
as well as the delegatedelegate
to 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 catgeorycategory
and brief descriptiondescription
as well as the delegatedelegate
to 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 namename
of typetype
, which defaults todefaultArg
, as well as the delegatedelegate
to process the argument and retrieve the description.Note:
- on the command line option names need to be preceded by
--
, e.g.--help
. delegate
must 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 namename
of typetype
, which defaults todefaultArg
, and catgeorycategory
as well as the delegatedelegate
to process the argument and retrieve the description.Note:
- on the command line option names need to be preceded by
--
, e.g.--help
. delegate
must 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 namename
of typetype
, which defaults todefaultArg
, and brief descriptiondescription
as well as the delegatedelegate
to 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 namename
of typetype
, which defaults todefaultArg
, in categorycategory
and brief descriptiondescription
as well as the delegatedelegate
to 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 ofdefaultArg
fails, 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:
true
on successful parsing of argument
-
parseDefault
public boolean parseDefault()Parses the default argument for this option. Typically called ifparse()
failed.- Returns:
true
on successful parsing of default argument
-
addKeys
Add allCLOption.Key
s in the arraychain
to 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.Key
s 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
, ornull
if there was no mapping forkey
. (Anull
return can also indicate that the map previously associatednull
withkey
.)
-
addKey
AddCLOption.Key
key
to option.- Parameters:
key
- theCLOption.Key
to be added- Returns:
- the previous value associated with
key
, ornull
if there was no mapping forkey
. (Anull
return can also indicate that the map previously associatednull
withkey
.)
-
getKey
Get the key with nameaKey
. Returnsnull
if the option has no keys or no key with the nameaKey
.- Parameters:
aKey
- the name of the key- Returns:
- the key with name
aKey
ornull
if no such key exists
-
match
Returns the key that best matchesname
. If several keys are equally good matches the first match is returned. Ifname
perfectly 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
Key
ornull
if no match was found
-
match
Returns the key that best matchesname
with at leastmin
characters matching. If several keys are equally good matches the first match is returned. Ifname
perfectly 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
Key
ornull
if no match was found
-
removeKey
Remove key from the option's key collection. Returnsnull
if the option has no keys or thekey
is not part of the collection.- Parameters:
key
- the key to remove- Returns:
- the key that was removed or
null
if no such key exists
-
removeKey
Remove key with nameaKey
from the option's key collection.- Parameters:
aKey
- the name of the key to remove- Returns:
- the key that was removed or
null
if no such key exists
-
clearKeys
public void clearKeys()Clear all keys from the option. -
isValidKey
Check ifkey
is a valid key for this option.- Parameters:
key
- the key to check- Returns:
true
ifkey
is a valid key
-
isValidKey
Check if the key with nameaKey
is a valid key for this option. This test is very lenient and passes ifaKey
and 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:
true
if the nameaKey
is 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
-
report
Print report for this option tooutput
through delegate. If no delegate is available oroutput == null
do nothing.- Parameters:
output
- thePrintStream
for the output
-
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:
true
if no argument set.
-
isSet
public boolean isSet()Check if option was set on command line (regardless of whether an argument was provided).- Returns:
true
if 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:
compareTo
in interfaceComparable<CLOption>
-