/** Returns the number of characters the synopsis heading will take on the same line as the synopsis. * @return the number of characters the synopsis heading will take on the same line as the synopsis. * @see #detailedSynopsis(int, Comparator, boolean) */ public int synopsisHeadingLength() { String[] lines = Ansi.OFF.new Text(commandSpec.usageMessage().synopsisHeading()).toString().split("\\r?\\n|\\r|%n", -1); return lines[lines.length - 1].length(); } /**
/** Returns command footer text as a string. Footer text can be zero or more lines, and can be specified * declaratively with the {@link Command#footer()} annotation attribute or programmatically by * setting the Help instance's {@link Help#footer} field. * @param params Arguments referenced by the format specifiers in the footer strings * @return the footer lines combined into a single String (which may be empty) */ public String footer(Object... params) { return join(ansi(), width(), commandSpec.usageMessage().footer(), new StringBuilder(), params).toString(); }
/** Returns command custom synopsis as a string. A custom synopsis can be zero or more lines, and can be * specified declaratively with the {@link Command#customSynopsis()} annotation attribute or programmatically * by setting the Help instance's {@link Help#customSynopsis} field. * @param params Arguments referenced by the format specifiers in the synopsis strings * @return the custom synopsis lines combined into a single String (which may be empty) */ public String customSynopsis(Object... params) { return join(ansi(), width(), commandSpec.usageMessage().customSynopsis(), new StringBuilder(), params).toString(); } /** Returns command description text as a string. Description text can be zero or more lines, and can be specified
/** * Returns a synopsis for the command, reserving the specified space for the synopsis heading. * @param synopsisHeadingLength the length of the synopsis heading that will be displayed on the same line * @return a synopsis * @see #abbreviatedSynopsis() * @see #detailedSynopsis(Comparator, boolean) * @see #synopsisHeading */ public String synopsis(int synopsisHeadingLength) { if (!empty(commandSpec.usageMessage().customSynopsis())) { return customSynopsis(); } return commandSpec.usageMessage().abbreviateSynopsis() ? abbreviatedSynopsis() : detailedSynopsis(synopsisHeadingLength, createShortOptionArityAndNameComparator(), true); }
/** Returns the text displayed before the command list; an empty string if there are no commands, * otherwise the result of {@code String.format(commandListHeading, params)}. * @param params the parameters to use to format the command list heading * @return the formatted command list heading */ public String commandListHeading(Object... params) { return commands.isEmpty() ? "" : heading(ansi(), width(), commandSpec.usageMessage().commandListHeading(), params); }
private static CommandSpec createCmd(String name, String description) { CommandSpec cmd = CommandSpec.create().name(name).mixinStandardHelpOptions(true); cmd.usageMessage().description(description); return cmd; }
@Test public void testUsageHelp_abbreviateSynopsisWithoutPositional() throws UnsupportedEncodingException { CommandSpec spec = CommandSpec.create(); spec.usageMessage().abbreviateSynopsis(true).requiredOptionMarker('!').sortOptions(false); spec.addOption(OptionSpec.builder("-x").required(true).description("required").build()); CommandLine commandLine = new CommandLine(spec); String actual = usageString(commandLine, Ansi.OFF); String expected = String.format("" + "Usage: <main class> [OPTIONS]%n" + "! -x required%n"); assertEquals(expected, actual); }
/** Returns the text displayed before the description text; an empty string if there is no description, * otherwise the result of {@code String.format(descriptionHeading, params)}. * @param params the parameters to use to format the description heading * @return the formatted description heading */ public String descriptionHeading(Object... params) { return empty(commandSpec.usageMessage().descriptionHeading()) ? "" : heading(ansi(), width(), commandSpec.usageMessage().descriptionHeading(), params); }
/** Returns the text displayed before the header text; the result of {@code String.format(headerHeading, params)}. * @param params the parameters to use to format the header heading * @return the formatted header heading */ public String headerHeading(Object... params) { return heading(ansi(), width(), commandSpec.usageMessage().headerHeading(), params); }
/** Returns the command header text as a string. Header text can be zero or more lines, and can be specified * declaratively with the {@link Command#header()} annotation attribute or programmatically by * setting the Help instance's {@link Help#header} field. * @param params Arguments referenced by the format specifiers in the header strings * @return the header lines combined into a single String (which may be empty) */ public String header(Object... params) { return join(ansi(), width(), commandSpec.usageMessage().header(), new StringBuilder(), params).toString(); } /** Returns command footer text as a string. Footer text can be zero or more lines, and can be specified
/** Returns command description text as a string. Description text can be zero or more lines, and can be specified * declaratively with the {@link Command#description()} annotation attribute or programmatically by * setting the Help instance's {@link Help#description} field. * @param params Arguments referenced by the format specifiers in the description strings * @return the description lines combined into a single String (which may be empty) */ public String description(Object... params) { return join(ansi(), width(), commandSpec.usageMessage().description(), new StringBuilder(), params).toString(); } /** Returns the command header text as a string. Header text can be zero or more lines, and can be specified
@Test public void testUsageWidthFromCommandAttribute() { @Command(usageHelpWidth = 60, description = "0123456789012345678901234567890123456789012345678901234567890123456789") class App {} CommandLine cmd = new CommandLine(new App()); assertEquals(60, cmd.getUsageHelpWidth()); assertEquals(60, cmd.getCommandSpec().usageMessage().width()); }
@Test public void testCommandSpecUsageMessageSetter() { CommandSpec spec = CommandSpec.wrapWithoutInspection(null); UsageMessageSpec old = spec.usageMessage(); assertSame(old, spec.usageMessage()); assertArrayEquals(new String[0], spec.usageMessage().description()); UsageMessageSpec update = new UsageMessageSpec().description("hi"); spec.usageMessage(update); assertSame(old, spec.usageMessage()); assertArrayEquals(new String[] {"hi"}, spec.usageMessage().description()); }
/** Returns the text displayed before the footer text; the result of {@code String.format(footerHeading, params)}. * @param params the parameters to use to format the footer heading * @return the formatted footer heading */ public String footerHeading(Object... params) { return heading(ansi(), width(), commandSpec.usageMessage().footerHeading(), params); } /** Returns a 2-column list with command names and the first line of their header or (if absent) description.
/** Returns the text displayed before the option list; an empty string if there are no options, * otherwise the result of {@code String.format(optionListHeading, params)}. * @param params the parameters to use to format the option list heading * @return the formatted option list heading */ public String optionListHeading(Object... params) { return commandSpec.optionsMap().isEmpty() ? "" : heading(ansi(), width(), commandSpec.usageMessage().optionListHeading(), params); }
/** Returns the {@code IHelpFactory} that is used to construct the usage help message. * @see #setHelpFactory(IHelpFactory) * @since 3.9 */ public IHelpFactory getHelpFactory() { return getCommandSpec().usageMessage().helpFactory(); }
/** Returns the text displayed before the positional parameter list; an empty string if there are no positional * parameters, otherwise the result of {@code String.format(parameterListHeading, params)}. * @param params the parameters to use to format the parameter list heading * @return the formatted parameter list heading */ public String parameterListHeading(Object... params) { return commandSpec.positionalParameters().isEmpty() ? "" : heading(ansi(), width(), commandSpec.usageMessage().parameterListHeading(), params); }
/** Returns the text displayed before the synopsis text; the result of {@code String.format(synopsisHeading, params)}. * @param params the parameters to use to format the synopsis heading * @return the formatted synopsis heading */ public String synopsisHeading(Object... params) { return heading(ansi(), width(), commandSpec.usageMessage().synopsisHeading(), params); }
private void setBundle(String bundleBaseName, ResourceBundle bundle) { usageMessage().messages(new Messages(this, bundleBaseName, bundle)); updateArgSpecMessages(); } private void updateArgSpecMessages() {
/** * Returns the map of section keys and renderers used to construct the usage help message. * The usage help message can be customized by adding, replacing and removing section renderers from this map. * Sections can be reordered with {@link #setHelpSectionKeys(List) setSectionKeys}. * Sections that are either not in this map or not in the list returned by {@link #getHelpSectionKeys() getSectionKeys} are omitted. * <p> * NOTE: By modifying the returned {@code Map}, only the usage help message <em>of this command</em> is affected. * Use {@link #setHelpSectionMap(Map)} to customize the usage help message for this command <em>and all subcommands</em>. * </p> * @since 3.9 */ public Map<String, IHelpSectionRenderer> getHelpSectionMap() { return getCommandSpec().usageMessage().sectionMap(); }