java.lang.Object
io.sf.jclf.app.CommandLine
Command-line parameter/argument handling.
Generic encapsulation of command-line parameters, with the following general rules:
- Only single-letter options can have name=value assignations on it
- Only double-dash options can have values assigned by equals
- Double-dash options can't have contiguous values (e.g. -p2 is valid but not --p2)
For example,
- it is legal: -s -p2 -Dname1=value1 -Dname2=value2 --path=/usr/bin
- It is not legal: -p=2 -q=3
- and --varPATH=/usr/bin would be interpreted as parameter "varPATH" having value "/usr/bin", and not as "PATH" being a variable of type "var" with the value "/usr/bin".
Those options prefixed by a "-" or "--" are called "parameters" or "options", while the rest are called "last arguments" or simply "arguments".
Usage example:
Arguments: -afirst_arg -Dname1=value -Dlogic --dir=/var/tmp
public static void main(String[] args) { CommandLine cmdline = new CommandLine(args); String opt_a = cmdline.getParam("a"); // opt_a = "first_arg" Enumeration enu = cmdline.getParameterValues("D"); while (enu.hasMoreElements()) { String s = (String) enu.nextElement(); // s = "name1=value" // s = "logic" } String param_dir = cmdline.getAssignParam("dir"); // param_dir = "/var/tmp" }
-
Constructor Summary
ConstructorDescriptionCommandLine
(String[] cmdline) Constructor with a String array of command line arguments. -
Method Summary
Modifier and TypeMethodDescriptionString[]
getAssignParam
(String pname) Gets the value assigned to a '--' parameter.Returns the last command-line argument (the last of those arguments not starting with a "-").String[]
Returns the last command-line arguments (those arguments not starting with a "-").Gets a parameter of a given name.getParameterValues
(String pname) boolean
hasParameter
(String pname) Tells if the command line has a parameter of the given name.toString()
-
Constructor Details
-
CommandLine
Constructor with a String array of command line arguments.- Parameters:
cmdline
- the String array of command line arguments as given in themain
method.
-
-
Method Details
-
getParam
Gets a parameter of a given name.- Parameters:
pname
- parameter name.- Returns:
- the parameter value, empty string if the parameter has no value, or null if the parameter is not present at all.
-
getAssignParam
Gets the value assigned to a '--' parameter.- Parameters:
pname
- parameter name.- Returns:
- the parameter value, empty string if the parameter has no value, or null if the parameter is not present at all.
-
hasParameter
Tells if the command line has a parameter of the given name.- Parameters:
pname
- the parameter name.- Returns:
true
if the parameter exists,false
otherwise.
-
getLastArgument
Returns the last command-line argument (the last of those arguments not starting with a "-").- Returns:
- the last argument.
-
getLastArguments
Returns the last command-line arguments (those arguments not starting with a "-").- Returns:
- the last arguments.
-
getArguments
- Returns:
- the command-line array.
-
getParameterValues
-
toString
-