public class Options extends TypedProperties
Properties
map type with the added
abilities to read application options from the command line and to
use bean patterns to set an object's properties via command-line the
stored mappings.
A typical use pattern for this class is to construct a new instance
in the main
method, then call setFromCmdLine(java.lang.String[])
with the
given args. Next, an instanceof the class being invoked is created, and
setInto(java.lang.Object)
is called with that instance as a parameter. With this
pattern, the user can configure any bean properties of the class, or even
properties of classes reachable from the class, through the command line.Modifier and Type | Field and Description |
---|---|
static Options |
EMPTY
Immutable empty instance.
|
defaults
Constructor and Description |
---|
Options()
Default constructor.
|
Options(Properties defaults)
Construct the options instance with the given set of defaults.
|
Modifier and Type | Method and Description |
---|---|
static Collection<String> |
findOptionsFor(Class<?> type)
Finds all the options that can be set on the provided class.
|
boolean |
getBooleanProperty(String key,
String key2,
boolean def)
Specialization of
getBooleanProperty(java.lang.String, java.lang.String, boolean) to allow
a value to appear under either of two keys; useful for short and
long versions of command-line flags. |
double |
getDoubleProperty(String key,
String key2,
double def)
Specialization of
TypedProperties.getDoubleProperty(java.lang.String) to allow
a value to appear under either of two keys; useful for short and
long versions of command-line flags. |
float |
getFloatProperty(String key,
String key2,
float def)
Specialization of
TypedProperties.getFloatProperty(java.lang.String) to allow
a value to appear under either of two keys; useful for short and
long versions of command-line flags. |
int |
getIntProperty(String key,
String key2,
int def)
Specialization of
TypedProperties.getIntProperty(java.lang.String) to allow
a value to appear under either of two keys; useful for short and
long versions of command-line flags. |
long |
getLongProperty(String key,
String key2,
long def)
Specialization of
TypedProperties.getLongProperty(java.lang.String) to allow
a value to appear under either of two keys; useful for short and
long versions of command-line flags. |
String |
getProperty(String key,
String key2,
String def)
Specialization of
Properties.getProperty(java.lang.String) to allow
a value to appear under either of two keys; useful for short and
long versions of command-line flags. |
boolean |
removeBooleanProperty(String key,
String key2,
boolean def)
Specialization of
TypedProperties.removeBooleanProperty(java.lang.String) to allow
a value to appear under either of two keys; useful for short and
long versions of command-line flags. |
double |
removeDoubleProperty(String key,
String key2,
double def)
Specialization of
TypedProperties.removeDoubleProperty(java.lang.String) to allow
a value to appear under either of two keys; useful for short and
long versions of command-line flags. |
float |
removeFloatProperty(String key,
String key2,
float def)
Specialization of
TypedProperties.removeFloatProperty(java.lang.String) to allow
a value to appear under either of two keys; useful for short and
long versions of command-line flags. |
int |
removeIntProperty(String key,
String key2,
int def)
Specialization of
TypedProperties.removeIntProperty(java.lang.String) to allow
a value to appear under either of two keys; useful for short and
long versions of command-line flags. |
long |
removeLongProperty(String key,
String key2,
long def)
Specialization of
TypedProperties.removeLongProperty(java.lang.String) to allow
a value to appear under either of two keys; useful for short and
long versions of command-line flags. |
String |
removeProperty(String key,
String key2,
String def)
Specialization of
Hashtable.remove(Object) to allow
a value to appear under either of two keys; useful for short and
long versions of command-line flags. |
String[] |
setFromCmdLine(String[] args)
Parses the given argument list into flag/value pairs, which are stored
as properties.
|
Options |
setInto(Object obj)
This method uses reflection to set all the properties in the given
object that are named by the keys in this map.
|
getBooleanProperty, getBooleanProperty, getDoubleProperty, getDoubleProperty, getFloatProperty, getFloatProperty, getIntProperty, getIntProperty, getLongProperty, getLongProperty, removeBooleanProperty, removeBooleanProperty, removeDoubleProperty, removeDoubleProperty, removeFloatProperty, removeFloatProperty, removeIntProperty, removeIntProperty, removeLongProperty, removeLongProperty, removeProperty, removeProperty, setProperty, setProperty, setProperty, setProperty, setProperty, setProperty
getProperty, getProperty, list, list, load, load, loadFromXML, propertyNames, save, store, store, storeToXML, storeToXML, stringPropertyNames
clear, clone, compute, computeIfAbsent, computeIfPresent, contains, containsKey, containsValue, elements, entrySet, equals, forEach, get, getOrDefault, hashCode, isEmpty, keys, keySet, merge, put, putAll, putIfAbsent, rehash, remove, remove, replace, replace, replaceAll, size, toString, values
public static Options EMPTY
public Options()
public Options(Properties defaults)
Properties(Properties)
public String[] setFromCmdLine(String[] args)
java Foo -flag1 value1 -flag2 value2 ... arg1 arg2 ...
args
- the command-line argumentspublic Options setInto(Object obj)
"age"->"12"
obj.setAge(12)
"range"->"1,20"
obj.setRange(1, 20)
"range"->"10"
obj.setRange(10, 10)
"brother.name"->"Bob"
obj.getBrother().setName("Bob")
Map
returned by this method.Options
of key-value pairs in this object
for which no setters could be found.RuntimeException
- on parse errorpublic static Collection<String> findOptionsFor(Class<?> type)
type
- The class for which available options should be listed.type
. The
names will have initial caps. They will be ordered alphabetically.public boolean getBooleanProperty(String key, String key2, boolean def)
getBooleanProperty(java.lang.String, java.lang.String, boolean)
to allow
a value to appear under either of two keys; useful for short and
long versions of command-line flags.public float getFloatProperty(String key, String key2, float def)
TypedProperties.getFloatProperty(java.lang.String)
to allow
a value to appear under either of two keys; useful for short and
long versions of command-line flags.public double getDoubleProperty(String key, String key2, double def)
TypedProperties.getDoubleProperty(java.lang.String)
to allow
a value to appear under either of two keys; useful for short and
long versions of command-line flags.public long getLongProperty(String key, String key2, long def)
TypedProperties.getLongProperty(java.lang.String)
to allow
a value to appear under either of two keys; useful for short and
long versions of command-line flags.public int getIntProperty(String key, String key2, int def)
TypedProperties.getIntProperty(java.lang.String)
to allow
a value to appear under either of two keys; useful for short and
long versions of command-line flags.public String getProperty(String key, String key2, String def)
Properties.getProperty(java.lang.String)
to allow
a value to appear under either of two keys; useful for short and
long versions of command-line flags.public boolean removeBooleanProperty(String key, String key2, boolean def)
TypedProperties.removeBooleanProperty(java.lang.String)
to allow
a value to appear under either of two keys; useful for short and
long versions of command-line flags.public float removeFloatProperty(String key, String key2, float def)
TypedProperties.removeFloatProperty(java.lang.String)
to allow
a value to appear under either of two keys; useful for short and
long versions of command-line flags.public double removeDoubleProperty(String key, String key2, double def)
TypedProperties.removeDoubleProperty(java.lang.String)
to allow
a value to appear under either of two keys; useful for short and
long versions of command-line flags.public long removeLongProperty(String key, String key2, long def)
TypedProperties.removeLongProperty(java.lang.String)
to allow
a value to appear under either of two keys; useful for short and
long versions of command-line flags.public int removeIntProperty(String key, String key2, int def)
TypedProperties.removeIntProperty(java.lang.String)
to allow
a value to appear under either of two keys; useful for short and
long versions of command-line flags.public String removeProperty(String key, String key2, String def)
Hashtable.remove(Object)
to allow
a value to appear under either of two keys; useful for short and
long versions of command-line flags.Copyright © 2006–2022 Apache Software Foundation. All rights reserved.