Class AbstractHelpFormatter

java.lang.Object
org.apache.commons.cli.help.AbstractHelpFormatter
Direct Known Subclasses:
HelpFormatter

public abstract class AbstractHelpFormatter extends Object
Helps formatters provides the framework to link a HelpAppendable with a OptionFormatter and a default TableDefinition so to produce standardized format help output.
Since:
1.10.0
  • Field Details

    • DEFAULT_COMPARATOR

      public static final Comparator<Option> DEFAULT_COMPARATOR
      The default comparator for Option implementations.
    • DEFAULT_OPTION_GROUP_SEPARATOR

      public static final String DEFAULT_OPTION_GROUP_SEPARATOR
      The default separator between OptionGroup elements: " | ".
      See Also:
    • DEFAULT_SYNTAX_PREFIX

      public static final String DEFAULT_SYNTAX_PREFIX
      The string to display at the beginning of the usage statement: "usage: ".
      See Also:
    • comparator

      private final Comparator<Option> comparator
      The comparator for sorting Option collections
    • helpAppendable

      private final HelpAppendable helpAppendable
      The HelpAppendable that produces the final output.
    • optionFormatBuilder

      private final OptionFormatter.Builder optionFormatBuilder
      The OptionFormatter.Builder used to display options within the help page.
    • optionGroupSeparator

      private final String optionGroupSeparator
      The separator between OptionGroup components.
    • syntaxPrefix

      private String syntaxPrefix
      The phrase printed before the syntax line.
  • Constructor Details

    • AbstractHelpFormatter

      protected AbstractHelpFormatter(AbstractHelpFormatter.Builder<?,?> builder)
      Constructs the base formatter.
      Parameters:
      builder - the builder.
  • Method Details

    • getComparator

      protected 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 an OptionFormatter for the specified Option.
      Parameters:
      option - The Option to format.
      Returns:
      an OptionFormatter for the specified Option.
    • getOptionGroupSeparator

      protected String getOptionGroupSeparator()
      Gets the option group separator.
      Returns:
      The option group separator.
    • getSerializer

      public final HelpAppendable getSerializer()
      Gets the HelpAppendable associated with this help formatter.
      Returns:
      The HelpAppendable associated with this help formatter.
    • getSyntaxPrefix

      public final String getSyntaxPrefix()
      Gets the currently set syntax prefix.
      Returns:
      The currently set syntax prefix.
    • getTableDefinition

      protected abstract TableDefinition getTableDefinition(Iterable<Option> options)
      Converts a collection of Options into a TableDefinition.
      Parameters:
      options - The options to create a table for.
      Returns:
      the TableDefinition.
    • printHelp

      public void printHelp(String cmdLineSyntax, String header, Iterable<Option> options, String footer, boolean autoUsage) throws IOException
      Prints the help for Options with the specified command line syntax.
      Parameters:
      cmdLineSyntax - the syntax for this application.
      header - the banner to display at the beginning of the help.
      options - the collection of Option objects to print.
      footer - the banner to display at the end of the help.
      autoUsage - whether to print an automatically generated usage statement.
      Throws:
      IOException - If the output could not be written to the HelpAppendable.
    • printHelp

      public void printHelp(String cmdLineSyntax, String header, Options options, String footer, boolean autoUsage) throws IOException
      Prints the help for a collection of Options with the specified command line syntax.
      Parameters:
      cmdLineSyntax - the syntax for this application.
      header - the banner to display at the beginning of the help.
      options - the collection of Option objects to print.
      footer - the banner to display at the end of the help.
      autoUsage - whether to print an automatically generated usage statement.
      Throws:
      IOException - If the output could not be written to the HelpAppendable.
    • printOptions

      public final void printOptions(Iterable<Option> options) throws IOException
      Prints the option table for a collection of Option objects to the HelpAppendable.
      Parameters:
      options - the collection of Option objects to print in the table.
      Throws:
      IOException - If the output could not be written to the HelpAppendable.
    • printOptions

      public final void printOptions(Options options) throws IOException
      Prints the option table for the specified Options to the HelpAppendable.
      Parameters:
      options - the Options to print in the table.
      Throws:
      IOException - If the output could not be written to the HelpAppendable.
    • printOptions

      public final void printOptions(TableDefinition tableDefinition) throws IOException
      Parameters:
      tableDefinition - the TableDefinition to print.
      Throws:
      IOException - If the output could not be written to the HelpAppendable.
    • setSyntaxPrefix

      public final void setSyntaxPrefix(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 List<Option> sort(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 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 String toArgName(String argName)
      Formats the argName as an argument a defined in the enclosed OptionFormatter.Builder.
      Parameters:
      argName - the string to format as an argument.
      Returns:
      the argName formatted as an argument.
    • toSyntaxOptions

      public String toSyntaxOptions(Iterable<Option> options)
      Return the string representation of the options as used in the syntax display.

      This is probably not the method you want. This method does not track the presence of option groups. To display the option grouping use toSyntaxOptions(Options) or toSyntaxOptions(OptionGroup) for individual groups.

      Parameters:
      options - The collection of Option instances to create the string representation for.
      Returns:
      the string representation of the options as used in the syntax display.
    • toSyntaxOptions

      protected String toSyntaxOptions(Iterable<Option> options, 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 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 String toSyntaxOptions(Options options)
      Return the string representation of the options as used in the syntax display.
      Parameters:
      options - The Options to create the string representation for.
      Returns:
      the string representation of the options as used in the syntax display.