|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjcmdline.AbstractParameter
jcmdline.FileParam
public class FileParam
Encapsulate a command line parameter whose value will be the name of a file or directory. Attributes, such as whether the value is to be a file or directory, whether it must be readable, etc, may be specified and will be validated.
Usage:
public static void main(String[] args) {
FileParam filesArg =
new FileParam("file",
"a file to be processed - defaults to stdin",
FileParam.IS_FILE & FileParam.IS_READABLE,
FileParam.OPTIONAL,
FileParam.MULTI_VALUED);
CmdLineHandler cl =
new VersionCmdLineHandler("V 1.0",
"echoFiles",
"echos specified files to stdout",
new Parameter[] {},
new Parameter[] { filesArg } ));
cl.parse(args);
if (filesArg.isSet()) {
for (Iterator itr = filesArg.getFiles().iterator();
itr.hasNext();) {
processStream(new FileInputStream((File)itr.next()));
}
} else {
processStream(System.in);
}
}
CmdLineParser| Field Summary | |
|---|---|
static java.lang.String |
DEFAULT_DIR_OPTION_LABEL
the default label that will represent option values for this Parameter where IS_DIR is set. |
static java.lang.String |
DEFAULT_FILE_OPTION_LABEL
the default label that will represent option values for this Parameter where IS_DIR is not set. |
static int |
DOESNT_EXIST
indicates that a file or directory specified as a value for this FileParam must not exist |
static int |
EXISTS
indicates that a file or directory specified as a value for this FileParam must exist |
static int |
IS_DIR
indicates that a value specified for this FileParam must name an existing directory |
static int |
IS_FILE
indicates that a value specified for this FileParam must name an existing file |
static int |
IS_READABLE
indicates that a value specified for this FileParam must name an existing file or directory for which the caller has read access |
static int |
IS_WRITEABLE
indicates that a value specified for this FileParam must name an existing file or directory for which the caller has write access |
static int |
NO_ATTRIBUTES
indicates that no file/dir attributes are required or will be checked |
| Fields inherited from class jcmdline.AbstractParameter |
|---|
acceptableValues, desc, hidden, ignoreRequired, multiValued, optional, optionLabel, set, tag, values |
| Fields inherited from interface jcmdline.Parameter |
|---|
HIDDEN, MULTI_VALUED, OPTIONAL, PUBLIC, REQUIRED, SINGLE_VALUED |
| Constructor Summary | |
|---|---|
FileParam(java.lang.String tag,
java.lang.String desc)
constructor - creates single-valued, optional, public parameter which accepts any valid file or directory name as its value |
|
FileParam(java.lang.String tag,
java.lang.String desc,
boolean optional)
constructor - creates single-valued, public parameter which accepts any valid file or directory name as its value and is optional or required, as specified. |
|
FileParam(java.lang.String tag,
java.lang.String desc,
int attributes)
constructor - creates a single-valued, optional, public, parameter accepts a file or directory name with the specified attributes. |
|
FileParam(java.lang.String tag,
java.lang.String desc,
int attributes,
boolean optional)
constructor - creates a single-valued, public, parameter that accepts a file or directory name with the specified attributes, and which is required or optional, as specified. |
|
FileParam(java.lang.String tag,
java.lang.String desc,
int attributes,
boolean optional,
boolean multiValued)
constructor - creates a public parameter that accepts a file or directory name with the specified attributes, and which is required or optional and/or multi-valued, as specified. |
|
FileParam(java.lang.String tag,
java.lang.String desc,
int attributes,
boolean optional,
boolean multiValued,
boolean hidden)
constructor - creates a parameter that accepts a file or directory name with the specified attributes, and which is required or optional and/or multi-valued or hidden, as specified. |
|
| Method Summary | |
|---|---|
boolean |
attrSpecified(int attr)
Indicates whether an attribute has been specified for this FileParam. |
int |
getAttributes()
gets the value of attributes |
java.io.File |
getFile()
Gets the value of the FileParam as a File object. |
java.util.Collection |
getFiles()
Gets the values of the FileParam as a Collection of File objects. |
void |
setAttributes(int attributes)
Sets the value of attributes. |
void |
validateValue(java.lang.String val)
Validates a prospective value for the FileParam - called by add/setValue(s)(). |
| Methods inherited from class jcmdline.AbstractParameter |
|---|
addValue, getAcceptableValues, getDesc, getIgnoreRequired, getOptionLabel, getTag, getValue, getValues, isHidden, isMultiValued, isOptional, isSet, setAcceptableValues, setAcceptableValues, setDesc, setHidden, setIgnoreRequired, setMultiValued, setOptional, setOptionLabel, setTag, setValue, setValues, setValues |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final int NO_ATTRIBUTES
setAttributes(),
Constant Field Valuespublic static final int EXISTS
setAttributes(),
Constant Field Valuespublic static final int DOESNT_EXIST
setAttributes(),
Constant Field Valuespublic static final int IS_FILE
setAttributes(),
Constant Field Valuespublic static final int IS_DIR
setAttributes(),
Constant Field Valuespublic static final int IS_READABLE
setAttributes(),
Constant Field Valuespublic static final int IS_WRITEABLE
setAttributes(),
Constant Field Valuespublic static final java.lang.String DEFAULT_FILE_OPTION_LABEL
IS_DIR is not set.
The following demonstrates a possible usage for a FileParam option,
where the option label is '<file>':
out <file> the output file
setOptionLabel(),
"FileParam.defaultFileOptionLabel in 'strings' properties
file"public static final java.lang.String DEFAULT_DIR_OPTION_LABEL
IS_DIR is set. The following demonstrates a possible
usage for a FileParam option, where the option label is
'<dir>':
out <dir> the directory in which files will be created
setOptionLabel(),
"FileParam.defaultDirOptionLabel in 'strings' properties
file"| Constructor Detail |
|---|
public FileParam(java.lang.String tag,
java.lang.String desc)
tag - a unique identifier for this parameterdesc - a description of the parameter, suitable for display
in a usage statement
java.lang.IllegalArgumentException - if tag
or setTag(),
setDesc()
public FileParam(java.lang.String tag,
java.lang.String desc,
boolean optional)
tag - a unique identifier for this parameterdesc - a description of the parameter, suitable for display
in a usage statementoptional - OPTIONAL if
optional,
REQUIRED if required
java.lang.IllegalArgumentException - if any of the specified
parameters are invalid.setTag(),
setDesc(),
OPTIONAL,
REQUIRED
public FileParam(java.lang.String tag,
java.lang.String desc,
int attributes)
tag - a unique identifier for this parameterdesc - a description of the parameter, suitable for display
in a usage statementattributes - the attributes that must apply to a file or
directory specified as a value to this FileParam
java.lang.IllegalArgumentException - if any parameter is
invalid.setTag(),
setDesc(),
setAttributes()
public FileParam(java.lang.String tag,
java.lang.String desc,
int attributes,
boolean optional)
tag - a unique identifier for this parameterdesc - a description of the parameter, suitable for display
in a usage statementattributes - the attributes that must apply to a file or
directory specified as a value to this FileParamoptional - OPTIONAL if
optional,
REQUIRED if required
java.lang.IllegalArgumentException - if any parameter is
invalid.setTag(),
setDesc(),
setAttributes(),
OPTIONAL,
REQUIRED
public FileParam(java.lang.String tag,
java.lang.String desc,
int attributes,
boolean optional,
boolean multiValued)
tag - a unique identifier for this parameterdesc - a description of the parameter, suitable for display
in a usage statementattributes - the attributes that must apply to a file or
directory specified as a value to this FileParamoptional - OPTIONAL if
optional,
REQUIRED if requiredmultiValued - MULTI_VALUED if
the parameter can accept multiple values,
SINGLE_VALUED
if the parameter can contain only a single value
java.lang.IllegalArgumentException - if any parameter is
invalid.setTag(),
setDesc(),
setAttributes(),
OPTIONAL,
REQUIRED,
SINGLE_VALUED,
MULTI_VALUED
public FileParam(java.lang.String tag,
java.lang.String desc,
int attributes,
boolean optional,
boolean multiValued,
boolean hidden)
If the IS_DIR attribute is specified, the option label for
this FileParam will be set to DEFAULT_DIR_OPTION_LABEL,
else it will be DEFAULT_FILE_OPTION_LABEL.
tag - a unique identifier for this parameterdesc - a description of the parameter, suitable for display
in a usage statementattributes - the attributes that must apply to a file or
directory specified as a value to this FileParamoptional - OPTIONAL if
optional,
REQUIRED if requiredmultiValued - MULTI_VALUED if
the parameter can accept multiple values,
SINGLE_VALUED
if the parameter can contain only a single valuehidden - HIDDEN if parameter is
not to be listed in the usage,
PUBLIC otherwise.
java.lang.IllegalArgumentException - if any parameter is
invalid.setTag(),
setDesc(),
setOptionLabel(),
setAttributes(),
OPTIONAL,
REQUIRED,
SINGLE_VALUED,
MULTI_VALUED,
HIDDEN,
PUBLIC| Method Detail |
|---|
public java.io.File getFile()
java.lang.RuntimeException - if the value of the FileParam
has not been set.Parameter.isSet()public java.util.Collection getFiles()
Parameter.isSet()
public void validateValue(java.lang.String val)
throws CmdLineException
validateValue in interface ParametervalidateValue in class AbstractParameterval - the value to validate
CmdLineException - if value is not valid.setAttributes()public boolean attrSpecified(int attr)
attr - one of NO_ATTRIBUTES, EXISTS,
DOESNT_EXIST, IS_DIR,
IS_FILE, IS_READABLE, or
IS_WRITEABLE
true if the attribute is set,
false if the attribute is not set or
attr is not a valid attributepublic void setAttributes(int attributes)
FileParam fp = new FileParam("tempDir",
"a directory in which temporary files can be stored",
FileParam.IS_DIR & FileParam.IS_WRITEABLE);
In this case, a valid parameter value would have to be both a
directory and writeable.
Specify NO_ATTRIBUTES if none of the other attributes is
required.
attributes - a combination of NO_ATTRIBUTES,
EXISTS,
DOESNT_EXIST,
IS_DIR, IS_FILE,
IS_READABLE, and IS_WRITEABLE
java.lang.IllegalArgumentException - if the attributes value
is invalid.getAttributes()public int getAttributes()
setAttributes()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||