public class CommandLineParser
extends java.lang.Object
The parser can only handle parameters that take 0 or 1 arguments. That is, you can parse command lines like
doit -f -i file1 -o file2 --dir file3 /h file4 file5 file6
The syntax of parameters is left to the user, no common prefix is assumed or enforced. Parameter
names can be arbitrarily long. You can define aliases for parameters: -h
,
/H
and --help
can all mean the same parameter if so configured.
Constructor and Description |
---|
CommandLineParser()
Create a new command line parser.
|
Modifier and Type | Method and Description |
---|---|
boolean |
addAlias(java.lang.String param,
java.lang.String alias)
Add an alias for an already defined parameter.
|
boolean |
addParameter(java.lang.String paramName)
Add a new switch.
|
boolean |
addParameter(java.lang.String paramName,
boolean hasArg)
Add a new parameter name.
|
java.lang.String |
getParamArgument(java.lang.String paramName)
Get the argument to a parameter, if it exists.
|
java.lang.String[] |
getRestArgs()
Get the rest of the args, i.e., args that follow the last know parameter.
|
boolean |
isInArgsList(java.lang.String paramName)
Check if the parameter was used on the command line.
|
boolean |
isKnownParameter(java.lang.String paramName)
Check if the given parameter name is known to this parser.
|
void |
parseCmdLine(java.lang.String[] args)
Parse the command line.
|
public boolean addParameter(java.lang.String paramName, boolean hasArg)
paramName
- The name of the parameter.hasArg
- If the command line argument following this parameter should be interpreted as an
argument to the parameter.false
iff paramName
already exists.public boolean addParameter(java.lang.String paramName)
addParameter(name, false)
.paramName
- The name of the parameter.false
iff paramName
already exists.public boolean addAlias(java.lang.String param, java.lang.String alias)
param
- A known parameter.alias
- The alias.false
iff the parameter does not exist or the alias is already known.public void parseCmdLine(java.lang.String[] args) throws java.lang.Exception
args
- The command line args as passed to main()
.java.lang.Exception
- If a parameter that requires an argument does not have one (i.e., is the last
parameter in the list).public java.lang.String[] getRestArgs()
public boolean isKnownParameter(java.lang.String paramName)
paramName
- The name of the parameter.true
iff the name was added with
addParameter()
or
addAlias()
.public boolean isInArgsList(java.lang.String paramName)
paramName
- The name of the parameter.true
iff the name is known and was used as a command line argument.public java.lang.String getParamArgument(java.lang.String paramName)
paramName
- The name of the parameter.null
, else.Copyright © 2012. All Rights Reserved.