private void printUsageMessage(UsageMessageSpec usageMessage, PrintWriter pw, String indent) { pw.printf("%sUsageMessageSpec:%n", indent); indent += " "; pw.printf("%swidth: %s%n", indent, usageMessage.width()); pw.printf("%sabbreviateSynopsis: %s%n", indent, usageMessage.abbreviateSynopsis()); pw.printf("%shidden: %s%n", indent, usageMessage.hidden()); pw.printf("%sshowDefaultValues: %s%n", indent, usageMessage.showDefaultValues()); pw.printf("%ssortOptions: %s%n", indent, usageMessage.sortOptions()); pw.printf("%srequiredOptionMarker: '%s'%n", indent, usageMessage.requiredOptionMarker()); pw.printf("%sheaderHeading: '%s'%n", indent, usageMessage.headerHeading()); pw.printf("%sheader: %s%n", indent, Arrays.toString(usageMessage.header())); pw.printf("%ssynopsisHeading: '%s'%n", indent, usageMessage.synopsisHeading()); pw.printf("%scustomSynopsis: %s%n", indent, Arrays.toString(usageMessage.customSynopsis())); pw.printf("%sdescriptionHeading: '%s'%n", indent, usageMessage.descriptionHeading()); pw.printf("%sdescription: %s%n", indent, Arrays.toString(usageMessage.description())); pw.printf("%sparameterListHeading: '%s'%n", indent, usageMessage.parameterListHeading()); pw.printf("%soptionListHeading: '%s'%n", indent, usageMessage.optionListHeading()); pw.printf("%scommandListHeading: '%s'%n", indent, usageMessage.commandListHeading()); pw.printf("%sfooterHeading: '%s'%n", indent, usageMessage.footerHeading()); pw.printf("%sfooter: %s%n", indent, Arrays.toString(usageMessage.footer())); }
@Test public void testTooSmallUsageWidthPropertyValue() throws UnsupportedEncodingException { PrintStream originalErr = System.err; ByteArrayOutputStream baos = new ByteArrayOutputStream(2500); System.setErr(new PrintStream(baos)); System.clearProperty("picocli.trace"); System.setProperty("picocli.usage.width", "54"); int actual = new UsageMessageSpec().width(); System.setErr(originalErr); System.clearProperty("picocli.usage.width"); assertEquals(55, actual); assertEquals(format("[picocli WARN] Invalid picocli.usage.width value 54. Using minimum usage width 55.%n"), baos.toString("UTF-8")); }
@Test public void testInvalidUsageWidthPropertyValue() throws UnsupportedEncodingException { PrintStream originalErr = System.err; ByteArrayOutputStream baos = new ByteArrayOutputStream(2500); System.setErr(new PrintStream(baos)); System.clearProperty("picocli.trace"); System.setProperty("picocli.usage.width", "INVALID"); int actual = new UsageMessageSpec().width(); System.setErr(originalErr); System.clearProperty("picocli.usage.width"); assertEquals(80, actual); assertEquals(format("[picocli WARN] Invalid picocli.usage.width value 'INVALID'. Using usage width 80.%n"), baos.toString("UTF-8")); }
@Test public void testShouldGetUsageWidthFromSystemProperties() { int defaultWidth = new UsageMessageSpec().width(); assertEquals(80, defaultWidth); try { System.setProperty("picocli.usage.width", "123"); int width = new UsageMessageSpec().width(); assertEquals(123, width); } finally { System.setProperty("picocli.usage.width", String.valueOf(defaultWidth)); } }
sep = append(pw, sep, indent, "helpCommand = %s", spec.helpCommand(), false); sep = append(pw, sep, indent, "separator = \"%s\"", spec.parser().separator(), "="); sep = append(pw, sep, indent, "usageHelpWidth = %s", spec.usageMessage().width(), 80);
void updateFromCommand(Command cmd, CommandSpec commandSpec) { if (isNonDefault(cmd.synopsisHeading(), DEFAULT_SYNOPSIS_HEADING)) {synopsisHeading = cmd.synopsisHeading();} if (isNonDefault(cmd.commandListHeading(), DEFAULT_COMMAND_LIST_HEADING)) {commandListHeading = cmd.commandListHeading();} if (isNonDefault(cmd.requiredOptionMarker(), DEFAULT_REQUIRED_OPTION_MARKER)) {requiredOptionMarker = cmd.requiredOptionMarker();} if (isNonDefault(cmd.abbreviateSynopsis(), DEFAULT_ABBREVIATE_SYNOPSIS)) {abbreviateSynopsis = cmd.abbreviateSynopsis();} if (isNonDefault(cmd.sortOptions(), DEFAULT_SORT_OPTIONS)) {sortOptions = cmd.sortOptions();} if (isNonDefault(cmd.showDefaultValues(), DEFAULT_SHOW_DEFAULT_VALUES)) {showDefaultValues = cmd.showDefaultValues();} if (isNonDefault(cmd.hidden(), DEFAULT_HIDDEN)) {hidden = cmd.hidden();} if (isNonDefault(cmd.customSynopsis(), DEFAULT_MULTI_LINE)) {customSynopsis = cmd.customSynopsis().clone();} if (isNonDefault(cmd.description(), DEFAULT_MULTI_LINE)) {description = cmd.description().clone();} if (isNonDefault(cmd.descriptionHeading(), DEFAULT_SINGLE_VALUE)) {descriptionHeading = cmd.descriptionHeading();} if (isNonDefault(cmd.header(), DEFAULT_MULTI_LINE)) {header = cmd.header().clone();} if (isNonDefault(cmd.headerHeading(), DEFAULT_SINGLE_VALUE)) {headerHeading = cmd.headerHeading();} if (isNonDefault(cmd.footer(), DEFAULT_MULTI_LINE)) {footer = cmd.footer().clone();} if (isNonDefault(cmd.footerHeading(), DEFAULT_SINGLE_VALUE)) {footerHeading = cmd.footerHeading();} if (isNonDefault(cmd.parameterListHeading(), DEFAULT_SINGLE_VALUE)) {parameterListHeading = cmd.parameterListHeading();} if (isNonDefault(cmd.optionListHeading(), DEFAULT_SINGLE_VALUE)) {optionListHeading = cmd.optionListHeading();} if (isNonDefault(cmd.usageHelpWidth(), DEFAULT_USAGE_WIDTH)) {width(cmd.usageHelpWidth());} // validate if (!empty(cmd.resourceBundle())) { // else preserve superclass bundle messages(new Messages(commandSpec, cmd.resourceBundle())); } } void initFromMixin(UsageMessageSpec mixin, CommandSpec commandSpec) {
private int width() { return commandSpec.usageMessage().width(); } /** Returns command custom synopsis as a string. A custom synopsis can be zero or more lines, and can be
/** Sets the maximum width of the usage help message. Longer lines are wrapped. * <p>The specified setting will be registered with this {@code CommandLine} and the full hierarchy of its * subcommands and nested sub-subcommands <em>at the moment this method is called</em>. Subcommands added * later will have the default setting. To ensure a setting is applied to all * subcommands, call the setter last, after adding subcommands.</p> * @param width the maximum width of the usage help message * @see UsageMessageSpec#width(int) * @return this {@code CommandLine} object, to allow method chaining */ public CommandLine setUsageHelpWidth(int width) { getCommandSpec().usageMessage().width(width); for (CommandLine command : getCommandSpec().subcommands().values()) { command.setUsageHelpWidth(width); } return this; }
@Test public void testUsageWidthFromSystemPropertyOverridesCommandAttribute() { @Command(usageHelpWidth = 60, description = "0123456789012345678901234567890123456789012345678901234567890123456789") class App {} System.setProperty("picocli.usage.width", "123"); try { CommandLine cmd = new CommandLine(new App()); assertEquals(123, cmd.getUsageHelpWidth()); assertEquals(123, cmd.getCommandSpec().usageMessage().width()); } finally { System.clearProperty("picocli.usage.width"); } }
public String render(Help help) { CommandSpec spec = help.commandSpec(); if (spec.subcommands().isEmpty()) { return ""; } // prepare layout: two columns // the left column overflows, the right column wraps if text is too long TextTable textTable = TextTable.forColumns(help.ansi(), new Column(15, 2, Overflow.SPAN), new Column(spec.usageMessage().width() - 15, 2, Overflow.WRAP)); for (CommandLine subcommand : spec.subcommands().values()) { addHierarchy(subcommand, textTable, ""); } return textTable.toString(); }
/** Returns the maximum width of the usage help message. The default is 80. * @see UsageMessageSpec#width() */ public int getUsageHelpWidth() { return getCommandSpec().usageMessage().width(); }
@Test(expected = InitializationException.class) public void testUsageHelp_width_setterDisallowsValuesBelow55() { new UsageMessageSpec().width(54); }
@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 testUsageHelp_width_configurableWithSystemProperty() { System.setProperty("picocli.usage.width", "67"); try { assertEquals(67, new UsageMessageSpec().width()); } finally { System.clearProperty("picocli.usage.width"); } }
@Test public void testUsageHelp_width_default80() { assertEquals(80, UsageMessageSpec.DEFAULT_USAGE_WIDTH); assertEquals(UsageMessageSpec.DEFAULT_USAGE_WIDTH, new UsageMessageSpec().width()); }
@Test public void testShouldGetUsageWidthFromSystemProperties() { int defaultWidth = new UsageMessageSpec().width(); assertEquals(80, defaultWidth); try { System.setProperty("picocli.usage.width", "123"); int width = new UsageMessageSpec().width(); assertEquals(123, width); } finally { System.setProperty("picocli.usage.width", String.valueOf(defaultWidth)); } }