public class FileParam extends AbstractParameter<File>
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 (File f : filesArg.getValues()) {
processStream(new FileInputStream(f);
}
} else {
processStream(System.in);
}
}
CmdLineParser| Modifier and Type | Field and Description |
|---|---|
static String |
DEFAULT_DIR_OPTION_LABEL
the default label that will represent option values for this Parameter where
IS_DIR is set. |
static 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
|
acceptableValues, desc, hidden, ignoreRequired, multiValued, optional, optionLabel, set, tag, valuesHIDDEN, MULTI_VALUED, OPTIONAL, PUBLIC, REQUIRED, SINGLE_VALUED| Constructor and Description |
|---|
FileParam(String tag,
String desc)
constructor - creates single-valued, optional, public parameter which accepts any valid file or directory name as
its value
|
FileParam(String tag,
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(String tag,
String desc,
int attributes)
constructor - creates a single-valued, optional, public, parameter accepts a file or directory name with the
specified attributes.
|
FileParam(String tag,
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(String tag,
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(String tag,
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.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
attrSpecified(int attr)
Indicates whether an attribute has been specified for this FileParam.
|
File |
convertValue(String strVal)
Converts a String value to the type associated with the Parameter.
|
int |
getAttributes()
gets the value of attributes
|
void |
setAttributes(int attributes)
Sets the value of attributes.
|
void |
validateValue(File f)
Validates a prospective value for the FileParam - called by add/setValue(s)().
|
addStringValue, addValue, getAcceptableValues, getDesc, getIgnoreRequired, getOptionLabel, getTag, getValue, getValues, isHidden, isMultiValued, isOptional, isSet, setAcceptableValues, setAcceptableValues, setDesc, setHidden, setIgnoreRequired, setMultiValued, setOptional, setOptionLabel, setTag, setValue, setValues, setValuespublic static final 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"public static final 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 int DOESNT_EXIST
setAttributes(),
Constant Field Valuespublic static final int EXISTS
setAttributes(),
Constant Field Valuespublic static final int IS_DIR
setAttributes(),
Constant Field Valuespublic static final int IS_FILE
setAttributes(),
Constant Field Valuespublic static final int IS_READABLE
setAttributes(),
Constant Field Valuespublic static final int IS_WRITEABLE
setAttributes(),
Constant Field Valuespublic static final int NO_ATTRIBUTES
setAttributes(),
Constant Field Valuespublic FileParam(String tag, String desc)
tag - a unique identifier for this parameterdesc - a description of the parameter, suitable for display in a usage statementIllegalArgumentException - if tag or desc are invalid.setTag(),
setDesc()public FileParam(String tag, 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 requiredIllegalArgumentException - if any of the specified parameters are invalid.setTag(),
setDesc(),
OPTIONAL,
REQUIREDpublic FileParam(String tag, 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 FileParamIllegalArgumentException - if any parameter is invalid.setTag(),
setDesc(),
setAttributes()public FileParam(String tag, 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 requiredIllegalArgumentException - if any parameter is invalid.setTag(),
setDesc(),
setAttributes(),
OPTIONAL,
REQUIREDpublic FileParam(String tag, 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 valueIllegalArgumentException - if any parameter is invalid.setTag(),
setDesc(),
setAttributes(),
OPTIONAL,
REQUIRED,
SINGLE_VALUED,
MULTI_VALUEDpublic FileParam(String tag, 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.IllegalArgumentException - if any parameter is invalid.setTag(),
setDesc(),
setOptionLabel(),
setAttributes(),
OPTIONAL,
REQUIRED,
SINGLE_VALUED,
MULTI_VALUED,
HIDDEN,
PUBLICpublic boolean attrSpecified(int attr)
attr - one of NO_ATTRIBUTES, EXISTS, DOESNT_EXIST, IS_DIR,
IS_FILE, IS_READABLE, or IS_WRITEABLEtrue if the attribute is set, false if the attribute is not set or
attr is not a valid attributepublic File convertValue(String strVal) throws CmdLineException
AbstractParameterconvertValue in class AbstractParameter<File>strVal - the String value of the ParameterCmdLineException - if the conversion cannot be madeAbstractParameter.convertValue(java.lang.String)public int getAttributes()
setAttributes()public 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_WRITEABLEIllegalArgumentException - if the attributes value is invalid.getAttributes()public void validateValue(File f) throws CmdLineException
validateValue in class AbstractParameter<File>f - the value to validateCmdLineException - if f is not valid.setAttributes()Copyright © 2017. All rights reserved.