public class ArgParser
extends java.lang.Object
ArgParser
gives support for command line parsing.
Options use a hyphen and a single character and can take a number of
arguments.
The options -?
and -h
are predefined and should be
used to print the usage via printUsage
.Constructor and Description |
---|
ArgParser(java.lang.String name,
java.lang.String usage)
Constructs a new argument parser for the program.
|
ArgParser(java.lang.String name,
java.lang.String usage,
int args)
Constructs a new argument parser for the program.
|
ArgParser(java.lang.String _name,
java.lang.String _usage,
int _minArgs,
int _maxArgs)
Constructs a new argument parser for the program.
|
Modifier and Type | Method and Description |
---|---|
ArgParser |
addOption(char option)
Adds an option without arguments to the parser.
|
ArgParser |
addOption(char option,
int numberOfArguments)
Adds an option with exactly
args arguments to the parser. |
ArgParser |
addOption(char option,
int _minimumNumberOfArguments,
int _maximumNumberOfArguments)
Adds an option to the parser.
|
java.lang.String |
getArgument() |
java.lang.String |
getArgument(char option) |
java.lang.String |
getArgument(char option,
java.lang.String defaultValue) |
java.lang.String |
getArgument(java.lang.String defaultValue) |
int |
getArgumentCount() |
int |
getArgumentCount(char option) |
java.util.List<java.lang.String> |
getArguments() |
java.util.List<java.lang.String> |
getArguments(char option) |
boolean |
getBoolArgument() |
boolean |
getBoolArgument(boolean defaultValue) |
boolean |
getBoolArgument(char option) |
boolean |
getBoolArgument(char option,
boolean defaultValue) |
double |
getDoubleArgument() |
double |
getDoubleArgument(char option) |
double |
getDoubleArgument(char option,
double defaultValue) |
double |
getDoubleArgument(double defaultValue) |
int |
getIntArgument() |
int |
getIntArgument(char option) |
int |
getIntArgument(char option,
int defaultValue) |
int |
getIntArgument(int defaultValue) |
java.lang.String |
getNthArgument(char option,
int n) |
java.lang.String |
getNthArgument(int n) |
boolean |
getNthBoolArgument(char option,
int n) |
boolean |
getNthBoolArgument(int n) |
double |
getNthDoubleArgument(char option,
int n) |
double |
getNthDoubleArgument(int n) |
int |
getNthIntArgument(char option,
int n) |
int |
getNthIntArgument(int n) |
boolean |
hasArgument(char option,
int n)
Returns whether a nth argument is available for an option or not.
|
boolean |
hasArgument(int n)
Returns whether a nth argument is available for the program or not.
|
boolean |
hasArguments()
Returns whether the program has arguments.
|
boolean |
hasArguments(char option)
Returns whether the option has arguments or is provided at all.
|
boolean |
hasOption(char option) |
boolean |
parse(java.lang.String[] arguments)
Parses the arguments.
|
boolean |
parseSilent(java.lang.String[] arguments)
Parses the arguments and returns whether an error or a help option
occurred.
|
void |
printError(java.io.PrintStream out,
java.lang.String msg) |
void |
printError(java.lang.String msg) |
void |
printUsage() |
void |
printUsage(java.io.PrintStream out) |
public ArgParser(java.lang.String _name, java.lang.String _usage, int _minArgs, int _maxArgs)
_name
- name of the program_usage
- usage text for the program_minArgs
- the minimum number of additional arguments_maxArgs
- the maximum number of additional arguments or -1 for no maximum.public ArgParser(java.lang.String name, java.lang.String usage, int args)
name
- name of the programusage
- usage text for the programargs
- the number of required argumentspublic ArgParser(java.lang.String name, java.lang.String usage)
name
- name of the programusage
- usage text for the programpublic boolean parse(java.lang.String[] arguments)
-h
or -?
is passed as argument the
method will return true.ArgumentException
- if any option/argument constraint has been broken.to prevent an exception to be thrown
public boolean parseSilent(java.lang.String[] arguments)
public void printUsage(java.io.PrintStream out)
public void printUsage()
public void printError(java.io.PrintStream out, java.lang.String msg)
public void printError(java.lang.String msg)
public ArgParser addOption(char option, int _minimumNumberOfArguments, int _maximumNumberOfArguments)
minArgs
and maxArgs
._minimumNumberOfArguments
- the minimum number of arguments for this option_maximumNumberOfArguments
- the maximum number of arguments for this option or -1 for no
maximum.public ArgParser addOption(char option, int numberOfArguments)
args
arguments to the parser.option
- the option characternumberOfArguments
- the number of arguments for this optionpublic ArgParser addOption(char option)
option
- the option characterpublic boolean hasOption(char option)
public int getArgumentCount()
public int getArgumentCount(char option)
public boolean hasArguments()
public boolean hasArguments(char option)
public boolean hasArgument(int n)
public boolean hasArgument(char option, int n)
public java.lang.String getArgument()
public java.util.List<java.lang.String> getArguments()
public java.lang.String getArgument(java.lang.String defaultValue)
public java.lang.String getNthArgument(int n)
public java.util.List<java.lang.String> getArguments(char option)
public java.lang.String getArgument(char option)
public java.lang.String getArgument(char option, java.lang.String defaultValue)
public java.lang.String getNthArgument(char option, int n)
public int getIntArgument()
public int getIntArgument(int defaultValue)
public int getNthIntArgument(int n)
public int getIntArgument(char option)
public int getIntArgument(char option, int defaultValue)
public int getNthIntArgument(char option, int n)
public boolean getBoolArgument()
public boolean getBoolArgument(boolean defaultValue)
public boolean getNthBoolArgument(int n)
public boolean getBoolArgument(char option)
public boolean getBoolArgument(char option, boolean defaultValue)
public boolean getNthBoolArgument(char option, int n)
public double getDoubleArgument()
public double getDoubleArgument(double defaultValue)
public double getNthDoubleArgument(int n)
public double getDoubleArgument(char option)
public double getDoubleArgument(char option, double defaultValue)
public double getNthDoubleArgument(char option, int n)