ArgsOptionParser
public
class
ArgsOptionParser
extends OptionSetter
| java.lang.Object | ||
| ↳ | com.android.tradefed.config.OptionSetter | |
| ↳ | com.android.tradefed.config.ArgsOptionParser | |
Populates Option fields from parsed command line arguments.
Option fields. A long option maps to the
Option name, and a short option maps to Option short name. Each option name and
option short name must be unique with respect to all other
Option fields within the same object.
A single option argument can get mapped to multiple Option fields with the same name
across multiple objects. Option arguments can be namespaced to uniquely refer to an
Option field within a single object using that object's full class name or its
OptionClass alias value separated by ':'. ie
--classname:optionname optionvalue or --optionclassalias:optionname optionvalue.
// Non-@Option fields will be ignored.
class Options {
@Option(name = "quiet", shortName = 'q')
boolean quiet = false;
// Here the user can use --no-color.
@Option(name = "color")
boolean color = true;
@Option(name = "mode", shortName = 'm')
String mode = "standard; // Supply a default just by setting the field.
@Option(name = "port", shortName = 'p')
int portNumber = 8888;
// There's no need to offer a short name for rarely-used options.
@Option(name = "timeout" )
double timeout = 1.0;
@Option(name = "output-file", shortName = 'o' })
File output;
// Multiple options are added to the collection.
// The collection field itself must be non-null.
@Option(name = "input-file", shortName = 'i')
List<File> inputs = new ArrayList<File>();
}
Options options = new Options();
List<String> posArgs = new OptionParser(options).parse("--input-file", "/tmp/file1.txt");
for (File inputFile : options.inputs) {
if (!options.quiet) {
...
}
...
}
- the getopt(1) man page
- Python's "optparse" module (http://docs.python.org/library/optparse.html)
- the POSIX "Utility Syntax Guidelines" (http://www.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap12.html#tag_12_02)
- the GNU "Standards for Command Line Interfaces" (http://www.gnu.org/prep/standards/standards.html#Command_002dLine-Interfaces)
See also:
Summary
Public constructors | |
|---|---|
ArgsOptionParser(
Creates a |
|
ArgsOptionParser(Object... optionSources)
Creates a |
|
Public methods | |
|---|---|
|
getInopOptions()
Returns the set of options that did not change any default values. |
static
String
|
getOptionHelp(boolean importantOnly, Object optionObject)
Output help text for all |
|
parse(String... args)
Parses the command-line arguments 'args', setting the @Option fields of the 'optionSource' provided to the constructor. |
|
parse(
Alternate |
|
parseBestEffort(
Alternate |
|
parseBestEffort(
Alternate |
|
parseBestEffort(String... args)
A best-effort version of |
void
|
validateMandatoryOptions()
Validates that all fields marked as mandatory have been set. |
Public constructors
ArgsOptionParser
public ArgsOptionParser (optionSources)
Creates a ArgsOptionParser for a collection of objects.
| Parameters | |
|---|---|
optionSources |
: the config objects. |
| Throws | |
|---|---|
ConfigurationException |
if config objects is improperly configured. |
ArgsOptionParser
public ArgsOptionParser (Object... optionSources)
Creates a ArgsOptionParser for one or more objects.
| Parameters | |
|---|---|
optionSources |
Object: the config objects. |
| Throws | |
|---|---|
ConfigurationException |
if config objects is improperly configured. |
Public methods
getInopOptions
publicgetInopOptions ()
Returns the set of options that did not change any default values.
| Returns | |
|---|---|
|
|
getOptionHelp
public static String getOptionHelp (boolean importantOnly,
Object optionObject)Output help text for all Option fields in optionObject.
[-option_shortname, --option_name] [option_description] Default: [current option field's value in optionObject]
| Parameters | |
|---|---|
importantOnly |
boolean: if true only print help for the important options |
optionObject |
Object: the object to print help text for |
| Returns | |
|---|---|
String |
a String containing user-friendly help text for all Option fields |
parse
publicparse (String... args)
Parses the command-line arguments 'args', setting the @Option fields of the 'optionSource' provided to the constructor.
| Parameters | |
|---|---|
args |
String |
| Returns | |
|---|---|
|
a ERROR(/List) of the positional arguments left over after processing all options. |
| Throws | |
|---|---|
ConfigurationException |
if error occurred parsing the arguments. |
parse
publicparse ( args)
Alternate parse(String) method that takes a ERROR(/List) of arguments
| Parameters | |
|---|---|
args |
|
| Returns | |
|---|---|
|
a ERROR(/List) of the positional arguments left over after processing all options. |
| Throws | |
|---|---|
ConfigurationException |
if error occurred parsing the arguments. |
parseBestEffort
publicparseBestEffort ( args)
Alternate parseBestEffort(String) method that takes a ERROR(/List) of
arguments
| Parameters | |
|---|---|
args |
|
| Returns | |
|---|---|
|
a ERROR(/List) of the left over arguments |
parseBestEffort
publicparseBestEffort ( args, boolean forceContinue)
Alternate parseBestEffort(String) method that takes a ERROR(/List) of
arguments, and can be forced to continue parsing until the end, even if some args do not
parse.
| Parameters | |
|---|---|
args |
: list that will contain the left over args. |
forceContinue |
boolean: True if it should continue to parse even if some args do not parse. |
| Returns | |
|---|---|
|
a ERROR(/List) of the left over arguments |
parseBestEffort
publicparseBestEffort (String... args)
A best-effort version of parse(String). If a ConfigurationException is
thrown, that exception is captured internally, and the remaining arguments (including the
argument which caused the exception to be thrown) are returned. This method does not throw.
| Parameters | |
|---|---|
args |
String |
| Returns | |
|---|---|
|
a ERROR(/List) of the left over arguments |
validateMandatoryOptions
public void validateMandatoryOptions ()
Validates that all fields marked as mandatory have been set.
| Throws | |
|---|---|
|
com.android.tradefed.config.ConfigurationException |
ConfigurationException |
|