Class UsageMessageFormatter

  • Direct Known Subclasses:
    io.vertx.core.impl.launcher.VertxCommandLauncher

    public class UsageMessageFormatter
    extends Object
    Usage message formatter.
    Author:
    Clement Escoffier
    • Field Detail

      • DEFAULT_WIDTH

        public static final int DEFAULT_WIDTH
        default number of characters per line
        See Also:
        Constant Field Values
      • DEFAULT_LEFT_PAD

        public static final int DEFAULT_LEFT_PAD
        default padding to the left of each line
        See Also:
        Constant Field Values
      • DEFAULT_DESC_PAD

        public static final int DEFAULT_DESC_PAD
        number of space characters to be prefixed to each description line
        See Also:
        Constant Field Values
      • DEFAULT_USAGE_PREFIX

        public static final String DEFAULT_USAGE_PREFIX
        the string to display at the beginning of the usage statement
        See Also:
        Constant Field Values
      • DEFAULT_LONG_OPT_PREFIX

        public static final String DEFAULT_LONG_OPT_PREFIX
        default prefix for long Option
        See Also:
        Constant Field Values
      • DEFAULT_LONG_OPT_SEPARATOR

        public static final String DEFAULT_LONG_OPT_SEPARATOR
        default separator displayed between a long Option and its value
        See Also:
        Constant Field Values
      • optionComparator

        protected Comparator<Option> optionComparator
        Comparator used to sort the options when they output in help text

        Defaults to case-insensitive alphabetical sorting by option key.

    • Constructor Detail

      • UsageMessageFormatter

        public UsageMessageFormatter()
    • Method Detail

      • setWidth

        public void setWidth​(int width)
      • getWidth

        public int getWidth()
      • setLeftPadding

        public void setLeftPadding​(int padding)
      • getLeftPadding

        public int getLeftPadding()
      • setDescPadding

        public void setDescPadding​(int padding)
      • getDescPadding

        public int getDescPadding()
      • setUsagePrefix

        public void setUsagePrefix​(String prefix)
      • getUsagePrefix

        public String getUsagePrefix()
      • setNewLine

        public void setNewLine​(String newline)
      • getNewLine

        public String getNewLine()
      • setOptionPrefix

        public void setOptionPrefix​(String prefix)
      • getOptionPrefix

        public String getOptionPrefix()
      • setLongOptionPrefix

        public void setLongOptionPrefix​(String prefix)
      • getLongOptionPrefix

        public String getLongOptionPrefix()
      • setLongOptionSeparator

        public void setLongOptionSeparator​(String longOptSeparator)
        Set 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 '='.
      • getLongOptionSeparator

        public String getLongOptionSeparator()
        Returns the separator displayed between a long option and its value.
        Returns:
        the separator
      • setArgName

        public void setArgName​(String name)
      • getArgName

        public String getArgName()
      • getOptionComparator

        public Comparator<Option> getOptionComparator()
        Comparator used to sort the options when they output in help text. Defaults to case-insensitive alphabetical sorting by option key.
        Returns:
        the Comparator currently in use to sort the options
      • setOptionComparator

        public void setOptionComparator​(Comparator<Option> comparator)
        Set 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 - the Comparator to use for sorting the options
      • appendOption

        protected void appendOption​(StringBuilder buff,
                                    Option option)
        Appends the usage clause for an Option to a StringBuilder.
        Parameters:
        buff - the StringBuilder to append to
        option - the Option to append
      • appendArgument

        protected void appendArgument​(StringBuilder buff,
                                      Argument argument,
                                      boolean required)
        Appends the usage clause for an Argument to a StringBuilder.
        Parameters:
        buff - the StringBuilder to append to
        argument - the argument to add
        required - whether the Option is required or not
      • usage

        public void usage​(StringBuilder builder,
                          CLI cli)
        Computes the usage of the given CLI.
        Parameters:
        builder - where the usage is going to be written
        cli - the cli
      • usage

        public void usage​(StringBuilder builder,
                          String prefix,
                          CLI cli)
        Computes the usage of the given CLI.
        Parameters:
        builder - where the usage is going to be written
        prefix - a prefix to prepend to the usage line. It will be added between 'Usage: ' and the CLI name.
        cli - the cli
      • computeOptionsAndArguments

        public void computeOptionsAndArguments​(StringBuilder buffer,
                                               List<Option> options,
                                               List<Argument> arguments)
        Computes the help for the specified Options to the specified writer.
        Parameters:
        buffer - The buffer to write the help to
        options - The command line Options
        arguments - the command line Arguments
      • buildWrapped

        public void buildWrapped​(StringBuilder buffer,
                                 String text)
        Builds the specified text to the specified buffer.
        Parameters:
        buffer - The buffer to write the help to
        text - The text to be written to the buffer
      • buildWrapped

        public void buildWrapped​(StringBuilder buffer,
                                 int nextLineTabStop,
                                 String text)
        Builds the specified text to the specified buffer.
        Parameters:
        buffer - The buffer to write the help to
        nextLineTabStop - The position on the next line for the first tab.
        text - The text to be written to the buffer
      • isNullOrEmpty

        public static boolean isNullOrEmpty​(String s)
      • renderOptionsAndArguments

        protected StringBuilder renderOptionsAndArguments​(StringBuilder sb,
                                                          List<Option> options,
                                                          List<Argument> arguments)
        Renders the specified Options and Arguments and return the rendered output in a StringBuilder.
        Parameters:
        sb - The StringBuilder to place the rendered Options and Arguments into.
        options - The command line Options
        arguments - The command line Arguments
        Returns:
        the StringBuilder with the rendered content.
      • renderWrappedText

        protected StringBuilder renderWrappedText​(StringBuilder sb,
                                                  int width,
                                                  int nextLineTabStop,
                                                  String text)
        Render the specified text and return the rendered Options in a StringBuilder.
        Parameters:
        sb - The StringBuilder to place the rendered text into.
        width - The number of characters to display per line
        nextLineTabStop - The position on the next line for the first tab.
        text - The text to be rendered.
        Returns:
        the StringBuilder with the rendered Options contents.
      • renderWrappedTextBlock

        public Appendable renderWrappedTextBlock​(StringBuilder sb,
                                                 int width,
                                                 int nextLineTabStop,
                                                 String text)
        Renders the specified text width a maximum width. This method differs from renderWrappedText by not removing leading spaces after a new line.
        Parameters:
        sb - The StringBuilder to place the rendered text into.
        width - The number of characters to display per line
        nextLineTabStop - The position on the next line for the first tab.
        text - The text to be rendered.
      • findWrapPos

        public static int findWrapPos​(String text,
                                      int width,
                                      int startPos)
        Finds the next text wrap position after 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.
        Parameters:
        text - The text being searched for the wrap position
        width - width of the wrapped text
        startPos - 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
      • createPadding

        public static String createPadding​(int len)
        Return a String of padding of length len.
        Parameters:
        len - The length of the String of padding to create.
        Returns:
        The String of padding
      • rtrim

        public static String rtrim​(String s)
        Remove 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