public class HelpFormatter extends 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 and Description |
---|---|
static String |
DEFAULT_ARG_NAME
default name for an argument
|
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 String |
DEFAULT_LONG_OPT_PREFIX
default prefix for long Option
|
static String |
DEFAULT_LONG_OPT_SEPARATOR
default separator displayed between a long Option and its value
|
static String |
DEFAULT_OPT_PREFIX
default prefix for shortOpts
|
static String |
DEFAULT_SYNTAX_PREFIX
the string to display at the beginning of the usage statement
|
static int |
DEFAULT_WIDTH
default number of characters per line
|
String |
defaultArgName
Deprecated.
Scope will be made private for next major version
- use get/setArgName methods instead.
|
int |
defaultDescPad
Deprecated.
Scope will be made private for next major version
- use get/setDescPadding methods instead.
|
int |
defaultLeftPad
Deprecated.
Scope will be made private for next major version
- use get/setLeftPadding methods instead.
|
String |
defaultLongOptPrefix
Deprecated.
Scope will be made private for next major version
- use get/setLongOptPrefix methods instead.
|
String |
defaultNewLine
Deprecated.
Scope will be made private for next major version
- use get/setNewLine methods instead.
|
String |
defaultOptPrefix
Deprecated.
Scope will be made private for next major version
- use get/setOptPrefix methods instead.
|
String |
defaultSyntaxPrefix
Deprecated.
Scope will be made private for next major version
- use get/setSyntaxPrefix methods instead.
|
int |
defaultWidth
Deprecated.
Scope will be made private for next major version
- use get/setWidth methods instead.
|
protected Comparator<Option> |
optionComparator
Comparator used to sort the options when they output in help text
Defaults to case-insensitive alphabetical sorting by option key
|
Constructor and Description |
---|
HelpFormatter() |
Modifier and Type | Method and Description |
---|---|
protected String |
createPadding(int len)
Return a String of padding of length
len . |
protected int |
findWrapPos(String text,
int width,
int startPos)
Finds the next text wrap position after
startPos for the
text in text with the column width width . |
String |
getArgName()
Returns the 'argName'.
|
int |
getDescPadding()
Returns the 'descPadding'.
|
int |
getLeftPadding()
Returns the 'leftPadding'.
|
String |
getLongOptPrefix()
Returns the 'longOptPrefix'.
|
String |
getLongOptSeparator()
Returns the separator displayed between a long option and its value.
|
String |
getNewLine()
Returns the 'newLine'.
|
Comparator<Option> |
getOptionComparator()
Comparator used to sort the options when they output in help text.
|
String |
getOptPrefix()
Returns the 'optPrefix'.
|
String |
getSyntaxPrefix()
Returns the 'syntaxPrefix'.
|
int |
getWidth()
Returns the 'width'.
|
void |
printHelp(int width,
String cmdLineSyntax,
String header,
Options options,
String footer)
Print the help for
options with the specified
command line syntax. |
void |
printHelp(int width,
String cmdLineSyntax,
String header,
Options options,
String footer,
boolean autoUsage)
Print the help for
options with the specified
command line syntax. |
void |
printHelp(PrintWriter pw,
int width,
String cmdLineSyntax,
String header,
Options options,
int leftPad,
int descPad,
String footer)
Print the help for
options with the specified
command line syntax. |
void |
printHelp(PrintWriter pw,
int width,
String cmdLineSyntax,
String header,
Options options,
int leftPad,
int descPad,
String footer,
boolean autoUsage)
Print the help for
options with the specified
command line syntax. |
void |
printHelp(String cmdLineSyntax,
Options options)
Print the help for
options with the specified
command line syntax. |
void |
printHelp(String cmdLineSyntax,
Options options,
boolean autoUsage)
Print the help for
options with the specified
command line syntax. |
void |
printHelp(String cmdLineSyntax,
String header,
Options options,
String footer)
Print the help for
options with the specified
command line syntax. |
void |
printHelp(String cmdLineSyntax,
String header,
Options options,
String footer,
boolean autoUsage)
Print the help for
options with the specified
command line syntax. |
void |
printOptions(PrintWriter pw,
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(PrintWriter pw,
int width,
String cmdLineSyntax)
Print the cmdLineSyntax to the specified writer, using the
specified width.
|
void |
printUsage(PrintWriter pw,
int width,
String app,
Options options)
Prints the usage statement for the specified application.
|
void |
printWrapped(PrintWriter pw,
int width,
int nextLineTabStop,
String text)
Print the specified text to the specified PrintWriter.
|
void |
printWrapped(PrintWriter pw,
int width,
String text)
Print the specified text to the specified PrintWriter.
|
protected StringBuffer |
renderOptions(StringBuffer sb,
int width,
Options options,
int leftPad,
int descPad)
Render the specified Options and return the rendered Options
in a StringBuffer.
|
protected StringBuffer |
renderWrappedText(StringBuffer sb,
int width,
int nextLineTabStop,
String text)
Render the specified text and return the rendered Options
in a StringBuffer.
|
protected String |
rtrim(String s)
Remove the trailing whitespace from the specified String.
|
void |
setArgName(String name)
Sets the 'argName'.
|
void |
setDescPadding(int padding)
Sets the 'descPadding'.
|
void |
setLeftPadding(int padding)
Sets the 'leftPadding'.
|
void |
setLongOptPrefix(String prefix)
Sets the 'longOptPrefix'.
|
void |
setLongOptSeparator(String longOptSeparator)
Set the separator displayed between a long option and its value.
|
void |
setNewLine(String newline)
Sets the 'newLine'.
|
void |
setOptionComparator(Comparator<Option> comparator)
Set the comparator used to sort the options when they output in help text.
|
void |
setOptPrefix(String prefix)
Sets the 'optPrefix'.
|
void |
setSyntaxPrefix(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
public static final String DEFAULT_SYNTAX_PREFIX
public static final String DEFAULT_OPT_PREFIX
public static final String DEFAULT_LONG_OPT_PREFIX
public static final String DEFAULT_LONG_OPT_SEPARATOR
public static final String DEFAULT_ARG_NAME
@Deprecated public int defaultWidth
@Deprecated public int defaultLeftPad
@Deprecated public int defaultDescPad
@Deprecated public String defaultSyntaxPrefix
@Deprecated public String defaultNewLine
@Deprecated public String defaultOptPrefix
@Deprecated public String defaultLongOptPrefix
@Deprecated public String defaultArgName
protected Comparator<Option> optionComparator
public HelpFormatter()
public 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(String prefix)
prefix
- the new value of 'syntaxPrefix'public String getSyntaxPrefix()
public void setNewLine(String newline)
newline
- the new value of 'newLine'public String getNewLine()
public void setOptPrefix(String prefix)
prefix
- the new value of 'optPrefix'public String getOptPrefix()
public void setLongOptPrefix(String prefix)
prefix
- the new value of 'longOptPrefix'public String getLongOptPrefix()
public void setLongOptSeparator(String longOptSeparator)
longOptSeparator
- the separator, typically ' ' or '='.public String getLongOptSeparator()
public void setArgName(String name)
name
- the new value of 'argName'public String getArgName()
public Comparator<Option> getOptionComparator()
Comparator
currently in use to sort the optionspublic void setOptionComparator(Comparator<Option> comparator)
comparator
- the Comparator
to use for sorting the optionspublic void printHelp(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(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(String cmdLineSyntax, String header, Options options, 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(String cmdLineSyntax, String header, Options options, 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, String cmdLineSyntax, String header, Options options, 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, String cmdLineSyntax, String header, Options options, 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(PrintWriter pw, int width, String cmdLineSyntax, String header, Options options, int leftPad, int descPad, String footer)
options
with the specified
command line syntax.pw
- the writer to which the help will be writtenwidth
- 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 helpIllegalStateException
- if there is no room to print a linepublic void printHelp(PrintWriter pw, int width, String cmdLineSyntax, String header, Options options, int leftPad, int descPad, String footer, boolean autoUsage)
options
with the specified
command line syntax.pw
- the writer to which the help will be writtenwidth
- 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 statementIllegalStateException
- if there is no room to print a linepublic void printUsage(PrintWriter pw, int width, String app, Options options)
pw
- The PrintWriter to print the usage statementwidth
- The number of characters to display per lineapp
- The application nameoptions
- The command line Optionspublic void printUsage(PrintWriter pw, int width, String cmdLineSyntax)
pw
- The printWriter to write the help towidth
- The number of characters per line for the usage statement.cmdLineSyntax
- The usage statement.public void printOptions(PrintWriter pw, int width, Options options, int leftPad, int descPad)
pw
- The printWriter to write the help towidth
- 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(PrintWriter pw, int width, String text)
pw
- The printWriter to write the help towidth
- The number of characters to display per linetext
- The text to be written to the PrintWriterpublic void printWrapped(PrintWriter pw, int width, int nextLineTabStop, String text)
pw
- The printWriter to write the help towidth
- The number of characters to display per linenextLineTabStop
- The position on the next line for the first tab.text
- The text to be written to the PrintWriterprotected StringBuffer renderOptions(StringBuffer sb, int width, Options options, int leftPad, int descPad)
sb
- The StringBuffer to place the rendered Options into.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 lineprotected StringBuffer renderWrappedText(StringBuffer sb, int width, int nextLineTabStop, String text)
sb
- The StringBuffer to place the rendered text into.width
- The number of characters to display per linenextLineTabStop
- The position on the next line for the first tab.text
- The text to be rendered.protected int findWrapPos(String text, int width, int startPos)
startPos
for the
text in text
with the column width width
.
The wrap point is the last position before startPos+width having a
whitespace character (space, \n, \r). If there is no whitespace character
before startPos+width, it will return startPos+width.text
- The text being searched for the wrap positionwidth
- width of the wrapped textstartPos
- position from which to start the lookup whitespace
characterprotected String createPadding(int len)
len
.len
- The length of the String of padding to create.Copyright © 2002–2015 The Apache Software Foundation. All rights reserved.