Package org.apache.commons.cli.help
Class AbstractHelpFormatter
- java.lang.Object
-
- org.apache.commons.cli.help.AbstractHelpFormatter
-
- Direct Known Subclasses:
HelpFormatter
public abstract class AbstractHelpFormatter extends java.lang.Object
Helps formatters provides the framework to link aHelpAppendablewith aOptionFormatterand a defaultTableDefinitionso to produce standardized format help output.- Since:
- 1.10.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classAbstractHelpFormatter.Builder<B extends AbstractHelpFormatter.Builder<B,T>,T extends AbstractHelpFormatter>Abstracts building instances for subclasses.
-
Field Summary
Fields Modifier and Type Field Description static java.util.Comparator<Option>DEFAULT_COMPARATORThe default comparator forOptionimplementations.static java.lang.StringDEFAULT_OPTION_GROUP_SEPARATORThe default separator betweenOptionGroupelements: " | ".static java.lang.StringDEFAULT_SYNTAX_PREFIXThe string to display at the beginning of the usage statement: "usage: ".
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractHelpFormatter(AbstractHelpFormatter.Builder<?,?> builder)Constructs the base formatter.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected java.util.Comparator<Option>getComparator()Gets the comparator for sorting options.protected HelpAppendablegetHelpAppendable()Gets the help appendable.protected OptionFormatter.BuildergetOptionFormatBuilder()Gets the option formatter builder.OptionFormattergetOptionFormatter(Option option)Constructs anOptionFormatterfor the specifiedOption.protected java.lang.StringgetOptionGroupSeparator()Gets the option group separator.HelpAppendablegetSerializer()Gets theHelpAppendableassociated with this help formatter.java.lang.StringgetSyntaxPrefix()Gets the currently set syntax prefix.protected abstract TableDefinitiongetTableDefinition(java.lang.Iterable<Option> options)Converts a collection ofOptions into aTableDefinition.voidprintHelp(java.lang.String cmdLineSyntax, java.lang.String header, java.lang.Iterable<Option> options, java.lang.String footer, boolean autoUsage)Prints the help for a collection ofOptions with the specified command line syntax.voidprintHelp(java.lang.String cmdLineSyntax, java.lang.String header, Options options, java.lang.String footer, boolean autoUsage)Prints the help forOptionswith the specified command line syntax.voidprintOptions(java.lang.Iterable<Option> options)Prints the option table for a collection ofOptionobjects to theHelpAppendable.voidprintOptions(TableDefinition tableDefinition)Prints aTableDefinitionto theHelpAppendable.voidprintOptions(Options options)Prints the option table for the specifiedOptionsto theHelpAppendable.voidsetSyntaxPrefix(java.lang.String prefix)Sets the syntax prefix.java.util.List<Option>sort(java.lang.Iterable<Option> options)Creates a new list of options ordered by the comparator.java.util.List<Option>sort(Options options)Creates a new list of options ordered by the comparator.java.lang.StringtoArgName(java.lang.String argName)Formats theargNameas an argument a defined in the enclosedOptionFormatter.Builderjava.lang.StringtoSyntaxOptions(java.lang.Iterable<Option> options)Return the string representation of the options as used in the syntax display.protected java.lang.StringtoSyntaxOptions(java.lang.Iterable<Option> options, java.util.function.Function<Option,OptionGroup> lookup)Return the string representation of the options as used in the syntax display.java.lang.StringtoSyntaxOptions(OptionGroup group)Return the string representation of the options as used in the syntax display.java.lang.StringtoSyntaxOptions(Options options)Return the string representation of the options as used in the syntax display.
-
-
-
Field Detail
-
DEFAULT_COMPARATOR
public static final java.util.Comparator<Option> DEFAULT_COMPARATOR
The default comparator forOptionimplementations.
-
DEFAULT_OPTION_GROUP_SEPARATOR
public static final java.lang.String DEFAULT_OPTION_GROUP_SEPARATOR
The default separator betweenOptionGroupelements: " | ".- See Also:
- Constant Field Values
-
DEFAULT_SYNTAX_PREFIX
public static final java.lang.String DEFAULT_SYNTAX_PREFIX
The string to display at the beginning of the usage statement: "usage: ".- See Also:
- Constant Field Values
-
-
Constructor Detail
-
AbstractHelpFormatter
protected AbstractHelpFormatter(AbstractHelpFormatter.Builder<?,?> builder)
Constructs the base formatter.- Parameters:
builder- the builder
-
-
Method Detail
-
getComparator
protected java.util.Comparator<Option> getComparator()
Gets the comparator for sorting options.- Returns:
- The comparator for sorting options.
-
getHelpAppendable
protected HelpAppendable getHelpAppendable()
Gets the help appendable.- Returns:
- The help appendable.
-
getOptionFormatBuilder
protected OptionFormatter.Builder getOptionFormatBuilder()
Gets the option formatter builder.- Returns:
- The option formatter builder.
-
getOptionFormatter
public final OptionFormatter getOptionFormatter(Option option)
Constructs anOptionFormatterfor the specifiedOption.- Parameters:
option- The Option to format.- Returns:
- an
OptionFormatterfor the specifiedOption.
-
getOptionGroupSeparator
protected java.lang.String getOptionGroupSeparator()
Gets the option group separator.- Returns:
- The option group separator.
-
getSerializer
public final HelpAppendable getSerializer()
Gets theHelpAppendableassociated with this help formatter.- Returns:
- The
HelpAppendableassociated with this help formatter.
-
getSyntaxPrefix
public final java.lang.String getSyntaxPrefix()
Gets the currently set syntax prefix.- Returns:
- The currently set syntax prefix.
-
getTableDefinition
protected abstract TableDefinition getTableDefinition(java.lang.Iterable<Option> options)
Converts a collection ofOptions into aTableDefinition.- Parameters:
options- The options to create a table for.- Returns:
- the TableDefinition.
-
printHelp
public void printHelp(java.lang.String cmdLineSyntax, java.lang.String header, java.lang.Iterable<Option> options, java.lang.String footer, boolean autoUsage) throws java.io.IOException
Prints the help for a collection ofOptions with the specified command line syntax.- Parameters:
cmdLineSyntax- the syntax for this applicationheader- the banner to display at the beginning of the helpoptions- the collection ofOptionobjects to print.footer- the banner to display at the end of the helpautoUsage- whether to print an automatically generated usage statement- Throws:
java.io.IOException- If the output could not be written to theHelpAppendable
-
printHelp
public final void printHelp(java.lang.String cmdLineSyntax, java.lang.String header, Options options, java.lang.String footer, boolean autoUsage) throws java.io.IOException
Prints the help forOptionswith the specified command line syntax.- Parameters:
cmdLineSyntax- the syntax for this applicationheader- the banner to display at the beginning of the helpoptions- theOptionsto printfooter- the banner to display at the end of the helpautoUsage- whether to print an automatically generated usage statement- Throws:
java.io.IOException- If the output could not be written to theHelpAppendable
-
printOptions
public final void printOptions(java.lang.Iterable<Option> options) throws java.io.IOException
Prints the option table for a collection ofOptionobjects to theHelpAppendable.- Parameters:
options- the collection of Option objects to print in the table.- Throws:
java.io.IOException- If the output could not be written to theHelpAppendable
-
printOptions
public final void printOptions(Options options) throws java.io.IOException
Prints the option table for the specifiedOptionsto theHelpAppendable.- Parameters:
options- the Options to print in the table.- Throws:
java.io.IOException- If the output could not be written to theHelpAppendable
-
printOptions
public final void printOptions(TableDefinition tableDefinition) throws java.io.IOException
Prints aTableDefinitionto theHelpAppendable.- Parameters:
tableDefinition- theTableDefinitionto print.- Throws:
java.io.IOException- If the output could not be written to theHelpAppendable
-
setSyntaxPrefix
public final void setSyntaxPrefix(java.lang.String prefix)
Sets the syntax prefix. This is the phrase that is printed before the syntax line.- Parameters:
prefix- the new value for the syntax prefix.
-
sort
public java.util.List<Option> sort(java.lang.Iterable<Option> options)
Creates a new list of options ordered by the comparator.- Parameters:
options- the Options to sort.- Returns:
- a new list of options ordered by the comparator.
-
sort
public java.util.List<Option> sort(Options options)
Creates a new list of options ordered by the comparator.- Parameters:
options- the Options to sort.- Returns:
- a new list of options ordered by the comparator.
-
toArgName
public final java.lang.String toArgName(java.lang.String argName)
Formats theargNameas an argument a defined in the enclosedOptionFormatter.Builder- Parameters:
argName- the string to format as an argument.- Returns:
- the
argNameformatted as an argument.
-
toSyntaxOptions
public java.lang.String toSyntaxOptions(java.lang.Iterable<Option> options)
Return the string representation of the options as used in the syntax display.- Parameters:
options- The collection ofOptioninstances to create the string representation for.- Returns:
- the string representation of the options as used in the syntax display.
-
toSyntaxOptions
protected java.lang.String toSyntaxOptions(java.lang.Iterable<Option> options, java.util.function.Function<Option,OptionGroup> lookup)
Return the string representation of the options as used in the syntax display.- Parameters:
options- The options to create the string representation for.lookup- a function to determine if the Option is part of an OptionGroup that has already been processed.- Returns:
- the string representation of the options as used in the syntax display.
-
toSyntaxOptions
public java.lang.String toSyntaxOptions(OptionGroup group)
Return the string representation of the options as used in the syntax display.- Parameters:
group- The OptionGroup to create the string representation for.- Returns:
- the string representation of the options as used in the syntax display.
-
toSyntaxOptions
public java.lang.String toSyntaxOptions(Options options)
Return the string representation of the options as used in the syntax display.- Parameters:
options- TheOptionsto create the string representation for.- Returns:
- the string representation of the options as used in the syntax display.
-
-