O - type of options beanpublic final class JCommanderImproved<O> extends Object
JCommander.
Example:
public static void main(final String[] args) {
// Create an instance for your custom Options type.
// This option type is a typical JCommander annotated beanclass.
// Note that this type must provide a zero argument constructor.
// If not the constructor of JCommanderImproved throws an exception.
final JCommanderImproved<Options> cliArgs = new JCommanderImproved<Options>("mytool", Options.class);
// Parse and gather the options.
final Options opts = cliArgs.gatherOptions(args);
// Print the helpmessage:
System.out.print(
cliArgs.helpMessage(
"[-v|--version] [-h|--help]",
"This is mytool.",
"$> mytool -v")
);
}
| Modifier and Type | Field and Description |
|---|---|
private static int |
AVAILABLE_WIDTH
Available width (in characters) for generated help string.
|
private static String |
DEFAULT_NEW_LINE
System dependent newline character.
|
private static int |
LEFT_COL_WIDTH
Default width of left column (in characters) for generated help string.
|
private com.beust.jcommander.JCommander |
optionsParser
Parses the command line options.
|
private Class<O> |
optionsType
Class for type of options model.
|
private String |
programName
The name of the tool for what this class gather options.
|
| Constructor and Description |
|---|
JCommanderImproved(String programName,
Class<O> optionsType)
Dedicated constructor.
|
| Modifier and Type | Method and Description |
|---|---|
private static <O> O |
createOptions(Class<O> optionsType)
Creates an option bean.
|
O |
gatherOptions(String[] args)
Parses the given arguments and return a ready set up options bean.
|
String |
helpMessage(String usage,
String descriptions,
String example)
Generates help message for main command.
|
(package private) static String |
lineBreak(String in,
int length,
int leftPadLength)
Breaks the given string to fit given length and pads on the left side before line breaks.
|
private void |
reset() |
(package private) static String |
rightPad(String in,
int length)
Pads given string with spaces on the right side to given length.
|
(package private) static String |
spaces(int length)
Generates a string of spaces with given length.
|
private static final int LEFT_COL_WIDTH
private static final int AVAILABLE_WIDTH
private static final String DEFAULT_NEW_LINE
private com.beust.jcommander.JCommander optionsParser
private final String programName
public JCommanderImproved(String programName, Class<O> optionsType)
Throws an IllegalArgumentException if the options type can't be instantiated.
programName - must not be null or emptyoptionsType - must not be nullpublic O gatherOptions(String[] args)
args - must not be nullnull always new instancepublic String helpMessage(String usage, String descriptions, String example)
The given parameters enhance the produced help messages:
usage: A usage string like e.g. -a --foo [-h|--help]descriptions: A more or less longer description of the tool.example: Examples how to use the tool.If one of the three parameters is empty the whole part is omitted in the output.
usage - must not be nulldescriptions - must not be nullexample - must not be nullnull or emptystatic String rightPad(String in, int length)
in - if null only spaces will be returnedlength - must not be negativenullstatic String spaces(int length)
length - empty string will be returned if < 1nullstatic String lineBreak(String in, int length, int leftPadLength)
in - empty string will be returned if nulllength - must not be negativeleftPadLength - must not be negativenullprivate void reset()
private static <O> O createOptions(Class<O> optionsType)
Throws an IllegalArgumentException if the options type can't be instantiated.
O - type of options beanoptionsType - must not be nullnull, always new instanceCopyright © 2014 Sven Strittmatter. All Rights Reserved.