public final class Arguments extends Entity
1) Specify available parameters by creating constants of the Parameter
type:
public static final PATH_PARAM = new Parameter("I", "path", true, "Adds a directory"); public static final HELP_PARAM = new Parameter("h", "help", false, "Shows this message");
2) Create a Parameters
instance and add the specified parameters:
Parameters params = new Parameters(); params.addParameter(PATH_PARAM); params.addParameter(HELP_PARAM);
3) Create a Arguments
instance and parse a command line:
Arguments args = new Arguments(params); args.setCommandLine(strings);
4) Get arguments using the contains
and getValue
methods.
if(args.contains(HELP_PARAM) { params.help(); } if(args.contains(PATH_PARAM)) { addDirectory(args.getValue(PATH_PARAM)); }
Modifier and Type | Field and Description |
---|---|
static EntityType |
TYPE |
Constructor and Description |
---|
Arguments(Parameters parameters)
Constructs an arguments entity with the given specification of parameters.
|
Arguments(Parameters parameters,
java.lang.String... args)
Constructs an arguments entity with the given specification of parameters.
|
Modifier and Type | Method and Description |
---|---|
void |
add(Entity entity)
Adds a given entity to this one.
|
boolean |
contains(Parameter parameter)
Checks whether the given parameter is specified.
|
java.lang.String[] |
getArguments()
Returns the unparsed part of the command line (typically, list of files).
|
java.lang.String[] |
getCommandLine()
Returns the command line having been set before.
|
java.lang.String |
getValue(Parameter parameter)
Returns the parameter value.
|
java.lang.String |
getValue(Parameter parameter,
java.lang.String defaultValue)
Returns the parameter value.
|
void |
setCommandLine(java.lang.String... args)
Parses the given command line.
|
java.lang.String |
toString() |
equals, getEntityType, hashCode
addMetaInfo, addMetaInfo, addMetaInfo, addMetaInfo, getAllMetaInfo, getMetaInfo, getMetaInfo, getStringMetaInfo, getStringMetaInfo, hasMetaInfo, hasMetaInfo
public static final EntityType TYPE
public Arguments(Parameters parameters)
parameters
- the parameters specification.public Arguments(Parameters parameters, java.lang.String... args) throws org.apache.commons.cli.ParseException
parameters
- the parameters specification.args
- the command line to be parsed.org.apache.commons.cli.ParseException
- the parse exception.public void setCommandLine(java.lang.String... args) throws org.apache.commons.cli.ParseException
args
- the command line to be parsed.org.apache.commons.cli.ParseException
- the parse exception.public java.lang.String[] getCommandLine()
public boolean contains(Parameter parameter)
parameter
- the parameter to be checked.true
if the parameter is specified; false
otherwise.public java.lang.String getValue(Parameter parameter)
parameter
- the parameter which value to be returned.null
otherwise.public java.lang.String getValue(Parameter parameter, java.lang.String defaultValue)
parameter
- the parameter which value to be returned.defaultValue
- the value to be returned if the parameter is absentpublic java.lang.String[] getArguments()
public void add(Entity entity)
Entity