Module dev.civl.gmc
Package dev.civl.gmc

Class CommandLineParser

java.lang.Object
dev.civl.gmc.CommandLineParser

public class CommandLineParser extends Object
A tool for parsing a command line and generating a GMCConfiguration. A parser is instantiated by specifying the set of options that it will be able to parse. It can then be used repeatedly to parse a command line and produce a configuration.
  • Constructor Details

    • CommandLineParser

      public CommandLineParser(Collection<Option> options)
      Constructs a new parser from the given collection of options.
      Parameters:
      options - a collection of non-null options with distinct names
  • Method Details

    • newConfig

      public GMCConfiguration newConfig()
      Returns new, empty configuration with option set equal to the set of options associated to this parser.
      Returns:
      new empty configuration compatible with this parser
    • parse

      public void parse(GMCConfiguration config, Collection<String> args) throws CommandLineException
      Given a collection of strings and a configuration compatible with this parser, parses the strings and uses the resulting information to modify the configuration. The strings are expected to be in the following format:
       ('-' text '=' text)* text* (('--' text) ('-' text '=' text)* text*)*
       
      where text is a string that doesn't start with '-' and contains no space.
      Parameters:
      config - a configuration with the same option set as this parser
      args - a collection of strings, the command line arguments
      Throws:
      CommandLineException - if the args do not conform to what is expected. What is expected is determined by the set of options associated to this parser.
    • parse

      Given a collection of strings, parses them in the order of their iterator to produce a new configuration.
      Parameters:
      args - a collection of strings, the command line arguments
      Returns:
      a new configuration obtained from the information in the args
      Throws:
      CommandLineException - if the args do not conform to what is expected. What is expected is determined by the set of options associated to this parser.
    • parse

      public void parse(GMCConfiguration config, BufferedReader reader) throws IOException, CommandLineException
      Parses the reader, interpreting lines as command line args, modifying given configuration accordingly. Ignores all lines until it reaches a line of the form "== Begin Configuration ==". Then parses each line as if it were a command line argument. Continues until end of stream or a line of the form "== End Configuration ==" is reached.
      Parameters:
      config - a configuration compatible with this parser
      reader - a buffered reader which provides a sequence of lines as described above
      Throws:
      IOException - if an I/O error occurs in reading the reader
      CommandLineException - if format does not conform to pattern described above
    • parse

      public void parse(GMCConfiguration config, File file) throws FileNotFoundException, IOException, CommandLineException
      Parses a file containing a configuration section, using the command line arguments from that section to modify the given configuration.
      Parameters:
      config - a configuration to be modified; it must have the same set of options as those associated to this parser
      file - the file to open and parse; it must contain a configuration section
      Throws:
      FileNotFoundException - if the file does not exist
      IOException - if an I/O error occurs in opening, reading, or closing the file
      CommandLineException - if the command lines do not conform to the options of this parser
    • printUsage

      public void printUsage(PrintStream out)
      Prints the list of options in a human-readable format. Appropriate for including in a "usage" message for the user.
      Parameters:
      out - print stream to which to print this information