public class ArgsEngine
A reusable solution for Command Line Arguments Parsing in Java.
The following shows how to use this API.
// Initiate the arguments engine.
ArgsEngine engine = new ArgsEngine();
// Configure the switches/options. Use true for valued options
engine.add("-q", "--quiet");
engine.add("-o", "--redirect-output", true);
engine.add("-h", "--help");
// Perform the parsing.
engine.parse(args);
// Start fetching states of switches
boolean quiet = engine.getBoolean("-q");
if(engine.getBoolean("-o"))
{
// For valued options, use getString.
String redir = engine.getString("-o");
}
This has been modified from the original.public void add(java.lang.String shortForm,
java.lang.String longForm)
Configures the engine by adding options.
shortForm - the short form of an option. Example: "-h".longForm - the long form of an option. Example: "--help".public void add(java.lang.String shortForm,
java.lang.String longForm,
boolean valued)
Configures the engine by adding options.
shortForm - the short form of an option. Example: "-h".longForm - the long form of an option. Example: "--help".valued - indicates if the option expects a value. The next argument will be considered as the value for this option.public void parse(java.lang.String[] args)
Parses the input command line arguments. The result of parsing will be stored in the current instance of ArgsEngine.
Operations , #getNonOptions() and net.maizegenetics.util.ArgsEngine$getBoolean(java.lang.String) can be then used to extract the values.net.maizegenetics.util.ArgsEngine$getString(java.lang.String)
args - the command line arguments.net.maizegenetics.util.ArgsEngine$getBoolean(java.lang.String),
net.maizegenetics.util.ArgsEngine$getString(java.lang.String)public java.lang.String getString(java.lang.String key)
Gets the value for a valued option.
key - the valued option key. Either short form or long form can be input.public boolean getBoolean(java.lang.String key)
Gets the option.
key - the option's short form or long form name.