Package com.nt.udc.util
Class CommandLineParser
java.lang.Object
com.nt.udc.util.CommandLineParser
This class is for parsing command lines, as typed by a user to
a Java program. The CommandLineParser organizes the command line
into a series of settings and values for easier querying by the
actual program.
The CommandLineParser groups the incoming command line into two
types of settings:
- switches (settings without values)
- options (settings with values)
For example, a switch that turns on large font may look like this:
"-bigfont".
and an option that sets a configuration file may be:
"-cfgfile /homedir/cfg.txt"
When instantiating the CommandLineParser, it expects to be told
what the valid switches and options are, so that it can inform the
user when they have entered a bad command line.
An example of a command line follows, assuming the switches and
options are supported:
java com.nt.udc.myProgram -file /homedir/file.txt -output .
-nowarnings -noinfo
-
Constructor Summary
ConstructorsConstructorDescriptionCommandLineParser
(String[] commandLine, String[] switches, String[] options) Given a command line, valid switches and valid options, initialize this parser. -
Method Summary
Modifier and TypeMethodDescriptionint
getOptionValue
(String optionStr) Given the option setting, return its value.int
boolean
isOptionDefined
(String optionStr) Returns whether this option was used on the command line.boolean
isSwitchDefined
(String switchStr) Returns whether this switch was used on the command line.boolean
Parses the command line, and returns whether the parse was successful.
-
Constructor Details
-
CommandLineParser
Given a command line, valid switches and valid options, initialize this parser.- Parameters:
commandLine
- Command line to parseswitches
- valid switchesoptions
- valid options
-
-
Method Details
-
parseCommandLine
public boolean parseCommandLine()Parses the command line, and returns whether the parse was successful.- Returns:
- true, if parsing was successful
-
getOptionValue
Given the option setting, return its value.- Parameters:
optionStr
- Name of option- Returns:
- option's value, or null if no option found
-
isSwitchDefined
Returns whether this switch was used on the command line.- Parameters:
switchStr
- Name of switch- Returns:
- true, if switch was used
-
isOptionDefined
Returns whether this option was used on the command line.- Parameters:
optionStr
- Name of option- Returns:
- true, if option was used
-
getSwitchCount
public int getSwitchCount() -
getOptionCount
public int getOptionCount()
-