Class CommandLineParser

java.lang.Object
com.nt.udc.util.CommandLineParser

public class CommandLineParser extends Object
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 Details

    • CommandLineParser

      public CommandLineParser(String[] commandLine, String[] switches, String[] options)
      Given a command line, valid switches and valid options, initialize this parser.
      Parameters:
      commandLine - Command line to parse
      switches - valid switches
      options - 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

      public String getOptionValue(String optionStr)
      Given the option setting, return its value.
      Parameters:
      optionStr - Name of option
      Returns:
      option's value, or null if no option found
    • isSwitchDefined

      public boolean isSwitchDefined(String switchStr)
      Returns whether this switch was used on the command line.
      Parameters:
      switchStr - Name of switch
      Returns:
      true, if switch was used
    • isOptionDefined

      public boolean isOptionDefined(String optionStr)
      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()