public class CommandLineArguments
extends java.lang.Object
Here is an example of calling a program from the command line with arguments:
A sample program: java myProgram systemName -userid myID -password myPWD
The Java code to parse the command:
// Create a vector to hold all the defined/expected command line arguments.
Vector options = new Vector();
options.addElement("-userID");
options.addElement("-password");
// Create a Hashtable to map shortcuts to the command line arguments.
Hashtable shortcuts = new Hashtable();
shortcuts.put("-u", "-userID");
shortcuts.put("-p", "-password");
// Create a CommandLineArguments object with the args array passed into main(String args[])
// along with the vector and hashtable just created.
CommandLineArguments arguments = new CommandLineArguments(args, options, shortcuts);
// Get the name of the IBM i system that the user wants to run to.
String system = arguments.getOptionValue("");
// Get the user ID that the user wants to log in with.
String uid = arguments.getOptionValue("-userID");
// Get the password that the user wants to log in with.
String pwd = arguments.getOptionValue("-password");
| Constructor and Description |
|---|
CommandLineArguments(java.lang.String[] args)
Creates a CommandLineArguments object.
|
CommandLineArguments(java.lang.String[] args,
java.util.Vector expectedOptions,
java.util.Hashtable shortcuts)
Creates a CommandLineArguments object.
|
| Modifier and Type | Method and Description |
|---|---|
java.util.Enumeration |
getExtraOptions()
Returns the list of any extra options that were specified.
|
java.util.Enumeration |
getOptionNames()
Returns the list of option names that were specified.
|
java.lang.String |
getOptionValue(java.lang.String optionName)
Returns the value of an option.
|
boolean |
isOptionSpecified(java.lang.String optionName)
Indicates whether the option was specified.
|
public CommandLineArguments(java.lang.String[] args)
args - The command line arguments.public CommandLineArguments(java.lang.String[] args,
java.util.Vector expectedOptions,
java.util.Hashtable shortcuts)
args - The command line arguments.expectedOptions - The expected options. This is a Vector of
Strings, each starting with a hyphen ("-").
These are not case sensitive. Specify null
if any options are expected.shortcuts - The shortcuts. This is a Hashtable where
the keys are Strings for the shortcuts (e.g. "-?") and
the elements are Strings for the option (e.g. "-help").
All strings start with a hyphen ("-"). Specify
null if no shortcuts are used.public java.util.Enumeration getExtraOptions()
public java.util.Enumeration getOptionNames()
public java.lang.String getOptionValue(java.lang.String optionName)
optionName - The option name.public boolean isOptionSpecified(java.lang.String optionName)
optionName - The option name.