net.opentsdb.tools
Class ArgP

java.lang.Object
  extended by net.opentsdb.tools.ArgP

public final class ArgP
extends Object

A dead simple command-line argument parser. Because I couldn't find any one in Java that wasn't horribly bloated.

Example:

public static void main(String[] args) {
     final ArgP argp = new ArgP();
     argp.addOption("--verbose", "Whether or not to be verbose.");
     argp.addOption("--path", "PATH", "The input path to read.");
     try {
       args = argp.parse(args);
     } catch (IllegalArgumentException e) {
       System.err.println(e.getMessage());
       System.err.print(argp.usage());  // Note: usage already ends with \n.
       System.exit(1);
     }
     final boolean verbose = argp.has("--verbose");
     final String path = argp.get("--path");  // Check that it's non-null.
     ...
   }
 
This parser honors the convention that argument -- means "stop parsing options".

This class is not thread-safe.


Constructor Summary
ArgP()
          Constructor.
 
Method Summary
 void addOption(String name, String help)
          Registers an option that doesn't take a value in this argument parser.
 void addOption(String name, String meta, String help)
          Registers an option in this argument parser.
 void addUsageTo(StringBuilder buf)
          Appends the usage to the given buffer.
 String get(String name)
          Returns the value of the given option, if it was given.
 String get(String name, String defaultv)
          Returns the value of the given option, or a default value.
 boolean has(String name)
          Returns whether or not the given option was given.
 boolean optionExists(String name)
          Returns whether or not the given option name exists.
 String[] parse(String[] args)
          Parses the command line given in argument.
 String toString()
           
 String usage()
          Returns a usage string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ArgP

public ArgP()
Constructor.

Method Detail

addOption

public void addOption(String name,
                      String meta,
                      String help)
Registers an option in this argument parser.

Parameters:
name - The name of the option to recognize (e.g. --foo).
meta - The meta-variable to associate with the value of the option.
help - A short description of this option.
Throws:
IllegalArgumentException - if the given name was already used.
IllegalArgumentException - if the name doesn't start with a dash.
IllegalArgumentException - if any of the given strings is empty.

addOption

public void addOption(String name,
                      String help)
Registers an option that doesn't take a value in this argument parser.

Parameters:
name - The name of the option to recognize (e.g. --foo).
help - A short description of this option.
Throws:
IllegalArgumentException - if the given name was already used.
IllegalArgumentException - if the name doesn't start with a dash.
IllegalArgumentException - if any of the given strings is empty.

optionExists

public boolean optionExists(String name)
Returns whether or not the given option name exists.

Calling addOption(foo, ...) entails that optionExists(foo) returns true.

Parameters:
name - The name of the option to recognize (e.g. --foo).

parse

public String[] parse(String[] args)
Parses the command line given in argument.

Returns:
The remaining words that weren't options (i.e. that didn't start with a dash).
Throws:
IllegalArgumentException - if the given command line wasn't valid.

get

public String get(String name)
Returns the value of the given option, if it was given. Returns null if the option wasn't given, or if the option doesn't take a value (in which case you should use has(java.lang.String) instead).

Parameters:
name - The name of the option to recognize (e.g. --foo).
Throws:
IllegalArgumentException - if this option wasn't registered with addOption(java.lang.String, java.lang.String, java.lang.String).
IllegalStateException - if parse(java.lang.String[]) wasn't called.

get

public String get(String name,
                  String defaultv)
Returns the value of the given option, or a default value.

Parameters:
name - The name of the option to recognize (e.g. --foo).
defaultv - The default value to return if the option wasn't given.
Throws:
IllegalArgumentException - if this option wasn't registered with addOption(java.lang.String, java.lang.String, java.lang.String).
IllegalStateException - if parse(java.lang.String[]) wasn't called.

has

public boolean has(String name)
Returns whether or not the given option was given.

Parameters:
name - The name of the option to recognize (e.g. --foo).
Throws:
IllegalArgumentException - if this option wasn't registered with addOption(java.lang.String, java.lang.String, java.lang.String).
IllegalStateException - if parse(java.lang.String[]) wasn't called.

addUsageTo

public void addUsageTo(StringBuilder buf)
Appends the usage to the given buffer.

Parameters:
buf - The buffer to write to.

usage

public String usage()
Returns a usage string.


toString

public String toString()
Overrides:
toString in class Object