public class GetOpt extends Object
-a) and long ( --freakin-long-option) options.-abqv), and if any options take parameters,
those will be read in sequence from the command line following the glommage.--) is recognized and handled.System.err.add...()processParams()get...Value()An example of the automatic usage statement (as rendered by a standard printStackTrace()):
com.bluecirclesoft.open.getopt.GetOpt.CommandLineOptionException:
error: Unknown option: -?
error: required option --output-dir was not given
usage:
com.bluecirclesoft.open.getopt.ClassGenerator.Output.JavaNoMiddle -d <directory> [
-D <type> ]
where:
-d, --output-dir <directory>:
(required) output the new classes/hierarchy to <directory>
-D, --date-type <type>:
the type to use for dates (default: javax.sql.Date)
at com.bluecirclesoft.open.getopt.GetOpt.GetOpt.processParams(GetOpt.java:459)
at com.bluecirclesoft.open.getopt.ClassGenerator.Output.JavaNoMiddle.setParams(JavaNoMiddle.java:80)
at com.bluecirclesoft.open.getopt.ClassGenerator.Generate.main(Generate.java:72)
And the code that generated it:
GetOpt opts = new GetOpt(getClass().getName());
opts.addParam('d', "output-dir", "directory",
"output the new classes/hierarchy to <directory>", true);
opts.addParam('D', "date-type", "type", "the type to use for dates
(default: "
+ m_DateType + ")", false);
opts.processParams(Options);
| Modifier and Type | Field and Description |
|---|---|
static CommandLineProcessingFlavors |
DEFAULT_FLAVOR |
| Modifier and Type | Method and Description |
|---|---|
OptionSpecification |
addFlag(String documentation,
Consumer<Boolean> onEncounter)
Add an option without an argument (a flag - true or false)
|
<T> void |
addLongOpt(OptionSpecification optionSpecification,
String opt) |
<T> OptionSpecification |
addParam(String paramMnemonic,
String documentation,
boolean required,
Class<T> type,
Consumer<T> onEncounter)
Add an option with argument (with a value to be passed in).
|
OptionSpecification |
addParam(String paramMnemonic,
String documentation,
boolean required,
Consumer<String> onEncounter)
Add an option with argument (with a value to be passed in).
|
<T> void |
addShortOpt(OptionSpecification optionSpecification,
Character opt) |
static GetOpt |
create(Class mainClass)
Constructor.
|
static GetOpt |
create(Class mainClass,
CommandLineProcessingFlavors flavor)
Constructor.
|
static GetOpt |
create(String programName)
Factory method.
|
static GetOpt |
create(String programName,
CommandLineProcessingFlavors flavor)
Factory method.
|
static GetOpt |
createFromReceptacle(Object receptacle,
Class mainClass) |
static GetOpt |
createFromReceptacle(Object receptacle,
Class mainClass,
CommandLineProcessingFlavors flavor) |
static GetOpt |
createFromReceptacle(Object receptacle,
String programName) |
static GetOpt |
createFromReceptacle(Object receptacle,
String programName,
CommandLineProcessingFlavors flavor) |
OptionSpecification |
getLongOpt(String str,
Collection<String> problems) |
Set<OptionSpecification> |
getOptions() |
OptionSpecification |
getShortOptProcessing(Character ch) |
boolean |
isFlagSet(char shortOpt) |
void |
usage(StringBuilder errStr)
Generate usage string.
|
public static final CommandLineProcessingFlavors DEFAULT_FLAVOR
public OptionSpecification getShortOptProcessing(Character ch)
public OptionSpecification getLongOpt(String str, Collection<String> problems)
public static GetOpt create(String programName)
programName - The name of the program (for the usage message)public static GetOpt create(Class mainClass)
mainClass - The class name of the program (for the usage message)public static GetOpt create(String programName, CommandLineProcessingFlavors flavor)
programName - The name of the program (for the usage message)public static GetOpt create(Class mainClass, CommandLineProcessingFlavors flavor)
mainClass - The class name of the program (for the usage message)public static GetOpt createFromReceptacle(Object receptacle, String programName)
public static GetOpt createFromReceptacle(Object receptacle, String programName, CommandLineProcessingFlavors flavor)
public static GetOpt createFromReceptacle(Object receptacle, Class mainClass, CommandLineProcessingFlavors flavor)
public OptionSpecification addFlag(String documentation, Consumer<Boolean> onEncounter)
documentation - Documentation to display when generating the usage message. May not be
null.IllegalArgumentException - if both ShortOpt and LongOpt are nullIllegalArgumentException - if Documentation is null or zero length (Work with me here!
I'm building a freakin' usage message for you!)IllegalArgumentException - if either ShortOpt or LongOpt has already been addedpublic <T> OptionSpecification addParam(String paramMnemonic, String documentation, boolean required, Class<T> type, Consumer<T> onEncounter)
paramMnemonic - A short name for the parameter. May not be null.documentation - Documentation to display when generating the usage message. May not be
null.required - Whether the option is required to be specified on the command linetype - the type that the argument should be converted toonEncounter - the function to invoke when the option is encounteredGetOptSetupException - if documentation is null or zero lengthGetOptSetupException - if paramMnemonic is null or zero lengthpublic OptionSpecification addParam(String paramMnemonic, String documentation, boolean required, Consumer<String> onEncounter)
paramMnemonic - A short name for the parameter. May not be null.documentation - Documentation to display when generating the usage message. May not be
null.required - Whether the option is required to be specified on the command lineonEncounter - the function to invoke when the option is encounteredGetOptSetupException - if documentation is null or zero lengthGetOptSetupException - if paramMnemonic is null or zero lengthpublic void usage(StringBuilder errStr)
errStr - the output string builderpublic <T> void addShortOpt(OptionSpecification optionSpecification, Character opt)
public <T> void addLongOpt(OptionSpecification optionSpecification, String opt)
public boolean isFlagSet(char shortOpt)
public Set<OptionSpecification> getOptions()
Copyright © 2015. All rights reserved.