Class AbstractHelpFormatter

  • Direct Known Subclasses:
    HelpFormatter

    public abstract class AbstractHelpFormatter
    extends java.lang.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 Detail

      • DEFAULT_COMPARATOR

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

        public static final java.lang.String DEFAULT_OPTION_GROUP_SEPARATOR
        The default separator between OptionGroup elements: " | ".
        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
      • comparator

        private final java.util.Comparator<Option> comparator
        The comparator for sorting Option collections
      • optionFormatBuilder

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

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

        private java.lang.String syntaxPrefix
        The phrase printed before the syntax line.
    • 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.
      • getOptionGroupSeparator

        protected java.lang.String getOptionGroupSeparator()
        Gets the option group separator.
        Returns:
        The option group separator.
      • 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 of Options into a TableDefinition.
        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 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:
        java.io.IOException - If the output could not be written to the HelpAppendable.
      • printHelp

        public 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 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:
        java.io.IOException - If the output could not be written to the HelpAppendable.
      • printOptions

        public final void printOptions​(java.lang.Iterable<Option> options)
                                throws java.io.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:
        java.io.IOException - If the output could not be written to the HelpAppendable.
      • printOptions

        public final void printOptions​(Options options)
                                throws java.io.IOException
        Prints the option table for the specified Options to the HelpAppendable.
        Parameters:
        options - the Options to print in the table.
        Throws:
        java.io.IOException - If the output could not be written to the HelpAppendable.
      • 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 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 java.lang.String toSyntaxOptions​(java.lang.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 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 - The Options to create the string representation for.
        Returns:
        the string representation of the options as used in the syntax display.