HelpFormatter.@Deprecated 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 https://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 https://example.com/issues
| Modifier and Type | Class | Description |
|---|---|---|
static class |
HelpFormatter.Builder |
Deprecated.
Builds
HelpFormatter. |
| Modifier and Type | Field | Description |
|---|---|---|
static String |
DEFAULT_ARG_NAME |
Deprecated.
Default name for an argument
|
static int |
DEFAULT_DESC_PAD |
Deprecated.
Number of space characters to be prefixed to each description line
|
static int |
DEFAULT_LEFT_PAD |
Deprecated.
Default padding to the left of each line
|
static String |
DEFAULT_LONG_OPT_PREFIX |
Deprecated.
Default prefix for long Option
|
static String |
DEFAULT_LONG_OPT_SEPARATOR |
Deprecated.
Default separator displayed between a long Option and its value
|
static String |
DEFAULT_OPT_PREFIX |
Deprecated.
Default prefix for shortOpts
|
static String |
DEFAULT_SYNTAX_PREFIX |
Deprecated.
The string to display at the beginning of the usage statement
|
static int |
DEFAULT_WIDTH |
Deprecated.
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 |
Deprecated.
Comparator used to sort the options when they output in help text
Defaults to case-insensitive alphabetical sorting by option key
|
| Constructor | Description |
|---|---|
HelpFormatter() |
Deprecated.
Constructs a new instance.
|
| Modifier and Type | Method | Description |
|---|---|---|
static HelpFormatter.Builder |
builder() |
Deprecated.
Creates a new builder.
|
protected String |
createPadding(int len) |
Deprecated.
Creates a String of padding of length
len. |
protected int |
findWrapPos(String text,
int width,
int startPos) |
Deprecated.
Finds the next text wrap position after
startPos for the text in text with the column width
width. |
String |
getArgName() |
Deprecated.
Gets the 'argName'.
|
int |
getDescPadding() |
Deprecated.
Gets the 'descPadding'.
|
static String |
getDescription(Option option) |
Deprecated.
Gets the option description or an empty string if the description is
null. |
int |
getLeftPadding() |
Deprecated.
Gets the 'leftPadding'.
|
String |
getLongOptPrefix() |
Deprecated.
Gets the 'longOptPrefix'.
|
String |
getLongOptSeparator() |
Deprecated.
Gets the separator displayed between a long option and its value.
|
String |
getNewLine() |
Deprecated.
Gets the 'newLine'.
|
Comparator<Option> |
getOptionComparator() |
Deprecated.
Comparator used to sort the options when they output in help text.
|
String |
getOptPrefix() |
Deprecated.
Gets the 'optPrefix'.
|
String |
getSyntaxPrefix() |
Deprecated.
Gets the 'syntaxPrefix'.
|
int |
getWidth() |
Deprecated.
Gets the 'width'.
|
void |
printHelp(int width,
String cmdLineSyntax,
String header,
Options options,
String footer) |
Deprecated.
Prints the help for
options with the specified command line syntax. |
void |
printHelp(int width,
String cmdLineSyntax,
String header,
Options options,
String footer,
boolean autoUsage) |
Deprecated.
Prints 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) |
Deprecated.
Prints 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) |
Deprecated.
Prints the help for
options with the specified command line syntax. |
void |
printHelp(String cmdLineSyntax,
Options options) |
Deprecated.
Prints the help for
options with the specified command line syntax. |
void |
printHelp(String cmdLineSyntax,
Options options,
boolean autoUsage) |
Deprecated.
Prints the help for
options with the specified command line syntax. |
void |
printHelp(String cmdLineSyntax,
String header,
Options options,
String footer) |
Deprecated.
Prints the help for
options with the specified command line syntax. |
void |
printHelp(String cmdLineSyntax,
String header,
Options options,
String footer,
boolean autoUsage) |
Deprecated.
Prints the help for
options with the specified command line syntax. |
void |
printOptions(PrintWriter pw,
int width,
Options options,
int leftPad,
int descPad) |
Deprecated.
Prints 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) |
Deprecated.
Prints the cmdLineSyntax to the specified writer, using the specified width.
|
void |
printUsage(PrintWriter pw,
int width,
String app,
Options options) |
Deprecated.
Prints the usage statement for the specified application.
|
void |
printWrapped(PrintWriter pw,
int width,
int nextLineTabStop,
String text) |
Deprecated.
Prints the specified text to the specified PrintWriter.
|
void |
printWrapped(PrintWriter pw,
int width,
String text) |
Deprecated.
Prints the specified text to the specified PrintWriter.
|
protected StringBuffer |
renderOptions(StringBuffer sb,
int width,
Options options,
int leftPad,
int descPad) |
Deprecated.
Renders the specified Options and return the rendered Options in a StringBuffer.
|
protected StringBuffer |
renderWrappedText(StringBuffer sb,
int width,
int nextLineTabStop,
String text) |
Deprecated.
Renders the specified text and return the rendered Options in a StringBuffer.
|
protected String |
rtrim(String s) |
Deprecated.
Removes the trailing whitespace from the specified String.
|
void |
setArgName(String name) |
Deprecated.
Sets the 'argName'.
|
void |
setDescPadding(int padding) |
Deprecated.
Sets the 'descPadding'.
|
void |
setLeftPadding(int padding) |
Deprecated.
Sets the 'leftPadding'.
|
void |
setLongOptPrefix(String prefix) |
Deprecated.
Sets the 'longOptPrefix'.
|
void |
setLongOptSeparator(String longOptSeparator) |
Deprecated.
Sets the separator displayed between a long option and its value.
|
void |
setNewLine(String newline) |
Deprecated.
Sets the 'newLine'.
|
void |
setOptionComparator(Comparator<Option> comparator) |
Deprecated.
Sets the comparator used to sort the options when they output in help text.
|
void |
setOptPrefix(String prefix) |
Deprecated.
Sets the 'optPrefix'.
|
void |
setSyntaxPrefix(String prefix) |
Deprecated.
Sets the 'syntaxPrefix'.
|
void |
setWidth(int width) |
Deprecated.
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 static HelpFormatter.Builder builder()
public static String getDescription(Option option)
null.option - The option to get the description from.null.protected String createPadding(int len)
len.len - The length of the String of padding to create.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 characterpublic String getArgName()
public int getDescPadding()
public int getLeftPadding()
public String getLongOptPrefix()
public String getLongOptSeparator()
public String getNewLine()
public Comparator<Option> getOptionComparator()
Comparator currently in use to sort the optionspublic String getOptPrefix()
public String getSyntaxPrefix()
public int getWidth()
public 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 by default.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 by default.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 printHelp(String cmdLineSyntax, Options options)
options with the specified command line syntax. This method prints help information
to System.out by default.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 by default.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 by default.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 by default.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 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 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 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 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 PrintWriterpublic 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 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 String rtrim(String s)
s - The String to remove the trailing padding from.public void setArgName(String name)
name - the new value of 'argName'public void setDescPadding(int padding)
padding - the new value of 'descPadding'public void setLeftPadding(int padding)
padding - the new value of 'leftPadding'public void setLongOptPrefix(String prefix)
prefix - the new value of 'longOptPrefix'public void setLongOptSeparator(String longOptSeparator)
longOptSeparator - the separator, typically ' ' or '='.public void setNewLine(String newline)
newline - the new value of 'newLine'public void setOptionComparator(Comparator<Option> comparator)
comparator - the Comparator to use for sorting the optionspublic void setOptPrefix(String prefix)
prefix - the new value of 'optPrefix'public void setSyntaxPrefix(String prefix)
prefix - the new value of 'syntaxPrefix'public void setWidth(int width)
width - the new value of 'width'Copyright © 2002-2025 The Apache Software Foundation. All rights reserved.Apache Commons CLI | Issue management | Source repository