|
|||||||||
PREV PACKAGE NEXT PACKAGE | FRAMES NO FRAMES |
See:
Description
Interface Summary | |
---|---|
GetoptErrorFormatter | Formatter for Getopt error messages. |
Class Summary | |
---|---|
BSDGetoptErrorFormatter | Error formatter designed to mimic the output of getopt on a BSD system. |
CommandLineOption | Class representing a command-line option. |
DarwinGetoptErrorFormatter | Error formatter designed to mimic the output of getopt on a Darwin (Mac OS) system. |
GNUGetoptErrorFormatter | Error formatter designed to mimic the output of getopt from the GNU C Library. |
OptionArgumentPair | Class representing a command-line option and its argument (if any). |
POSIXGetoptErrorFormatter | Error formatter designed to mimic the output of getopt as described in POSIX (via the Single Unix Specification). |
UltraGetopt | Main class of the UltraGetopt system. |
Enum Summary | |
---|---|
ArgumentDisposition | Argument requirement status for options. |
UltraGetopt.Behavior | Different behavior options for UltraGetopt. |
Exception Summary | |
---|---|
AmbiguousOptionException | Exception signaling that an option could be matched against multiple accepted options. |
CommandLineException | Superclass for all exceptions resulting from parsing of the command-line. |
ExtraArgumentException | Exception signaling that an option which does not take an argument was given an argument. |
InvalidOptionException | Exception signaling that an option was determined to be invalid by the
optionEncountered method. |
MissingArgumentException | Exception signaling that an option which requires an argument was not given any arguments. |
UnrecognizedOptionException | Exception signaling that an option encountered on the command-line was not recognized (not listed in the set of accepted options). |
Provides classes for parsing and processing command-line options including compatibility with many getopt implementations in addition to a substantial amount of customizability. This is the Java imagining of UltraGetopt, a versatile getopt replacement written in C.
This class is designed to provide command-line parsing facilities which follow the expected behavior for command-line option syntax as well as to provide an interface which allows program authors to customize its behavior to fit the specific needs of their program. By default UltraGetopt will attempt to guess the conventions for the operating system on which it is being run and tune its behavior accordingly. However, program authors are also able to specify any set of behaviors that they desire and UltraGetopt will honor those behavior on any system on which it is run. In addition, UltraGetopt should be easy to use and flexible enough to work with almost any set of requirements. For any suggestions about how to improve this package to better achieve these goals, please contact the author.
vim --noplugin -i ~/.viminfo -d file1 file2 |
import java.util.List;
import java.util.Map;
import name.kevinlocke.ultragetopt.ArgumentDisposition;
import name.kevinlocke.ultragetopt.CommandLineException;
import name.kevinlocke.ultragetopt.CommandLineOption;
import name.kevinlocke.ultragetopt.UltraGetopt;
public class ReunitePangea {
private static CommandLineOption[] acceptedopts = {
new CommandLineOption('f', "force", ArgumentDisposition.NO_ARGUMENT),
new CommandLineOption('p', "progress", ArgumentDisposition.OPTIONAL_ARGUMENT),
new CommandLineOption('w', "when", ArgumentDisposition.REQUIRED_ARGUMENT),
};
public static void main(String[] args) {
UltraGetopt getopt = null;
try {
getopt = new UltraGetopt(args, acceptedopts);
} catch (CommandLineException e) {
System.err.println(e.getMessage());
System.exit(1);
}
Map<String,String> options = getopt.getOptionsByName();
List<String> nonoptargs = getopt.getNonOptionArguments();
if (options.containsKey("force"))
// Apply extra force
if (options.containsKey("progress")) {
if (options.get("progress") != null)
// Send progress reports to file
else
// Send progress reports to stdout
}
if (options.containsKey("when"))
// Wait for the appropriate time
for (String government : nonoptargs)
// Contact government to let them know what's happening
// Move continents into place
}
}
CommandLineOption
class to
add custom functionality. One example would be to provide self-documenting
options. Consider the following program:
import java.util.Map;
import name.kevinlocke.ultragetopt.ArgumentDisposition;
import name.kevinlocke.ultragetopt.CommandLineException;
import name.kevinlocke.ultragetopt.CommandLineOption;
import name.kevinlocke.ultragetopt.UltraGetopt;
public class JettisonEngine {
private static class DocumentedOption extends CommandLineOption {
private String argname;
private String helpmsg;
public DocumentedOption(char shortopt, String longopt,
ArgumentDisposition arg, String argname, String helpmsg) {
super(shortopt, longopt, arg);
this.argname = argname;
this.helpmsg = helpmsg;
}
public void printHelp() {
String longandarg = longopt;
if (argreq == ArgumentDisposition.OPTIONAL_ARGUMENT)
longandarg += "[="+argname+"]";
else if (argreq == ArgumentDisposition.REQUIRED_ARGUMENT)
longandarg += " <"+argname+">";
String[] lines = helpmsg.split("\n");
System.out.printf("%4s,%-20s%s%n",
UltraGetopt.getDefaultShortLeaders()[0]+shortopt,
UltraGetopt.getDefaultLongLeaders()[0]+longandarg,
lines[0]);
for (int i=1; i<lines.length; i++)
System.out.printf("%25s%s%n", " ", lines[i]);
}
}
private static DocumentedOption[] acceptedopts = {
new DocumentedOption('a', "all", ArgumentDisposition.NO_ARGUMENT,
"", "Jettison all engines"),
new DocumentedOption('e', "engine", ArgumentDisposition.REQUIRED_ARGUMENT,
"number", "Specify which engine to jettison"),
new DocumentedOption('h', "help", ArgumentDisposition.NO_ARGUMENT,
"", "Print this help message (you may need more)"),
new DocumentedOption('m', "message", ArgumentDisposition.REQUIRED_ARGUMENT,
"msg", "Message to read to the crew"),
};
public static void main(String[] args) {
UltraGetopt getopt = null;
try {
getopt = new UltraGetopt(args, acceptedopts);
} catch (CommandLineException e) {
System.err.println(e.getMessage());
System.err.println("Run `JettisonEngine -h' for usage information");
System.exit(1);
}
Map<String,String> options = getopt.getOptionsByName();
if (options.containsKey("help")) {
System.out.println("Usage: JettisonEngine [options]");
System.out.println("Jettison one or more of the engines.\n");
System.out.println("JettisonEngine supports the following options:");
for (DocumentedOption option : acceptedopts)
option.printHelp();
return;
}
// Jettison the engine(s)
}
}
CommandLineOption
class to implement the
optionEncountered
callback mechanism. This function will be
called as each option is encountered on the command line when the options
are parsed in the UltraGetopt
constructor.
import java.util.Map;
import name.kevinlocke.ultragetopt.ArgumentDisposition;
import name.kevinlocke.ultragetopt.CommandLineException;
import name.kevinlocke.ultragetopt.CommandLineOption;
import name.kevinlocke.ultragetopt.UltraGetopt;
public class TimeMachine {
private static class ActionOption extends CommandLineOption {
private String argname;
private String helpmsg;
public ActionOption(char shortopt, String longopt,
ArgumentDisposition arg, String argname, String helpmsg) {
super(shortopt, longopt, arg);
this.argname = argname;
this.helpmsg = helpmsg;
}
public void printHelp() {
String longandarg = longopt;
if (argreq == ArgumentDisposition.OPTIONAL_ARGUMENT)
longandarg += "[="+argname+"]";
else if (argreq == ArgumentDisposition.REQUIRED_ARGUMENT)
longandarg += " <"+argname+">";
String[] lines = helpmsg.split("\n");
System.out.printf("%4s,%-20s%s%n",
UltraGetopt.getDefaultShortLeaders()[0]+shortopt,
UltraGetopt.getDefaultLongLeaders()[0]+longandarg,
lines[0]);
for (int i=1; i<lines.length; i++)
System.out.printf("%25s%s%n", " ", lines[i]);
}
@Override
public boolean optionEncountered(String argument) {
switch (shortopt) {
case 'h':
// Stop processing right here and print a help message
return false;
case 'l':
// Pause for given timespan (or 60 minutes)
break;
case 'r':
// Revert the specified temporal paradox (or the most recent)
break;
case 't':
// Travel to the given time
break;
default:
// Not an option that we are processing here
break;
}
return true;
}
}
private static ActionOption[] acceptedopts = {
new ActionOption('h', "help", ArgumentDisposition.NO_ARGUMENT,
"", "Print this help message"),
new ActionOption('l', "layover", ArgumentDisposition.OPTIONAL_ARGUMENT,
"mins", "Pause for sightseeing (default: 60)"),
new ActionOption('r', "revert", ArgumentDisposition.OPTIONAL_ARGUMENT,
"which", "Revert temporal paradox\n" +
"(default: most recent, relatively speaking)"),
new ActionOption('t', "time", ArgumentDisposition.REQUIRED_ARGUMENT,
"date", "Specify a destination in time"),
};
public static void main(String[] args) {
UltraGetopt getopt = null;
try {
getopt = new UltraGetopt(args, acceptedopts);
} catch (CommandLineException e) {
System.err.println(e.getMessage());
System.err.println("Run `TimeMachine -h' for usage information");
System.exit(1);
}
Map<String,String> options = getopt.getOptionsByName();
if (options.containsKey("help")) {
System.out.println("Usage: TimeMachine <sequence>");
System.out.println("Travel through time.");
System.out.println("WARNING: May not function at less than 88mph.\n");
System.out.println("TimeMachine supports the following options:");
for (ActionOption option : acceptedopts)
option.printHelp();
return;
}
// We hope you have enjoyed your trip
}
}
EnumSet
Map
Properties
|
|||||||||
PREV PACKAGE NEXT PACKAGE | FRAMES NO FRAMES |