public class HelpFormatter
extends java.lang.Object
Example:
Options options = new Options(); options.addOption(OptionBuilder.withLongOpt("file") .withDescription("The file to be processed") .hasArg() .withArgName("FILE") .isRequired() .create('f')); options.addOption(OptionBuilder.withLongOpt("version") .withDescription("Print the version of the application") .create('v')); options.addOption(OptionBuilder.withLongOpt("help").create('h')); String header = "Do something useful with an input file\n\n"; String footer = "\nPlease report issues at http://example.com/issues"; HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("myapp", header, options, footer, true);This produces the following output:
usage: myapp -f <FILE> [-h] [-v] Do something useful with an input file -f,--file <FILE> The file to be processed -h,--help -v,--version Print the version of the application Please report issues at http://example.com/issues
Modifier and Type | Field | Description |
---|---|---|
static int |
DEFAULT_DESC_PAD |
number of space characters to be prefixed to each description line
|
static int |
DEFAULT_LEFT_PAD |
Default padding to the left of each line
|
static int |
DEFAULT_WIDTH |
Default number of characters per line
|
protected java.util.Comparator<Option> |
optionComparator |
Comparator used to sort the options when they output in help text.
|
Constructor | Description |
---|---|
HelpFormatter(Console console) |
Creates a help formatter.
|
Modifier and Type | Method | Description |
---|---|---|
java.lang.String |
getArgName() |
Returns the 'argName'.
|
int |
getDescPadding() |
Returns the 'descPadding'.
|
int |
getLeftPadding() |
Returns the 'leftPadding'.
|
java.lang.String |
getLongOptPrefix() |
Returns the 'longOptPrefix'.
|
java.lang.String |
getLongOptSeparator() |
Returns the separator displayed between a long option and its value.
|
java.lang.String |
getNewLine() |
Returns the 'newLine'.
|
java.util.Comparator<Option> |
getOptionComparator() |
Comparator used to sort the options when they output in help text.
|
java.lang.String |
getOptPrefix() |
Returns the 'optPrefix'.
|
java.lang.String |
getSyntaxPrefix() |
Returns the 'syntaxPrefix'.
|
int |
getWidth() |
Returns the 'width'.
|
void |
printHelp(int width,
java.lang.String cmdLineSyntax,
java.lang.String header,
Options options,
int leftPad,
int descPad,
java.lang.String footer) |
Print the help for
options with the specified
command line syntax. |
void |
printHelp(int width,
java.lang.String cmdLineSyntax,
java.lang.String header,
Options options,
int leftPad,
int descPad,
java.lang.String footer,
boolean autoUsage) |
Print the help for
options with the specified
command line syntax. |
void |
printHelp(int width,
java.lang.String cmdLineSyntax,
java.lang.String header,
Options options,
java.lang.String footer) |
Print the help for
options with the specified
command line syntax. |
void |
printHelp(int width,
java.lang.String cmdLineSyntax,
java.lang.String header,
Options options,
java.lang.String footer,
boolean autoUsage) |
Print the help for
options with the specified
command line syntax. |
void |
printHelp(java.lang.String cmdLineSyntax,
Options options) |
Print the help for
options with the specified
command line syntax. |
void |
printHelp(java.lang.String cmdLineSyntax,
Options options,
boolean autoUsage) |
Print the help for
options with the specified
command line syntax. |
void |
printHelp(java.lang.String cmdLineSyntax,
java.lang.String header,
Options options,
java.lang.String footer) |
Print the help for
options with the specified
command line syntax. |
void |
printHelp(java.lang.String cmdLineSyntax,
java.lang.String header,
Options options,
java.lang.String footer,
boolean autoUsage) |
Print the help for
options with the specified
command line syntax. |
void |
printOptions(int width,
Options options,
int leftPad,
int descPad) |
Print the help for the specified Options to the specified writer,
using the specified width, left padding and description padding.
|
void |
printUsage(int width,
java.lang.String cmdLineSyntax) |
Print the cmdLineSyntax to the specified writer, using the
specified width.
|
void |
printUsage(int width,
java.lang.String commandName,
Options options) |
Prints the usage statement for the specified command.
|
void |
printWrapped(int width,
int nextLineTabStop,
java.lang.String text) |
Print the specified text to the specified PrintWriter.
|
void |
printWrapped(int width,
java.lang.String text) |
Print the specified text to the specified PrintWriter.
|
protected java.lang.StringBuilder |
renderOptions(java.lang.StringBuilder sb,
int width,
Options options,
int leftPad,
int descPad) |
Render the specified Options and return the rendered Options
in a StringBuilder.
|
protected java.lang.StringBuilder |
renderWrappedText(java.lang.StringBuilder sb,
int width,
int nextLineTabStop,
java.lang.String text) |
Render the specified text and return the rendered Options
in a StringBuilder.
|
void |
setArgName(java.lang.String name) |
Sets the 'argName'.
|
void |
setDescPadding(int padding) |
Sets the 'descPadding'.
|
void |
setLeftPadding(int padding) |
Sets the 'leftPadding'.
|
void |
setLongOptPrefix(java.lang.String prefix) |
Sets the 'longOptPrefix'.
|
void |
setLongOptSeparator(java.lang.String longOptSeparator) |
Set the separator displayed between a long option and its value.
|
void |
setNewLine(java.lang.String newline) |
Sets the 'newLine'.
|
void |
setOptionComparator(java.util.Comparator<Option> comparator) |
Set the comparator used to sort the options when they output in help text.
|
void |
setOptPrefix(java.lang.String prefix) |
Sets the 'optPrefix'.
|
void |
setSyntaxPrefix(java.lang.String prefix) |
Sets the 'syntaxPrefix'.
|
void |
setWidth(int width) |
Sets the 'width'.
|
public static final int DEFAULT_WIDTH
public static final int DEFAULT_LEFT_PAD
public static final int DEFAULT_DESC_PAD
protected java.util.Comparator<Option> optionComparator
public HelpFormatter(Console console)
console
- the console to which the help will be writtenpublic void setWidth(int width)
width
- the new value of 'width'public int getWidth()
public void setLeftPadding(int padding)
padding
- the new value of 'leftPadding'public int getLeftPadding()
public void setDescPadding(int padding)
padding
- the new value of 'descPadding'public int getDescPadding()
public void setSyntaxPrefix(java.lang.String prefix)
prefix
- the new value of 'syntaxPrefix'public java.lang.String getSyntaxPrefix()
public void setNewLine(java.lang.String newline)
newline
- the new value of 'newLine'public java.lang.String getNewLine()
public void setOptPrefix(java.lang.String prefix)
prefix
- the new value of 'optPrefix'public java.lang.String getOptPrefix()
public void setLongOptPrefix(java.lang.String prefix)
prefix
- the new value of 'longOptPrefix'public java.lang.String getLongOptPrefix()
public void setLongOptSeparator(java.lang.String longOptSeparator)
longOptSeparator
- the separator, typically ' ' or '='.public java.lang.String getLongOptSeparator()
public void setArgName(java.lang.String name)
name
- the new value of 'argName'public java.lang.String getArgName()
public java.util.Comparator<Option> getOptionComparator()
Comparator
currently in use to sort the optionspublic void setOptionComparator(java.util.Comparator<Option> comparator)
comparator
- the Comparator
to use for sorting the optionspublic void printHelp(java.lang.String cmdLineSyntax, Options options)
options
with the specified
command line syntax. This method prints help information to
System.out.cmdLineSyntax
- the syntax for this applicationoptions
- the Options instancepublic void printHelp(java.lang.String cmdLineSyntax, Options options, boolean autoUsage)
options
with the specified
command line syntax. This method prints help information to
System.out.cmdLineSyntax
- the syntax for this applicationoptions
- the Options instanceautoUsage
- whether to print an automatically generated usage statementpublic void printHelp(java.lang.String cmdLineSyntax, java.lang.String header, Options options, java.lang.String footer)
options
with the specified
command line syntax. This method prints help information to
System.out.cmdLineSyntax
- the syntax for this applicationheader
- the banner to display at the beginning of the helpoptions
- the Options instancefooter
- the banner to display at the end of the helppublic void printHelp(java.lang.String cmdLineSyntax, java.lang.String header, Options options, java.lang.String footer, boolean autoUsage)
options
with the specified
command line syntax. This method prints help information to
System.out.cmdLineSyntax
- the syntax for this applicationheader
- the banner to display at the beginning of the helpoptions
- the Options instancefooter
- the banner to display at the end of the helpautoUsage
- whether to print an automatically generated usage statementpublic void printHelp(int width, java.lang.String cmdLineSyntax, java.lang.String header, Options options, java.lang.String footer)
options
with the specified
command line syntax. This method prints help information to
System.out.width
- the number of characters to be displayed on each linecmdLineSyntax
- the syntax for this applicationheader
- the banner to display at the beginning of the helpoptions
- the Options instancefooter
- the banner to display at the end of the helppublic void printHelp(int width, java.lang.String cmdLineSyntax, java.lang.String header, Options options, java.lang.String footer, boolean autoUsage)
options
with the specified
command line syntax. This method prints help information to
System.out.width
- the number of characters to be displayed on each linecmdLineSyntax
- the syntax for this applicationheader
- the banner to display at the beginning of the helpoptions
- the Options instancefooter
- the banner to display at the end of the helpautoUsage
- whether to print an automatically generated usage statementpublic void printHelp(int width, java.lang.String cmdLineSyntax, java.lang.String header, Options options, int leftPad, int descPad, java.lang.String footer)
options
with the specified
command line syntax.width
- the number of characters to be displayed on each linecmdLineSyntax
- the syntax for this applicationheader
- the banner to display at the beginning of the helpoptions
- the Options instanceleftPad
- the number of characters of padding to be prefixed to each linedescPad
- the number of characters of padding to be prefixed to each description linefooter
- the banner to display at the end of the helpjava.lang.IllegalStateException
- if there is no room to print a linepublic void printHelp(int width, java.lang.String cmdLineSyntax, java.lang.String header, Options options, int leftPad, int descPad, java.lang.String footer, boolean autoUsage)
options
with the specified
command line syntax.width
- the number of characters to be displayed on each linecmdLineSyntax
- the syntax for this applicationheader
- the banner to display at the beginning of the helpoptions
- the Options instanceleftPad
- the number of characters of padding to be prefixed to each linedescPad
- the number of characters of padding to be prefixed to each description linefooter
- the banner to display at the end of the helpautoUsage
- whether to print an automatically generated usage statementjava.lang.IllegalStateException
- if there is no room to print a linepublic void printUsage(int width, java.lang.String commandName, Options options)
width
- the number of characters to display per linecommandName
- the command nameoptions
- the command line Optionspublic void printUsage(int width, java.lang.String cmdLineSyntax)
width
- the number of characters per line for the usage statementcmdLineSyntax
- the usage statementpublic void printOptions(int width, Options options, int leftPad, int descPad)
width
- the number of characters to display per lineoptions
- the command line OptionsleftPad
- the number of characters of padding to be prefixed to each linedescPad
- the number of characters of padding to be prefixed to each description linepublic void printWrapped(int width, java.lang.String text)
width
- the number of characters to display per linetext
- the text to be written to the PrintWriterpublic void printWrapped(int width, int nextLineTabStop, java.lang.String text)
width
- the number of characters to display per linenextLineTabStop
- the position on the next line for the first tabtext
- the text to be written to the PrintWriterprotected java.lang.StringBuilder renderOptions(java.lang.StringBuilder sb, int width, Options options, int leftPad, int descPad)
sb
- the StringBuilder to place the rendered Options intowidth
- the number of characters to display per lineoptions
- the command line OptionsleftPad
- the number of characters of padding to be prefixed to each linedescPad
- the number of characters of padding to be prefixed to each description lineprotected java.lang.StringBuilder renderWrappedText(java.lang.StringBuilder sb, int width, int nextLineTabStop, java.lang.String text)
sb
- the StringBuilder to place the rendered text intowidth
- the number of characters to display per linenextLineTabStop
- the position on the next line for the first tabtext
- the text to be renderedCopyright © 2008–2018 The Aspectran Project. All rights reserved.