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())); }
sep = appendClassName(pw, sep, indent, "defaultValueProvider = %s", spec.defaultValueProvider()); sep = append(pw, sep, indent, "resourceBundle = \"%s\"", spec.resourceBundleBaseName(), "null"); sep = append(pw, sep, indent, "sortOptions = %s", spec.usageMessage().sortOptions(), true); sep = append(pw, sep, indent, "hidden = %s", spec.usageMessage().hidden(), false); sep = append(pw, sep, indent, "helpCommand = %s", spec.helpCommand(), false);
assertTrue(help.commandSpec().usageMessage().abbreviateSynopsis()); assertTrue(help.commandSpec().usageMessage().showDefaultValues()); assertFalse(help.commandSpec().usageMessage().sortOptions()); assertEquals(";", help.commandSpec().parser().separator()); assertEquals('&', help.commandSpec().usageMessage().requiredOptionMarker());
assertTrue(help.commandSpec().usageMessage().abbreviateSynopsis()); assertTrue(help.commandSpec().usageMessage().showDefaultValues()); assertFalse(help.commandSpec().usageMessage().sortOptions()); assertEquals(";", help.commandSpec().parser().separator()); assertEquals('&', help.commandSpec().usageMessage().requiredOptionMarker());
assertTrue(help.commandSpec().usageMessage().abbreviateSynopsis()); assertTrue(help.commandSpec().usageMessage().showDefaultValues()); assertFalse(help.commandSpec().usageMessage().sortOptions()); assertEquals(":", help.commandSpec().parser().separator()); assertEquals('%', help.commandSpec().usageMessage().requiredOptionMarker());
assertArrayEquals(new String[] {"Mixin custom synopsis"}, commandSpec.usageMessage().customSynopsis()); assertTrue("showDefaultValues", commandSpec.usageMessage().showDefaultValues()); assertFalse("sortOptions", commandSpec.usageMessage().sortOptions());
assertTrue(help.commandSpec().usageMessage().abbreviateSynopsis()); assertTrue(help.commandSpec().usageMessage().showDefaultValues()); assertFalse(help.commandSpec().usageMessage().sortOptions());
void initFromMixin(UsageMessageSpec mixin, CommandSpec commandSpec) { if (initializable(synopsisHeading, mixin.synopsisHeading(), DEFAULT_SYNOPSIS_HEADING)) {synopsisHeading = mixin.synopsisHeading();} if (initializable(commandListHeading, mixin.commandListHeading(), DEFAULT_COMMAND_LIST_HEADING)) {commandListHeading = mixin.commandListHeading();} if (initializable(requiredOptionMarker, mixin.requiredOptionMarker(), DEFAULT_REQUIRED_OPTION_MARKER)) {requiredOptionMarker = mixin.requiredOptionMarker();} if (initializable(abbreviateSynopsis, mixin.abbreviateSynopsis(), DEFAULT_ABBREVIATE_SYNOPSIS)) {abbreviateSynopsis = mixin.abbreviateSynopsis();} if (initializable(sortOptions, mixin.sortOptions(), DEFAULT_SORT_OPTIONS)) {sortOptions = mixin.sortOptions();} if (initializable(showDefaultValues, mixin.showDefaultValues(), DEFAULT_SHOW_DEFAULT_VALUES)) {showDefaultValues = mixin.showDefaultValues();} if (initializable(hidden, mixin.hidden(), DEFAULT_HIDDEN)) {hidden = mixin.hidden();} if (initializable(customSynopsis, mixin.customSynopsis(), DEFAULT_MULTI_LINE)) {customSynopsis = mixin.customSynopsis().clone();} if (initializable(description, mixin.description(), DEFAULT_MULTI_LINE)) {description = mixin.description().clone();} if (initializable(descriptionHeading, mixin.descriptionHeading(), DEFAULT_SINGLE_VALUE)) {descriptionHeading = mixin.descriptionHeading();} if (initializable(header, mixin.header(), DEFAULT_MULTI_LINE)) {header = mixin.header().clone();} if (initializable(headerHeading, mixin.headerHeading(), DEFAULT_SINGLE_VALUE)) {headerHeading = mixin.headerHeading();} if (initializable(footer, mixin.footer(), DEFAULT_MULTI_LINE)) {footer = mixin.footer().clone();} if (initializable(footerHeading, mixin.footerHeading(), DEFAULT_SINGLE_VALUE)) {footerHeading = mixin.footerHeading();} if (initializable(parameterListHeading, mixin.parameterListHeading(), DEFAULT_SINGLE_VALUE)) {parameterListHeading = mixin.parameterListHeading();} if (initializable(optionListHeading, mixin.optionListHeading(), DEFAULT_SINGLE_VALUE)) {optionListHeading = mixin.optionListHeading();} if (Messages.empty(messages)) { messages(Messages.copy(commandSpec, mixin.messages())); } } void initFrom(UsageMessageSpec settings, CommandSpec commandSpec) {
@Test public void testUsageHelp_abbreviateSynopsisWithPositional() throws UnsupportedEncodingException { CommandSpec spec = CommandSpec.create(); spec.usageMessage().abbreviateSynopsis(true).requiredOptionMarker('!').sortOptions(false); spec.addOption(OptionSpec.builder("-x").required(true).description("required").build()); spec.addPositional(PositionalParamSpec.builder().arity("1").paramLabel("POSITIONAL").description("positional").build()); CommandLine commandLine = new CommandLine(spec); String actual = usageString(commandLine, Ansi.OFF); String expected = String.format("" + "Usage: <main class> [OPTIONS] POSITIONAL...%n" + "! POSITIONAL... positional%n" + "! -x required%n"); assertEquals(expected, actual); }
@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); }
/** * <p>Returns a description of the {@linkplain Option options} supported by the application. * This implementation {@linkplain #createShortOptionNameComparator() sorts options alphabetically}, and shows * only the {@linkplain Option#hidden() non-hidden} options in a {@linkplain TextTable tabular format} * using the {@linkplain #createDefaultOptionRenderer() default renderer} and {@linkplain Layout default layout}.</p> * @return the fully formatted option list * @see #optionList(Layout, Comparator, IParamLabelRenderer) */ public String optionList() { Comparator<OptionSpec> sortOrder = commandSpec.usageMessage().sortOptions() ? createShortOptionNameComparator() : createOrderComparatorIfNecessary(commandSpec.options()); return optionList(createLayout(calcLongOptionColumnWidth()), sortOrder, parameterLabelRenderer()); }
assertTrue(help.commandSpec().usageMessage().abbreviateSynopsis()); assertTrue(help.commandSpec().usageMessage().showDefaultValues()); assertFalse(help.commandSpec().usageMessage().sortOptions()); assertEquals(":", help.commandSpec().parser().separator()); assertEquals('%', help.commandSpec().usageMessage().requiredOptionMarker());
assertArrayEquals(new String[] {"Mixin custom synopsis"}, commandSpec.usageMessage().customSynopsis()); assertTrue("showDefaultValues", commandSpec.usageMessage().showDefaultValues()); assertFalse("sortOptions", commandSpec.usageMessage().sortOptions());
assertTrue(help.commandSpec().usageMessage().abbreviateSynopsis()); assertTrue(help.commandSpec().usageMessage().showDefaultValues()); assertFalse(help.commandSpec().usageMessage().sortOptions());
void initFromMixin(UsageMessageSpec mixin, CommandSpec commandSpec) { if (initializable(synopsisHeading, mixin.synopsisHeading(), DEFAULT_SYNOPSIS_HEADING)) {synopsisHeading = mixin.synopsisHeading();} if (initializable(commandListHeading, mixin.commandListHeading(), DEFAULT_COMMAND_LIST_HEADING)) {commandListHeading = mixin.commandListHeading();} if (initializable(requiredOptionMarker, mixin.requiredOptionMarker(), DEFAULT_REQUIRED_OPTION_MARKER)) {requiredOptionMarker = mixin.requiredOptionMarker();} if (initializable(abbreviateSynopsis, mixin.abbreviateSynopsis(), DEFAULT_ABBREVIATE_SYNOPSIS)) {abbreviateSynopsis = mixin.abbreviateSynopsis();} if (initializable(sortOptions, mixin.sortOptions(), DEFAULT_SORT_OPTIONS)) {sortOptions = mixin.sortOptions();} if (initializable(showDefaultValues, mixin.showDefaultValues(), DEFAULT_SHOW_DEFAULT_VALUES)) {showDefaultValues = mixin.showDefaultValues();} if (initializable(hidden, mixin.hidden(), DEFAULT_HIDDEN)) {hidden = mixin.hidden();} if (initializable(customSynopsis, mixin.customSynopsis(), DEFAULT_MULTI_LINE)) {customSynopsis = mixin.customSynopsis().clone();} if (initializable(description, mixin.description(), DEFAULT_MULTI_LINE)) {description = mixin.description().clone();} if (initializable(descriptionHeading, mixin.descriptionHeading(), DEFAULT_SINGLE_VALUE)) {descriptionHeading = mixin.descriptionHeading();} if (initializable(header, mixin.header(), DEFAULT_MULTI_LINE)) {header = mixin.header().clone();} if (initializable(headerHeading, mixin.headerHeading(), DEFAULT_SINGLE_VALUE)) {headerHeading = mixin.headerHeading();} if (initializable(footer, mixin.footer(), DEFAULT_MULTI_LINE)) {footer = mixin.footer().clone();} if (initializable(footerHeading, mixin.footerHeading(), DEFAULT_SINGLE_VALUE)) {footerHeading = mixin.footerHeading();} if (initializable(parameterListHeading, mixin.parameterListHeading(), DEFAULT_SINGLE_VALUE)) {parameterListHeading = mixin.parameterListHeading();} if (initializable(optionListHeading, mixin.optionListHeading(), DEFAULT_SINGLE_VALUE)) {optionListHeading = mixin.optionListHeading();} if (Messages.empty(messages)) { messages(Messages.copy(commandSpec, mixin.messages())); } } void initFrom(UsageMessageSpec settings, CommandSpec commandSpec) {
@Test public void testUsageHelp_abbreviateSynopsisWithPositional() throws UnsupportedEncodingException { CommandSpec spec = CommandSpec.create(); spec.usageMessage().abbreviateSynopsis(true).requiredOptionMarker('!').sortOptions(false); spec.addOption(OptionSpec.builder("-x").required(true).description("required").build()); spec.addPositional(PositionalParamSpec.builder().arity("1").paramLabel("POSITIONAL").description("positional").build()); CommandLine commandLine = new CommandLine(spec); String actual = usageString(commandLine, Ansi.OFF); String expected = String.format("" + "Usage: <main class> [OPTIONS] POSITIONAL...%n" + "! POSITIONAL... positional%n" + "! -x required%n"); assertEquals(expected, actual); }
/** * <p>Returns a description of the {@linkplain Option options} supported by the application. * This implementation {@linkplain #createShortOptionNameComparator() sorts options alphabetically}, and shows * only the {@linkplain Option#hidden() non-hidden} options in a {@linkplain TextTable tabular format} * using the {@linkplain #createDefaultOptionRenderer() default renderer} and {@linkplain Layout default layout}.</p> * @return the fully formatted option list * @see #optionList(Layout, Comparator, IParamLabelRenderer) */ public String optionList() { Comparator<OptionSpec> sortOrder = commandSpec.usageMessage().sortOptions() ? createShortOptionNameComparator() : createOrderComparatorIfNecessary(commandSpec.options()); return optionList(createLayout(calcLongOptionColumnWidth()), sortOrder, parameterLabelRenderer()); }
@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); }