|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
ObjectOptions
public class Options
The Options class parses command-line options and sets fields in your
program accordingly. Each field that is annotated with @Option is set from a command-line argument of the same name.
The Options class can also create usage messages and HTML documentation.
The Options class interprets annotations and Javadoc comments, so that
you do not have to write duplicative, boilerplate code and
documentation that could get out of sync with the rest of your program.
The main entry point is parse_or_usage(String[]).
Typical use in your program is:
public static void main(String[] args) {
MyProgram myInstance = new MyProgram();
Options options = new Options("MyProgram [options] infile outfile",
myInstance, MyUtilityClass.class);
// Sets fields in object myInstance, and sets static fields in
// class MyUtilityClass.
// Returns the original command line, with all options removed.
String[] file_args = options.parse_or_usage (args);
...
The @Option annotation on a field specifies user documentation
and, optionally, a one-character short name that users may supply on the
command line. The long name is taken from the name of the variable;
when the name contains an underscore, the user may substitute a hyphen on
the command line instead.
On the command line, the values for options are specified in the form
'--longname=value', '-shortname=value', '--longname value', or '-shortname
value'. If use_single_dash(boolean) is true, then the long names
take the form '-longname=value' or '-longname value'. The value is
mandatory for all options except booleans. Booleans are set to true if no
value is specified.
All arguments that start with '-' are processed as options. To
terminate option processing at the first non-option argument, see parse_options_after_arg(boolean). Also, the special option '--'
terminates option processing; all subsequent arguments are passed to the
program (along with any preceding non-option arguments) without being
scanned for options.
A user may provide an option multiple times on the command line. If the field is a list, each entry is be added to the list. If the field is not a list, then only the last occurrence is used (subsequent occurrences overwrite the previous value).
Unpublicized options
The @Unpublicized annotation causes an option not to be displayed
in the usage message. This can be useful for options that are
preliminary, experimental, or for internal purposes only. The @Unpublicized annotation must be specified in addition to the @Option annotation.
Option groups
The @OptionGroup annotation can be used to assign a name to a set of
related options. This is useful for providing organization when working with
many options. Options in the same group are displayed under the same heading
in usage texts. Option groups themselves can be unpublicized causing the
set of options belonging to the group to not be displayed in the default
usage message.
The @OptionGroup annotation must be specified on a field in addition
to an @Option annotation. The @OptionGroup annotation
acts like a delimiterall @Option-annotated fields up to
the next @OptionGroup annotation belong to the same group.
When using option groups, the first @Option-annotated field of
every class and object passed to the Options(String, Object...)
constructor must have an @OptionGroup annotation. Furthermore,
the first parameter of an @OptionGroup annotation (the group
name) must be unique among all classes and objects passed to the Options(String, Object...) constructor.
When an @Unpublicized annotation is used on a field that is in an
unpublicized option group, that field is excluded in all usage
messages, even when passing the group's name explicitly as a parameter to
usage(String...).
Option aliases
The @Option annotation has an optional parameter aliases
which accepts an array of strings. Each string in the array is an alias for
the option being defined and can be used in place of an option's long name
or short name. Aliases should start with a single dash or double dash. It
is the user's responsibility to ensure that aliases does not cause ambiguity
and do not collide with other options.
Note that parameters must be named when passing more than one parameter to an annotation, as in the following examples.
For option groups:
@OptionGroup(value="Debugging Options", unpublicized=true)
For option aliases:
@Option(value="-h Print the detailed help", aliases={"-help", "--help"})
Supported field types The field may be of the following types:
Example:
public static class Test {
@Option ("-o <filename> the output file ")
public static File outfile = new File("/tmp/foobar");
@Option ("-i ignore case")
public static boolean ignore_case;
@Option ("-t set the initial temperature")
public static double temperature = 75.0;
public static void main (String[] args) {
Options options = new Options ("Test [options] files", new Test());
String[] file_args = options.parse_or_usage (args);
}
}
For an example of this library being used in practice see Lookup. Limitations
Option,
OptionGroup,
Unpublicized| Nested Class Summary | |
|---|---|
static class |
Options.ArgException
Exceptions encountered during argument processing. |
| Field Summary | |
|---|---|
static boolean |
split_lists
|
String |
usage_synopsis
Synopsis of usage. |
| Constructor Summary | |
|---|---|
Options(Object... args)
Prepare for option processing. |
|
Options(String usage_synopsis,
Object... args)
Prepare for option processing. |
|
| Method Summary | |
|---|---|
String |
get_options_str()
Returns a string containing all of the options that were set and their arguments. |
void |
ignore_options_after_arg(boolean val)
Deprecated. Use parse_options_after_arg(boolean). |
void |
jdoc(RootDoc doc)
Entry point for creating HTML documentation. |
String[] |
parse_and_usage(String args)
Deprecated. Use parse_or_usage(String). |
String[] |
parse_and_usage(String[] args)
Deprecated. Use parse_or_usage(String[]). |
void |
parse_options_after_arg(boolean val)
If true, Options will parse arguments even after a non-option command-line argument. |
String[] |
parse_or_usage(String args)
Parses a command line and sets the options accordingly. |
String[] |
parse_or_usage(String[] args)
Parses a command line and sets the options accordingly. |
String[] |
parse(String args)
Parses a command line and sets the options accordingly. |
String[] |
parse(String[] args)
Parses a command line and sets the options accordingly. |
void |
print_usage()
Prints, to standard output, usage information. |
void |
print_usage(PrintStream ps)
Prints usage information. |
void |
print_usage(PrintStream ps,
String msg)
Prints a message followed by indented usage information. |
void |
print_usage(PrintStream ps,
String format,
Object... args)
Prints a message followed by usage information. |
void |
print_usage(String msg)
Prints, to standard output, a message followed by usage information. |
void |
print_usage(String format,
Object... args)
Prints, to standard output, a message followed by usage information. |
String |
settings()
Returns a string containing the current setting for each option, in a format that can be parsed by Options. |
String |
toString()
Returns a description of all of the known options. |
String |
usage(String... group_names)
Returns the String containing the usage message for command-line options. |
void |
use_single_dash(boolean val)
If true, long options (those derived from field names) will be parsed with a single dash prefix as in -longOption. |
| Methods inherited from class Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
public static boolean split_lists
public String usage_synopsis
This variable is public so that clients can reset it (useful for masquerading as another program, based on parsed options).
| Constructor Detail |
|---|
public Options(Object... args)
Option) must be
unique across all the arguments.
public Options(String usage_synopsis,
Object... args)
Option) must be
unique across all the arguments.
usage_synopsis - A synopsis of how to call your program| Method Detail |
|---|
public void parse_options_after_arg(boolean val)
@Deprecated public void ignore_options_after_arg(boolean val)
parse_options_after_arg(boolean).
public void use_single_dash(boolean val)
public String[] parse(String[] args)
throws Options.ArgException
Options.ArgException - if the command line contains unknown option or
misused options.
public String[] parse(String args)
throws Options.ArgException
Options.ArgException - if the command line contains unknown option or
misused options.parse(String[])public String[] parse_or_usage(String[] args)
parse(String[])public String[] parse_or_usage(String args)
parse_or_usage(String[])@Deprecated public String[] parse_and_usage(String[] args)
parse_or_usage(String[]).
@Deprecated public String[] parse_and_usage(String args)
parse_or_usage(String).
public void print_usage(PrintStream ps)
public void print_usage()
public void print_usage(PrintStream ps,
String msg)
public void print_usage(String msg)
public void print_usage(PrintStream ps,
String format,
Object... args)
public void print_usage(String format,
Object... args)
public String usage(String... group_names)
group_names - The list of option groups to include in the usage
message. If empty, will return usage for all option groups which are not
unpublicized. Or if option groups are not being used, will return usage
for all options which are not unpublicized.public void jdoc(RootDoc doc)
@Unpublicized options.
public String get_options_str()
settings()public String settings()
public String toString()
toString in class Object
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||