java.lang.Object
dev.civl.gmc.CommandLineParser
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 Summary
ConstructorsConstructorDescriptionCommandLineParser(Collection<Option> options) Constructs a new parser from the given collection of options. -
Method Summary
Modifier and TypeMethodDescriptionReturns new, empty configuration with option set equal to the set of options associated to this parser.voidparse(GMCConfiguration config, BufferedReader reader) Parses the reader, interpreting lines as command line args, modifying given configuration accordingly.voidparse(GMCConfiguration config, File file) Parses a file containing a configuration section, using the command line arguments from that section to modify the given configuration.voidparse(GMCConfiguration config, Collection<String> args) Given a collection of strings and a configuration compatible with this parser, parses the strings and uses the resulting information to modify the configuration.parse(Collection<String> args) Given a collection of strings, parses them in the order of their iterator to produce a new configuration.voidprintUsage(PrintStream out) Prints the list of options in a human-readable format.
-
Constructor Details
-
CommandLineParser
Constructs a new parser from the given collection of options.- Parameters:
options- a collection of non-null options with distinct names
-
-
Method Details
-
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
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*)*wheretextis a string that doesn't start with '-' and contains no space.- Parameters:
config- a configuration with the same option set as this parserargs- 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 parserreader- a buffered reader which provides a sequence of lines as described above- Throws:
IOException- if an I/O error occurs in reading the readerCommandLineException- 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 parserfile- the file to open and parse; it must contain a configuration section- Throws:
FileNotFoundException- if the file does not existIOException- if an I/O error occurs in opening, reading, or closing the fileCommandLineException- if the command lines do not conform to the options of this parser
-
printUsage
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
-