|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.cleancode.data.InputOptions
public class InputOptions
Manages a collection of data inputs and configuration properties.
// use command-line and configuration file data settings = new InputOptions(cmdLineData, configData); // use command-line and configuration file name settings = new InputOptions(cmdLineData, "settings.conf"); // casual use--pass the command line arguments directly settings = new InputOptions(argv) // also check for required data and legal config properties settings = new InputOptions( cmdLineData, "settings.conf", requiredFields, paramMap); // retrieve property; use supplied value as default if not defined String prompt = settings.getProperty("PROMPT", "% "); // retrieve property; no default String prompt = settings.getProperty("PROMPT"); // retrieve data integer index = settings.getData("indexValue"); // other methods if (settings.isProperty(itemName)) { ... } settings.addProperty("dir", "/foo"); settings.addData("dir", "/bar"); // property and data are distinct! Set configKeys = settings.getKeySet(); Set dataKeys = settings.getDataKeySet();
Diagnostic
module for example uses properties to,
among other things, specify a diagnostic mask, to specify output channels,
and to specify diagnostic levels for different events.
Typically these settings do not change very often and are stored
in a configuration file read by your program during startup.
So you pass the name of this configuration file to the
InputOptions
constructor to process the settings it contains.
But sometimes you may want to vary certain properties under
different conditions.
Therefore InputOptions also supports properties from the command line.
These properties may be ones already in the configuration file,
or they may be new ones.
The configuration file is processed before the command line,
so you might think of it as establishing defaults
for the properties it contains.
Then any command-line configuration properties of the same name
override those defaults (if allowed), so you can modify properties
with each invocation if desired.
Note that you may use either command-line properties
or configuration file properties, or both, at your discretion.
InputOptions
class allows you to mix both properties
and input data on the command line.
From a web page, then, you may include form data that is entered by the
user, and properties that you have stored on the form as
hidden parameters perhaps. These properties are distinguished from
the user data by naming conventions, described in the next section.
Diagnostic
module is well-suited for this.
Included in your configuration file you would typically specify
no diagnostic output during the normal execution of your program.
But then you run into some unexplained behavior.
Just from the command line you may vary the diagnostic settings
to display different information that may help you identify the problem.
This will even work through a web browser, since the Diagnostic module
can attach diagnostic output directly onto the target web page.
MODE_PROPERTY
.)
In the configuration file, you must then have qualified names
for each of these properties, as in A:weekday=5 and A:weekend=24, etc.
Getopt::Long
API, "Historically,
they are preceded by a single dash -, and consist of a single letter."
This single-hyphen style is not supported by InputOptions.
Again from Getopt::Long, "Due to the very cryptic nature of these options,
another style was developed that used long names.
So instead of a cryptic -l one could use the more descriptive --long."
This long style is supported by InputOptions, as indicated above.
--debug
.
This is equivalent to this explicit form with an argument:
--debug=true
[Java]
or --debug=1
[Perl]. (Perl users will realize
that any non-false value will do here.)
You may also negate the boolean style, as in --noDebug
.
This is equivalent to --debug=false
[Java]
or --debug=0
[Perl].
This negation technique may also be applied to the other (non-POSIX)
InputOptions property format, mentioned earlier, using no lowercase letters.
So QUIET=false
[Java] or QUIET=0
[Perl]
can also be written as noQUIET
.
Pay particular attention to the capitalization change
from --debug=false to --noDebug and the lack of a capitalization change
from DEBUG=false to noDEBUG.
inline.conf
,
then you could mix properties on the command line as inprogram_name --x1=4 @inline.conf --x4=0.5
program_name --x1=4 --x1=2 --x2=4.3 --x3=true --x4=0.5
x1
property is then given twice; the last
one will be used, so the value will be 2, not 4.
CONFIG_PWD_SWITCH_PROPERTY
with a non-empty value to permit command-line
parameters to be used; otherwise any command-line parameters
will just be silently ignored. So if you wish to diagnose a problem by
varying certain parameters from a web browser, turn the switch on in your
configuration file, do your testing, then turn it back off.
CONFIG_PASSWD_PROPERTY
.
Then command-line parameters will only be used if the
command-line property CMDLINE_PASSWD_PROPERTY
is supplied with the same value.
If you have required a password with this mechanism and the command-line
property does not match the config file CONFIG_PASSWD_PROPERTY
,
a warning message will be emitted, indicating the password did not match,
and again any command-line parameters will be ignored.
The warning is emitted via the Diagnostic.warnPrint
method,
so it will be output to the web page or to the error log, depending on
how you have configured the Diagnostic
module.
CONFIG_PWD_SWITCH_PROPERTY
nor the CONFIG_PASSWD_PROPERTY
are needed.
You may just grab the command-line parameters and start using them
without hindrance from the more advanced features.
Diagnostic
class.
Each library class which uses InputOptions
properties will have
in its documentation a table which enumerates
each property, its type, its default value, and what it is used for.
new
method during your program initialization,
specifying your configuration file and your command-line data.
The configuration data may be either an array reference
or a configuration file name.
Either way, it ultimately points to a list of properties as described next.
The command-line data may be either an array reference
providing similarly structured data, or a single string for convenience.
getProperty
to retrieve a property by name, or getData
to retrieve data.
[Java only] This method usually returns a String,
so there are convenience methods
getIntProperty
and getBooleanProperty
as well.
(getProperty
could also return an integer or a boolean
if you use the appropriate two-argument version).
name=value //comments
COMMENT_MARK
) in a property value, you must disable comments
with the COMMENT_OFF
directive on a line by itself.
Use the corresponding COMMENT_ON
to re-enable comments
afterward.
COMMENT_MARK
;
only uncommented lines will be processed.name1=value1 name2=value2 ...
main
to new
.
You may also specify a set of command-line inputs from a file
using command-line expansion (@filename).
You may freely intermix command-line parameters with command-line
expansion.
So, the command-line actually consists of any combination
of name-i=value-i
and @filename-i
. A command-line expansion file
is not the same as a configuration file, however.
The former is subject to access control constraints described elsewhere,
does not support the .COMMENT/.NOCOMMENT directives, and does not
support partial-line comments. (You may use full line comments.)
InputOptions also provides a convenient way to pass a configuration file
to your program via the command-line with a predefined parameter:
--file=filename
.
This will only work if you do not explicitly pass
a configuration file to the InputOptions constructor.
So this is more for command-line programs rather than server applications.
InputOptions
object,
passing a reference to its configuration file,
plus a CGI object from which to extract the command-line parameters.
System.getProperty
method whereby
you can access a limited (platform-independent) set of environment properties
as well as command-line properties defined with the -D
option.
Perl, on the other hand, provides access to all environment properties
via the %ENV
hash.
C operates similarly, with the getenv
function.
(In fact, Java also has a getenv
method, but it is deprecated
as it is non-portable.)
Both Perl and Java libraries provide the getopts
function
for parsing command-line options with great flexibility.
Perl also provides assorted Config modules.
Nested Class Summary | |
---|---|
static class |
InputOptions.Test
A standalone test class to exercise the InputOptions class. |
Field Summary | |
---|---|
static String |
CMDLINE_PASSWD_PROPERTY
Property name to enter a password from the command line. |
static String |
COMMENT_MARK
Designator for comment-to-end-of-line. |
static String |
COMMENT_OFF
Directive to disable comment recognition. |
static String |
COMMENT_ON
Directive to enable comment recognition. |
static String |
CONFIG_FILE_PROPERTY
Property name to select a property subset. |
static String |
CONFIG_PASSWD_PROPERTY
Property name to specify required password for command-line overrides (if desired). |
static String |
CONFIG_PWD_SWITCH_PROPERTY
Property name to allow command-line overrides. |
static String |
MODE_PROPERTY
Property name to select a property subset. |
static String |
VERSION
Current version of this class. |
Constructor Summary | |
---|---|
InputOptions()
Initializes the InputOptions property list
with no arguments. |
|
InputOptions(String cmdLineData)
Initializes the InputOptions property list
with just one command-line setting. |
|
InputOptions(String[] cmdLineData)
Initializes the InputOptions property list
with just command-line settings. |
|
InputOptions(String[] cmdLineData,
String configFile)
Initializes the InputOptions property list from the specified
configuration file with any command-line overrides. |
|
InputOptions(String[] cmdLineData,
String[] configData)
Initializes the InputOptions property list from the specified
configuration data with any command-line overrides. |
|
InputOptions(String[] cmdLineData,
String[] configData,
String[] requiredFields,
ParamMap paramMap)
Initializes the InputOptions property list from the specified
configuration data with any command-line overrides. |
Method Summary | |
---|---|
void |
addData(String key,
String val)
Adds a value for the specified data item. |
void |
addProperty(String key,
String val)
Adds a value for the specified configuration property. |
boolean |
configFileLoaded()
This indicates to other modules whether a config file is being used. |
boolean |
getBooleanProperty(String key)
Returns a boolean value for the specified property. |
String |
getData(String key)
Returns a string value for the specified datum. |
boolean |
getData(String key,
boolean defaultVal)
Returns an boolean value for the specified datum. |
int |
getData(String key,
int defaultVal)
Returns an integer value for the specified datum. |
String |
getData(String key,
String defaultVal)
Returns a string value for the specified datum. |
Set |
getDataKeySet()
Returns a set of data names. |
int |
getIntProperty(String key)
Returns an integer value for the specified property. |
Set |
getKeySet()
Returns a set of configuration property names. |
String |
getProperty(String key)
Returns a string value for the specified property. |
boolean |
getProperty(String key,
boolean defaultVal)
Returns a boolean value for the specified property. |
int |
getProperty(String key,
int defaultVal)
Returns an integer value for the specified property. |
String |
getProperty(String key,
String defaultVal)
Returns a string value for the specified property. |
static boolean |
isProperty(String name)
Determines if a name qualifies as an InputOptions property. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final String COMMENT_MARK
public static final String CONFIG_PWD_SWITCH_PROPERTY
public static final String CONFIG_PASSWD_PROPERTY
public static final String CMDLINE_PASSWD_PROPERTY
public static final String MODE_PROPERTY
public static final String CONFIG_FILE_PROPERTY
public static final String COMMENT_ON
public static final String COMMENT_OFF
public static final String VERSION
Constructor Detail |
---|
public InputOptions(String[] cmdLineData, String[] configData, String[] requiredFields, ParamMap paramMap)
InputOptions
property list from the specified
configuration data with any command-line overrides.
cmdLineData
- a list of command-line overridesconfigData
- a list of configuration settingsrequiredFields
- a list of mandatory fields which, if absent,
produce an errorparamMap
- parameter map for the calling classpublic InputOptions(String[] cmdLineData, String[] configData)
InputOptions
property list from the specified
configuration data with any command-line overrides.
cmdLineData
- a list of command-line overridesconfigData
- a list of configuration settingspublic InputOptions(String[] cmdLineData, String configFile) throws IOException
InputOptions
property list from the specified
configuration file with any command-line overrides.
cmdLineData
- a list of command-line overridesconfigFile
- a configuration file name containing
a list of configuration settings
IOException
- if any problems reading the file.public InputOptions(String[] cmdLineData)
InputOptions
property list
with just command-line settings.
cmdLineData
- a list of command-line overridespublic InputOptions(String cmdLineData)
InputOptions
property list
with just one command-line setting.
cmdLineData
- a single command-line overridepublic InputOptions()
InputOptions
property list
with no arguments.
Method Detail |
---|
public boolean configFileLoaded()
public static boolean isProperty(String name)
InputOptions
property.
Input items may be either configuration properties
(retrievable with the getProperty method)
or input data (retrievable with the getData method).
Names must conform to a specific pattern to be matched as a property,
either an all-capital word, or a word beginning with two hyphens.
In the latter case, one must pass in a string containing the
two hyphens; otherwise, it could not be distinguished from a data item.
Note that for getProperty, however, the two hyphens must not be included.
name
- name to check
public String getProperty(String key, String defaultVal)
key
- name of property to retrievedefaultVal
- default value to return if no property
public int getProperty(String key, int defaultVal)
key
- name of property to retrievedefaultVal
- default value to return if no property
getProperty
public boolean getProperty(String key, boolean defaultVal)
key
- name of property to retrievedefaultVal
- default value to return if no property
getProperty
public String getProperty(String key)
null
.
key
- name of property to retrieve
public String getData(String key, String defaultVal)
key
- name of datum to retrievedefaultVal
- default value to return if no datum
public int getData(String key, int defaultVal)
key
- name of datum to retrievedefaultVal
- default value to return if no datum
public boolean getData(String key, boolean defaultVal)
key
- name of datum to retrievedefaultVal
- default value to return if no datum
public String getData(String key)
null
.
key
- name of datum to retrieve
public int getIntProperty(String key)
getProperty
method
can determine whether to return an
integer or a string by the type of the default value.
The single-argument version cannot,
so this convenience method is provided.
key
- name of property to retrieve
public boolean getBooleanProperty(String key)
getProperty
method
can determine whether to return an
integer or a string by the type of the default value.
The single-argument version cannot,
so this convenience method is provided.
key
- name of property to retrieve
public void addProperty(String key, String val)
key
- name of propertyval
- value to setpublic void addData(String key, String val)
key
- name of datumval
- value to setpublic Set getKeySet()
public Set getDataKeySet()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
CleanCode Java Libraries | Copyright © 2001-2012 Michael Sorens - Revised 2012.12.10 |