Package org.apache.commons.cli
Class HelpFormatter
java.lang.Object
org.apache.commons.cli.HelpFormatter
Deprecated.
A formatter of help messages for command line options.
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classDeprecated.BuildsHelpFormatter.private static final classDeprecated.This class implements theComparatorinterface for comparing Options. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringDeprecated.Default name for an argumentstatic final intDeprecated.Number of space characters to be prefixed to each description linestatic final intDeprecated.Default padding to the left of each linestatic final StringDeprecated.Default prefix for long Optionstatic final StringDeprecated.Default separator displayed between a long Option and its valuestatic final StringDeprecated.Default prefix for shortOptsstatic final StringDeprecated.The string to display at the beginning of the usage statementstatic final intDeprecated.Default number of characters per lineDeprecated.Scope will be made private for next major version - use get/setArgName methods instead.intDeprecated.Scope will be made private for next major version - use get/setDescPadding methods instead.intDeprecated.Scope will be made private for next major version - use get/setLeftPadding methods instead.Deprecated.Scope will be made private for next major version - use get/setLongOptPrefix methods instead.Deprecated.Scope will be made private for next major version - use get/setNewLine methods instead.Deprecated.Scope will be made private for next major version - use get/setOptPrefix methods instead.Deprecated.Scope will be made private for next major version - use get/setSyntaxPrefix methods instead.intDeprecated.Scope will be made private for next major version - use get/setWidth methods instead.Deprecated.Function to format the description for a deprecated option.private static final StringDeprecated."Description" test for options headerprivate static final StringDeprecated."Options" text for options headerprivate static final StringDeprecated."Since" text for options headerprivate StringDeprecated.The separator displayed between the long option and its value.protected Comparator<Option> Deprecated.Comparator used to sort the options when they output in help text Defaults to case-insensitive alphabetical sorting by option keyprivate final PrintWriterDeprecated.Where to print help.private final booleanDeprecated.Flag to determine if since field should be displayed -
Constructor Summary
ConstructorsModifierConstructorDescriptionDeprecated.Constructs a new instance.privateHelpFormatter(Function<Option, String> deprecatedFormatFunction, PrintWriter printWriter, boolean showSince) Deprecated.Constructs a new instance. -
Method Summary
Modifier and TypeMethodDescriptionprivate voidappendOption(StringBuilder buff, Option option, boolean required) Deprecated.Appends the usage clause for an Option to a StringBuffer.private voidappendOptionGroup(StringBuilder buff, OptionGroup optionGroup) Deprecated.Appends the usage clause for an OptionGroup to a StringBuffer.(package private) <A extends Appendable>
AappendOptions(A sb, int width, Options options, int leftPad, int descPad) Deprecated.Renders the specified Options and return the rendered Options in a StringBuffer.(package private) <A extends Appendable>
AappendWrappedText(A appendable, int width, int nextLineTabStop, String text) Deprecated.Renders the specified text and return the rendered Options in a StringBuffer.static HelpFormatter.Builderbuilder()Deprecated.Creates a new builder.private static PrintWriterDeprecated.protected StringcreatePadding(int len) Deprecated.Creates a String of padding of lengthlen.private intdetermineMaxSinceLength(Options options) Deprecated.protected intfindWrapPos(String text, int width, int startPos) Deprecated.Finds the next text wrap position afterstartPosfor the text intextwith the column widthwidth.Deprecated.Gets the 'argName'.intDeprecated.Gets the 'descPadding'.static StringgetDescription(Option option) Deprecated.Gets the option description or an empty string if the description isnull.intDeprecated.Gets the 'leftPadding'.Deprecated.Gets the 'longOptPrefix'.Deprecated.Gets the separator displayed between a long option and its value.Deprecated.Gets the 'newLine'.Deprecated.Comparator used to sort the options when they output in help text.Deprecated.Gets the 'optPrefix'.Deprecated.Gets the 'syntaxPrefix'.intgetWidth()Deprecated.Gets the 'width'.voidDeprecated.Prints the help foroptionswith the specified command line syntax.voidprintHelp(int width, String cmdLineSyntax, String header, Options options, String footer, boolean autoUsage) Deprecated.Prints the help foroptionswith the specified command line syntax.voidprintHelp(PrintWriter pw, int width, String cmdLineSyntax, String header, Options options, int leftPad, int descPad, String footer) Deprecated.Prints the help foroptionswith the specified command line syntax.voidprintHelp(PrintWriter pw, int width, String cmdLineSyntax, String header, Options options, int leftPad, int descPad, String footer, boolean autoUsage) Deprecated.Prints the help foroptionswith the specified command line syntax.voidDeprecated.Prints the help foroptionswith the specified command line syntax.voidDeprecated.Prints the help foroptionswith the specified command line syntax.voidDeprecated.Prints the help foroptionswith the specified command line syntax.voidDeprecated.Prints the help foroptionswith the specified command line syntax.voidprintOptions(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.voidprintUsage(PrintWriter pw, int width, String cmdLineSyntax) Deprecated.Prints the cmdLineSyntax to the specified writer, using the specified width.voidprintUsage(PrintWriter pw, int width, String app, Options options) Deprecated.Prints the usage statement for the specified application.voidprintWrapped(PrintWriter pw, int width, int nextLineTabStop, String text) Deprecated.Prints the specified text to the specified PrintWriter.voidprintWrapped(PrintWriter pw, int width, String text) Deprecated.Prints the specified text to the specified PrintWriter.protected StringBufferrenderOptions(StringBuffer sb, int width, Options options, int leftPad, int descPad) Deprecated.Renders the specified Options and return the rendered Options in a StringBuffer.protected StringBufferrenderWrappedText(StringBuffer sb, int width, int nextLineTabStop, String text) Deprecated.Renders the specified text and return the rendered Options in a StringBuffer.private <A extends Appendable>
ArenderWrappedTextBlock(A appendable, int width, int nextLineTabStop, String text) Deprecated.Renders the specified text width a maximum width.protected StringDeprecated.Removes the trailing whitespace from the specified String.voidsetArgName(String name) Deprecated.Sets the 'argName'.voidsetDescPadding(int padding) Deprecated.Sets the 'descPadding'.voidsetLeftPadding(int padding) Deprecated.Sets the 'leftPadding'.voidsetLongOptPrefix(String prefix) Deprecated.Sets the 'longOptPrefix'.voidsetLongOptSeparator(String longOptSeparator) Deprecated.Sets the separator displayed between a long option and its value.voidsetNewLine(String newline) Deprecated.Sets the 'newLine'.voidsetOptionComparator(Comparator<Option> comparator) Deprecated.Sets the comparator used to sort the options when they output in help text.voidsetOptPrefix(String prefix) Deprecated.Sets the 'optPrefix'.voidsetSyntaxPrefix(String prefix) Deprecated.Sets the 'syntaxPrefix'.voidsetWidth(int width) Deprecated.Sets the 'width'.
-
Field Details
-
HEADER_OPTIONS
Deprecated."Options" text for options header- See Also:
-
HEADER_SINCE
Deprecated."Since" text for options header- See Also:
-
HEADER_DESCRIPTION
Deprecated."Description" test for options header- See Also:
-
DEFAULT_WIDTH
public static final int DEFAULT_WIDTHDeprecated.Default number of characters per line- See Also:
-
DEFAULT_LEFT_PAD
public static final int DEFAULT_LEFT_PADDeprecated.Default padding to the left of each line- See Also:
-
DEFAULT_DESC_PAD
public static final int DEFAULT_DESC_PADDeprecated.Number of space characters to be prefixed to each description line- See Also:
-
DEFAULT_SYNTAX_PREFIX
Deprecated.The string to display at the beginning of the usage statement- See Also:
-
DEFAULT_OPT_PREFIX
Deprecated.Default prefix for shortOpts- See Also:
-
DEFAULT_LONG_OPT_PREFIX
Deprecated.Default prefix for long Option- See Also:
-
DEFAULT_LONG_OPT_SEPARATOR
Deprecated.Default separator displayed between a long Option and its value- Since:
- 1.3
- See Also:
-
DEFAULT_ARG_NAME
Deprecated.Default name for an argument- See Also:
-
defaultWidth
Deprecated.Scope will be made private for next major version - use get/setWidth methods instead.Number of characters per line -
defaultLeftPad
Deprecated.Scope will be made private for next major version - use get/setLeftPadding methods instead.Amount of padding to the left of each line -
defaultDescPad
Deprecated.Scope will be made private for next major version - use get/setDescPadding methods instead.The number of characters of padding to be prefixed to each description line -
defaultSyntaxPrefix
Deprecated.Scope will be made private for next major version - use get/setSyntaxPrefix methods instead.The string to display at the beginning of the usage statement -
defaultNewLine
Deprecated.Scope will be made private for next major version - use get/setNewLine methods instead.The new line string -
defaultOptPrefix
Deprecated.Scope will be made private for next major version - use get/setOptPrefix methods instead.The shortOpt prefix -
defaultLongOptPrefix
Deprecated.Scope will be made private for next major version - use get/setLongOptPrefix methods instead.The long Opt prefix -
defaultArgName
Deprecated.Scope will be made private for next major version - use get/setArgName methods instead.The name of the argument -
optionComparator
Deprecated.Comparator used to sort the options when they output in help text Defaults to case-insensitive alphabetical sorting by option key -
deprecatedFormatFunction
Deprecated.Function to format the description for a deprecated option. -
printWriter
Deprecated.Where to print help. -
showSince
private final boolean showSinceDeprecated.Flag to determine if since field should be displayed -
longOptSeparator
Deprecated.The separator displayed between the long option and its value.
-
-
Constructor Details
-
HelpFormatter
public HelpFormatter()Deprecated.Constructs a new instance. -
HelpFormatter
private HelpFormatter(Function<Option, String> deprecatedFormatFunction, PrintWriter printWriter, boolean showSince) Deprecated.Constructs a new instance.- Parameters:
printWriter- TODO
-
-
Method Details
-
builder
Deprecated.Creates a new builder.- Returns:
- a new builder.
- Since:
- 1.7.0
-
createDefaultPrintWriter
Deprecated. -
getDescription
Deprecated.Gets the option description or an empty string if the description isnull.- Parameters:
option- The option to get the description from.- Returns:
- the option description or an empty string if the description is
null. - Since:
- 1.8.0
-
appendOption
Deprecated.Appends the usage clause for an Option to a StringBuffer.- Parameters:
buff- the StringBuffer to append tooption- the Option to appendrequired- whether the Option is required or not
-
appendOptionGroup
Deprecated.Appends the usage clause for an OptionGroup to a StringBuffer. The clause is wrapped in square brackets if the group is required. The display of the options is handled by appendOption- Parameters:
buff- the StringBuilder to append tooptionGroup- the group to append- See Also:
-
appendOptions
<A extends Appendable> A appendOptions(A sb, int width, Options options, int leftPad, int descPad) throws IOException Deprecated.Renders the specified Options and return the rendered Options in a StringBuffer.- Parameters:
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 line- Returns:
- the StringBuffer with the rendered Options contents.
- Throws:
IOException- if an I/O error occurs.
-
appendWrappedText
<A extends Appendable> A appendWrappedText(A appendable, int width, int nextLineTabStop, String text) throws IOException Deprecated.Renders the specified text and return the rendered Options in a StringBuffer.- Type Parameters:
A- The Appendable implementation.- Parameters:
appendable- 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.- Returns:
- the StringBuffer with the rendered Options contents.
- Throws:
IOException- if an I/O error occurs.
-
createPadding
Deprecated.Creates a String of padding of lengthlen.- Parameters:
len- The length of the String of padding to create.- Returns:
- The String of padding
-
determineMaxSinceLength
Deprecated. -
findWrapPos
Deprecated.Finds the next text wrap position afterstartPosfor the text intextwith the column widthwidth. 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.- Parameters:
text- The text being searched for the wrap positionwidth- width of the wrapped textstartPos- position from which to start the lookup whitespace character- Returns:
- position on which the text must be wrapped or -1 if the wrap position is at the end of the text
-
getArgName
Deprecated.Gets the 'argName'.- Returns:
- the 'argName'
-
getDescPadding
public int getDescPadding()Deprecated.Gets the 'descPadding'.- Returns:
- the 'descPadding'
-
getLeftPadding
public int getLeftPadding()Deprecated.Gets the 'leftPadding'.- Returns:
- the 'leftPadding'
-
getLongOptPrefix
Deprecated.Gets the 'longOptPrefix'.- Returns:
- the 'longOptPrefix'
-
getLongOptSeparator
Deprecated.Gets the separator displayed between a long option and its value.- Returns:
- the separator
- Since:
- 1.3
-
getNewLine
Deprecated.Gets the 'newLine'.- Returns:
- the 'newLine'
-
getOptionComparator
Deprecated.Comparator used to sort the options when they output in help text. Defaults to case-insensitive alphabetical sorting by option key.- Returns:
- the
Comparatorcurrently in use to sort the options - Since:
- 1.2
-
getOptPrefix
Deprecated.Gets the 'optPrefix'.- Returns:
- the 'optPrefix'
-
getSyntaxPrefix
Deprecated.Gets the 'syntaxPrefix'.- Returns:
- the 'syntaxPrefix'
-
getWidth
public int getWidth()Deprecated.Gets the 'width'.- Returns:
- the 'width'
-
printHelp
public void printHelp(int width, String cmdLineSyntax, String header, Options options, String footer) Deprecated.Prints the help foroptionswith the specified command line syntax. This method prints help information toSystem.outby default.- Parameters:
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 help
-
printHelp
public void printHelp(int width, String cmdLineSyntax, String header, Options options, String footer, boolean autoUsage) Deprecated.Prints the help foroptionswith the specified command line syntax. This method prints help information toSystem.outby default.- Parameters:
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 statement
-
printHelp
public void printHelp(PrintWriter pw, int width, String cmdLineSyntax, String header, Options options, int leftPad, int descPad, String footer) Deprecated.Prints the help foroptionswith the specified command line syntax.- Parameters:
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 help- Throws:
IllegalStateException- if there is no room to print a line
-
printHelp
public void printHelp(PrintWriter pw, int width, String cmdLineSyntax, String header, Options options, int leftPad, int descPad, String footer, boolean autoUsage) Deprecated.Prints the help foroptionswith the specified command line syntax.- Parameters:
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 statement- Throws:
IllegalStateException- if there is no room to print a line
-
printHelp
Deprecated.Prints the help foroptionswith the specified command line syntax. This method prints help information toSystem.outby default.- Parameters:
cmdLineSyntax- the syntax for this applicationoptions- the Options instance
-
printHelp
Deprecated.Prints the help foroptionswith the specified command line syntax. This method prints help information toSystem.outby default.- Parameters:
cmdLineSyntax- the syntax for this applicationoptions- the Options instanceautoUsage- whether to print an automatically generated usage statement
-
printHelp
Deprecated.Prints the help foroptionswith the specified command line syntax. This method prints help information toSystem.outby default.- Parameters:
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 help
-
printHelp
public void printHelp(String cmdLineSyntax, String header, Options options, String footer, boolean autoUsage) Deprecated.Prints the help foroptionswith the specified command line syntax. This method prints help information toSystem.outby default.- Parameters:
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 statement
-
printOptions
Deprecated.Prints the help for the specified Options to the specified writer, using the specified width, left padding and description padding.- Parameters:
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 line
-
printUsage
Deprecated.Prints the cmdLineSyntax to the specified writer, using the specified width.- Parameters:
pw- The printWriter to write the help towidth- The number of characters per line for the usage statement.cmdLineSyntax- The usage statement.
-
printUsage
Deprecated.Prints the usage statement for the specified application.- Parameters:
pw- The PrintWriter to print the usage statementwidth- The number of characters to display per lineapp- The application nameoptions- The command line Options
-
printWrapped
Deprecated.Prints the specified text to the specified PrintWriter.- Parameters:
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 PrintWriter
-
printWrapped
Deprecated.Prints the specified text to the specified PrintWriter.- Parameters:
pw- The printWriter to write the help towidth- The number of characters to display per linetext- The text to be written to the PrintWriter
-
renderOptions
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.- Parameters:
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 line- Returns:
- the StringBuffer with the rendered Options contents.
-
renderWrappedText
protected StringBuffer renderWrappedText(StringBuffer sb, int width, int nextLineTabStop, String text) Deprecated.Renders the specified text and return the rendered Options in a StringBuffer.- Parameters:
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.- Returns:
- the StringBuffer with the rendered Options contents.
-
renderWrappedTextBlock
private <A extends Appendable> A renderWrappedTextBlock(A appendable, int width, int nextLineTabStop, String text) Deprecated.Renders the specified text width a maximum width. This method differs from renderWrappedText by not removing leading spaces after a new line.- Parameters:
appendable- 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.
-
rtrim
Deprecated.Removes the trailing whitespace from the specified String.- Parameters:
s- The String to remove the trailing padding from.- Returns:
- The String of without the trailing padding
-
setArgName
Deprecated.Sets the 'argName'.- Parameters:
name- the new value of 'argName'
-
setDescPadding
public void setDescPadding(int padding) Deprecated.Sets the 'descPadding'.- Parameters:
padding- the new value of 'descPadding'
-
setLeftPadding
public void setLeftPadding(int padding) Deprecated.Sets the 'leftPadding'.- Parameters:
padding- the new value of 'leftPadding'
-
setLongOptPrefix
Deprecated.Sets the 'longOptPrefix'.- Parameters:
prefix- the new value of 'longOptPrefix'
-
setLongOptSeparator
Deprecated.Sets the separator displayed between a long option and its value. Ensure that the separator specified is supported by the parser used, typically ' ' or '='.- Parameters:
longOptSeparator- the separator, typically ' ' or '='.- Since:
- 1.3
-
setNewLine
Deprecated.Sets the 'newLine'.- Parameters:
newline- the new value of 'newLine'
-
setOptionComparator
Deprecated.Sets the comparator used to sort the options when they output in help text. Passing in a null comparator will keep the options in the order they were declared.- Parameters:
comparator- theComparatorto use for sorting the options- Since:
- 1.2
-
setOptPrefix
Deprecated.Sets the 'optPrefix'.- Parameters:
prefix- the new value of 'optPrefix'
-
setSyntaxPrefix
Deprecated.Sets the 'syntaxPrefix'.- Parameters:
prefix- the new value of 'syntaxPrefix'
-
setWidth
public void setWidth(int width) Deprecated.Sets the 'width'.- Parameters:
width- the new value of 'width'
-
HelpFormatter.