@Test public void testSynopsisOrderCorrectWhenParametersDeclaredOutOfOrder() { class WithParams { @Parameters(index = "1") String param1; @Parameters(index = "0") String param0; } Help help = new Help(new WithParams()); assertEquals(format("<main class> <param0> <param1>%n"), help.synopsis(0)); }
@Test public void testSynopsisOrderCorrectWhenSubClassAddsParameters() { class BaseWithParams { @Parameters(index = "1") String param1; @Parameters(index = "0") String param0; } class SubWithParams extends BaseWithParams { @Parameters(index = "3") String param3; @Parameters(index = "2") String param2; } Help help = new Help(new SubWithParams()); assertEquals(format("<main class> <param0> <param1> <param2> <param3>%n"), help.synopsis(0)); }
@Test public void testDefaultOptionRenderer_rendersSpacePrefixByDefaultForRequiredOptionsWithoutDefaultValue() { class Example { @Option(names = {"-b", "-a", "--alpha"}, required = true, description = "other") String otherField; } Help help = new Help(new Example()); Help.IOptionRenderer renderer = help.createDefaultOptionRenderer(); Help.IParamLabelRenderer parameterRenderer = help.createDefaultParamLabelRenderer(); OptionSpec option = help.options().get(0); Text[][] row = renderer.render(option, parameterRenderer, help.colorScheme()); assertEquals(1, row.length); assertArrayEquals(Arrays.toString(row[0]), textArray(help, " ", "-b", ",", "-a, --alpha=<otherField>", "other"), row[0]); }
@Test public void testHelpCreateLayout_CreatesDefaultColumns() { Help help = new Help(CommandSpec.create(), new ColorScheme(Help.Ansi.OFF)); Help.Layout layout = help.createDefaultLayout(); TextTable expected = TextTable.forDefaultColumns(Help.Ansi.OFF, 80); assertEquals(expected.columns().length, layout.table.columns().length); for (int i = 0; i < expected.columns().length; i++) { assertEquals(expected.columns()[i].indent, layout.table.columns()[i].indent); assertEquals(expected.columns()[i].width, layout.table.columns()[i].width); assertEquals(expected.columns()[i].overflow, layout.table.columns()[i].overflow); } }
@Test public void testSynopsis_optionalOptionArity0_1__withSeparator() { class App { @Option(names = {"--verbose", "-v"}) boolean verbose; @Option(names = {"--count", "-c"}, arity = "0..1") int count; @Option(names = {"--help", "-h"}, hidden = true) boolean helpRequested; } Help help = new Help(new App(), Help.Ansi.OFF); assertEquals("<main class> [-v] [-c[=<count>]]" + LINESEP, help.synopsis(0)); }
@Test public void testSynopsis_withSeparator_withLabeledRequiredParameters_ANSI() { @CommandLine.Command(separator = "=") class App { @CommandLine.Option(names = {"--verbose", "-v"}) boolean verbose; @CommandLine.Option(names = {"--count", "-c"}) int count; @CommandLine.Option(names = {"--help", "-h"}, hidden = true) boolean helpRequested; @CommandLine.Parameters(paramLabel = "FILE", arity = "1..*") File[] files; } CommandLine.Help help = new CommandLine.Help(new App(), Ansi.ON); assertEquals(Ansi.ON.new Text("@|bold <main class>|@ [@|yellow -v|@] [@|yellow -c|@=@|italic <count>|@] @|yellow FILE|@..." + LINESEP), help.synopsis(0)); }
@Test public void testSynopsis_withSeparator_withLabeledRequiredParameters() { @CommandLine.Command(separator = "=") class App { @CommandLine.Option(names = {"--verbose", "-v"}) boolean verbose; @CommandLine.Option(names = {"--count", "-c"}) int count; @CommandLine.Option(names = {"--help", "-h"}, hidden = true) boolean helpRequested; @CommandLine.Parameters(paramLabel = "FILE", arity = "1..*") File[] files; } CommandLine.Help help = new CommandLine.Help(new App(), Ansi.OFF); assertEquals("<main class> [-v] [-c=<count>] FILE..." + LINESEP, help.synopsis(0)); }
@SuppressWarnings("deprecation") @Test public void testDetailedSynopsis() { Help help = new Help(CommandSpec.create(), new Help.ColorScheme(Help.Ansi.OFF)); String str = help.detailedSynopsis(new Help.SortByShortestOptionNameAlphabetically(), true); assertEquals(String.format("<main class>%n"), str); }
@Test public void testSynopsis_optionalOptionArity1_n_withDefaultSeparator_ANSI() { @Command() class App { @Option(names = {"--verbose", "-v"}) boolean verbose; @Option(names = {"--count", "-c"}, arity = "1..*") int count; @Option(names = {"--help", "-h"}, hidden = true) boolean helpRequested; } Help help = new Help(new App(), Help.Ansi.ON); assertEquals(Help.Ansi.ON.new Text("@|bold <main class>|@ [@|yellow -v|@] [@|yellow -c|@=@|italic <count>|@...]" + LINESEP), help.synopsis(0)); }
@Test public void testSynopsis_optionalOptionArity1_n_withDefaultSeparator() { @Command() class App { @Option(names = {"--verbose", "-v"}) boolean verbose; @Option(names = {"--count", "-c"}, arity = "1..*") int count; @Option(names = {"--help", "-h"}, hidden = true) boolean helpRequested; } Help help = new Help(new App(), Help.Ansi.OFF); assertEquals("<main class> [-v] [-c=<count>...]" + LINESEP, help.synopsis(0)); }
@SuppressWarnings("deprecation") @Test public void testSynopsis() { Help help = new Help(CommandSpec.create(), new Help.ColorScheme(Help.Ansi.OFF)); String actual = help.synopsis(); assertEquals(String.format("<main class>%n"), actual); }
@Test public void testSynopsis_clustersRequiredBooleanOptions() { @Command(separator = "=") class App { @Option(names = {"--verbose", "-v"}, required = true) boolean verbose; @Option(names = {"--aaaa", "-a"}, required = true) boolean aBoolean; @Option(names = {"--xxxx", "-x"}, required = true) Boolean xBoolean; @Option(names = {"--count", "-c"}, paramLabel = "COUNT") int count; } Help help = new Help(new App(), Help.Ansi.OFF); assertEquals("<main class> -avx [-c=COUNT]" + LINESEP, help.synopsis(0)); }
@Test public void testSynopsis_clustersBooleanOptions() { @Command(separator = "=") class App { @Option(names = {"--verbose", "-v"}) boolean verbose; @Option(names = {"--aaaa", "-a"}) boolean aBoolean; @Option(names = {"--xxxx", "-x"}) Boolean xBoolean; @Option(names = {"--count", "-c"}, paramLabel = "COUNT") int count; } Help help = new Help(new App(), Help.Ansi.OFF); assertEquals("<main class> [-avx] [-c=COUNT]" + LINESEP, help.synopsis(0)); }
@Test public void testSynopsis_optionalOptionArity0_n__withSeparator() { @Command(separator = "=") class App { @Option(names = {"--verbose", "-v"}) boolean verbose; @Option(names = {"--count", "-c"}, arity = "0..*") int count; @Option(names = {"--help", "-h"}, hidden = true) boolean helpRequested; } Help help = new Help(new App(), Help.Ansi.OFF); // NOTE Expected :<main class> [-v] [-c[=<count>]...] but arity=0 for int field is weird anyway... assertEquals("<main class> [-v] [-c[=<count>...]]" + LINESEP, help.synopsis(0)); }
@Test public void testSynopsis_optionalOptionArity0_1_withSpaceSeparator_ANSI() { @Command(separator = " ") class App { @Option(names = {"--verbose", "-v"}) boolean verbose; @Option(names = {"--count", "-c"}, arity = "0..1") int count; @Option(names = {"--help", "-h"}, hidden = true) boolean helpRequested; } Help help = new Help(new App(), Help.Ansi.ON); assertEquals(Help.Ansi.ON.new Text("@|bold <main class>|@ [@|yellow -v|@] [@|yellow -c|@ [@|italic <count>|@]]" + LINESEP), help.synopsis(0)); }
@Test public void testAbreviatedSynopsis_withoutParameters() { @Command(abbreviateSynopsis = true) class App { @Option(names = {"--verbose", "-v"}) boolean verbose; @Option(names = {"--count", "-c"}) int count; @Option(names = {"--help", "-h"}, hidden = true) boolean helpRequested; } Help help = new Help(new App(), Help.Ansi.OFF); assertEquals("<main class> [OPTIONS]" + LINESEP, help.synopsis(0)); }
@Test public void testAbbreviatedSynopsis() { CommandSpec spec = CommandSpec.create(); spec.addPositional(PositionalParamSpec.builder().paramLabel("a").hidden(true).build()); spec.addPositional(PositionalParamSpec.builder().paramLabel("b").build()); Help help = new Help(spec, new Help.ColorScheme(Help.Ansi.OFF)); String actual = help.abbreviatedSynopsis(); assertEquals(String.format("<main class> b...%n"), actual); }
@Test public void testHelpCreateDetailedSynopsisOptionsText() { Help help = new Help(CommandSpec.create().addOption(OptionSpec.builder("xx").build()), new ColorScheme(Help.Ansi.OFF)); Text text = help.createDetailedSynopsisOptionsText(null, true); assertEquals(" [xx]", text.toString()); }
@Test public void testSynopsis_optionalOptionArity0_1_withSpaceSeparator() { @Command(separator = " ") class App { @Option(names = {"--verbose", "-v"}) boolean verbose; @Option(names = {"--count", "-c"}, arity = "0..1") int count; @Option(names = {"--help", "-h"}, hidden = true) boolean helpRequested; } Help help = new Help(new App(), Help.Ansi.OFF); assertEquals("<main class> [-v] [-c [<count>]]" + LINESEP, help.synopsis(0)); }
@Test public void testSynopsis_withSeparator_withLabeledParameters() { @CommandLine.Command(separator = "=") class App { @CommandLine.Option(names = {"--verbose", "-v"}) boolean verbose; @CommandLine.Option(names = {"--count", "-c"}) int count; @CommandLine.Option(names = {"--help", "-h"}, hidden = true) boolean helpRequested; @CommandLine.Parameters(paramLabel = "FILE") File[] files; } CommandLine.Help help = new CommandLine.Help(new App(), Ansi.OFF); assertEquals("<main class> [-v] [-c=<count>] [FILE...]" + LINESEP, help.synopsis(0)); }