com.google.caliper.util
Class CommandLineParser<T>

java.lang.Object
  extended by com.google.caliper.util.CommandLineParser<T>

public final class CommandLineParser<T>
extends Object

Parses command line options. Strings in the passed-in String[] are parsed left-to-right. Each String is classified as a short option (such as "-v"), a long option (such as "--verbose"), an argument to an option (such as "out.txt" in "-f out.txt"), or a non-option positional argument. A simple short option is a "-" followed by a short option character. If the option requires an argument (which is true of any non-boolean option), it may be written as a separate parameter, but need not be. That is, "-f out.txt" and "-fout.txt" are both acceptable. It is possible to specify multiple short options after a single "-" as long as all (except possibly the last) do not require arguments. A long option begins with "--" followed by several characters. If the option requires an argument, it may be written directly after the option name, separated by "=", or as the next argument. (That is, "--file=out.txt" or "--file out.txt".) A boolean long option '--name' automatically gets a '--no-name' companion. Given an option "--flag", then, "--flag", "--no-flag", "--flag=true" and "--flag=false" are all valid, though neither "--flag true" nor "--flag false" are allowed (since "--flag" by itself is sufficient, the following "true" or "false" is interpreted separately). You can use "yes" and "no" as synonyms for "true" and "false". Each String not starting with a "-" and not a required argument of a previous option is a non-option positional argument, as are all successive Strings. Each String after a "--" is a non-option positional argument. The fields corresponding to options are updated as their options are processed. Any remaining positional arguments are returned as an ImmutableList. Here's a simple example: // This doesn't need to be a separate class, if your application doesn't warrant it. // Non-@Option fields will be ignored. class Options {


Nested Class Summary
static interface CommandLineParser.Leftovers
          Annotates a single method in an options class to receive any "leftover" arguments.
static interface CommandLineParser.Option
          Annotates a field or method in an options class to signify that parsed values should be injected.
 
Method Summary
static
<T> CommandLineParser<T>
forClass(Class<? extends T> c)
           
 void parseAndInject(String[] args, T injectee)
          Parses the command-line arguments 'args', setting the @Option fields of the 'optionSource' provided to the constructor.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

forClass

public static <T> CommandLineParser<T> forClass(Class<? extends T> c)

parseAndInject

public void parseAndInject(String[] args,
                           T injectee)
                    throws InvalidCommandException
Parses the command-line arguments 'args', setting the @Option fields of the 'optionSource' provided to the constructor. Returns a list of the positional arguments left over after processing all options.

Throws:
InvalidCommandException


Copyright © 2009-2011 Google, Inc.. All Rights Reserved.