|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object net.opentsdb.tools.ArgP
public final class ArgP
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 |
---|
public ArgP()
Method Detail |
---|
public void addOption(String name, String meta, String help)
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.
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.public void addOption(String name, String help)
name
- The name of the option to recognize (e.g. --foo
).help
- A short description of this option.
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.public boolean optionExists(String name)
Calling
addOption
(foo, ...)
entails that optionExists(foo)
returns true
.
name
- The name of the option to recognize (e.g. --foo
).public String[] parse(String[] args)
IllegalArgumentException
- if the given command line wasn't valid.public String get(String name)
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).
name
- The name of the option to recognize (e.g. --foo
).
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.public String get(String name, String defaultv)
name
- The name of the option to recognize (e.g. --foo
).defaultv
- The default value to return if the option wasn't given.
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.public boolean has(String name)
name
- The name of the option to recognize (e.g. --foo
).
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.public void addUsageTo(StringBuilder buf)
buf
- The buffer to write to.public String usage()
public String toString()
toString
in class Object
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |