Package io.vertx.core.cli
Class UsageMessageFormatter
- java.lang.Object
-
- io.vertx.core.cli.UsageMessageFormatter
-
- Direct Known Subclasses:
io.vertx.core.impl.launcher.VertxCommandLauncher
public class UsageMessageFormatter extends Object
Usage message formatter.- Author:
- Clement Escoffier
-
-
Field Summary
Fields Modifier and Type Field Description static String
DEFAULT_ARG_NAME
default name for an argumentstatic int
DEFAULT_DESC_PAD
number of space characters to be prefixed to each description linestatic int
DEFAULT_LEFT_PAD
default padding to the left of each linestatic String
DEFAULT_LONG_OPT_PREFIX
default prefix for long Optionstatic String
DEFAULT_LONG_OPT_SEPARATOR
default separator displayed between a long Option and its valuestatic String
DEFAULT_OPT_PREFIX
default prefix for shortOptsstatic String
DEFAULT_USAGE_PREFIX
the string to display at the beginning of the usage statementstatic int
DEFAULT_WIDTH
default number of characters per lineprotected Comparator<Option>
optionComparator
Comparator used to sort the options when they output in help text
-
Constructor Summary
Constructors Constructor Description UsageMessageFormatter()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
appendArgument(StringBuilder buff, Argument argument, boolean required)
Appends the usage clause for an Argument to a StringBuilder.protected void
appendOption(StringBuilder buff, Option option)
Appends the usage clause for an Option to a StringBuilder.void
buildWrapped(StringBuilder buffer, int nextLineTabStop, String text)
Builds the specified text to the specified buffer.void
buildWrapped(StringBuilder buffer, String text)
Builds the specified text to the specified buffer.void
computeOptionsAndArguments(StringBuilder buffer, List<Option> options, List<Argument> arguments)
Computes the help for the specified Options to the specified writer.void
computeUsage(StringBuilder buffer, String cmdLineSyntax)
void
computeUsageLine(StringBuilder buffer, String prefix, CLI cli)
static String
createPadding(int len)
Return a String of padding of lengthlen
.static int
findWrapPos(String text, int width, int startPos)
Finds the next text wrap position afterstartPos
for the text intext
with the column widthwidth
.String
getArgName()
int
getDescPadding()
int
getLeftPadding()
String
getLongOptionPrefix()
String
getLongOptionSeparator()
Returns the separator displayed between a long option and its value.String
getNewLine()
Comparator<Option>
getOptionComparator()
Comparator used to sort the options when they output in help text.String
getOptionPrefix()
String
getUsagePrefix()
int
getWidth()
static boolean
isNullOrEmpty(String s)
protected StringBuilder
renderCommands(StringBuilder sb, Collection<CLI> commands)
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.protected StringBuilder
renderWrappedText(StringBuilder sb, int width, int nextLineTabStop, String text)
Render the specified text and return the rendered Options in a StringBuilder.Appendable
renderWrappedTextBlock(StringBuilder sb, int width, int nextLineTabStop, String text)
Renders the specified text width a maximum width.static String
rtrim(String s)
Remove the trailing whitespace from the specified String.void
setArgName(String name)
void
setDescPadding(int padding)
void
setLeftPadding(int padding)
void
setLongOptionPrefix(String prefix)
void
setLongOptionSeparator(String longOptSeparator)
Set the separator displayed between a long option and its value.void
setNewLine(String newline)
void
setOptionComparator(Comparator<Option> comparator)
Set the comparator used to sort the options when they output in help text.void
setOptionPrefix(String prefix)
void
setUsagePrefix(String prefix)
void
setWidth(int width)
void
usage(StringBuilder builder, CLI cli)
Computes the usage of the givenCLI
.void
usage(StringBuilder builder, String prefix, CLI cli)
Computes the usage of the givenCLI
.
-
-
-
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_OPT_PREFIX
public static final String DEFAULT_OPT_PREFIX
default prefix for shortOpts- 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
-
DEFAULT_ARG_NAME
public static final String DEFAULT_ARG_NAME
default name for an argument- 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.
-
-
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
- theComparator
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 tooption
- 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 toargument
- the argument to addrequired
- whether the Option is required or not
-
usage
public void usage(StringBuilder builder, CLI cli)
Computes the usage of the givenCLI
.- Parameters:
builder
- where the usage is going to be writtencli
- the cli
-
usage
public void usage(StringBuilder builder, String prefix, CLI cli)
Computes the usage of the givenCLI
.- Parameters:
builder
- where the usage is going to be writtenprefix
- a prefix to prepend to the usage line. It will be added between 'Usage: ' and the CLI name.cli
- the cli
-
computeUsage
public void computeUsage(StringBuilder buffer, String cmdLineSyntax)
-
computeUsageLine
public void computeUsageLine(StringBuilder buffer, String prefix, CLI 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 tooptions
- The command line Optionsarguments
- 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 totext
- 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 tonextLineTabStop
- The position on the next line for the first tab.text
- The text to be written to the buffer
-
renderCommands
protected StringBuilder renderCommands(StringBuilder sb, Collection<CLI> commands)
-
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 Optionsarguments
- 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 linenextLineTabStop
- 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 linenextLineTabStop
- 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 afterstartPos
for the text intext
with 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
-
createPadding
public static String createPadding(int len)
Return a String of padding of lengthlen
.- Parameters:
len
- The length of the String of padding to create.- Returns:
- The String of padding
-
-