@Test public void testUsageWithCustomColorScheme() throws UnsupportedEncodingException { CommandLine.Help.ColorScheme scheme = new CommandLine.Help.ColorScheme(Ansi.ON) .options(Ansi.Style.bg_magenta).parameters(Ansi.Style.bg_cyan).optionParams(Ansi.Style.bg_yellow).commands(Ansi.Style.reverse); class Args { @CommandLine.Parameters(description = "param desc") String[] params; @CommandLine.Option(names = "-x", description = "option desc") String[] options; } ByteArrayOutputStream baos = new ByteArrayOutputStream(); CommandLine.usage(new Args(), new PrintStream(baos, true, "UTF8"), scheme); String actual = baos.toString("UTF8"); String expected = String.format("" + "Usage: @|reverse <main class>|@ [@|bg_magenta -x|@=@|bg_yellow <options>|@]... [@|bg_cyan <params>|@...]%n" + " [@|bg_cyan <params>|@...] param desc%n" + " @|bg_magenta -x|@=@|bg_yellow <|@@|bg_yellow options>|@ option desc%n"); assertEquals(Ansi.ON.new Text(expected).toString(), actual); }
@Test public void testCreateDescriptionFirstLines() throws Exception { Method m = Help.class.getDeclaredMethod("createDescriptionFirstLines", Help.ColorScheme.class, Model.ArgSpec.class, String[].class, boolean[].class); m.setAccessible(true); String[][] input = new String[][] { new String[0], new String[] {""}, new String[] {"a", "b", "c"} }; Help.Ansi.Text[][] expectedOutput = new Help.Ansi.Text[][] { new Help.Ansi.Text[] {Help.Ansi.OFF.text("")}, new Help.Ansi.Text[] {Help.Ansi.OFF.text("")}, new Help.Ansi.Text[] {Help.Ansi.OFF.text("a"), Help.Ansi.OFF.text("b"), Help.Ansi.OFF.text("c")} }; for (int i = 0; i < input.length; i++) { String[] description = input[i]; Help.Ansi.Text[] result = (Help.Ansi.Text[]) m.invoke(null, new Help.ColorScheme(Help.Ansi.OFF), null, description, new boolean[3]); Help.Ansi.Text[] expected = expectedOutput[i]; for (int j = 0; j < result.length; j++) { assertEquals(expected[j], result[j]); } } }
@Test public void testPrintHelpIfRequestedWithCustomColorScheme() { ColorScheme customColorScheme = new Help.ColorScheme(Help.Ansi.ON) .optionParams(Style.fg_magenta) .commands(Style.bg_cyan) .options(Style.fg_green) .parameters(Style.bg_white); @Command(mixinStandardHelpOptions = true) class App { @Option(names = { "-f" }, paramLabel = "ARCHIVE", description = "the archive file") File archive; @Parameters(paramLabel = "POSITIONAL", description = "positional arg") String arg; } List<CommandLine> list = new CommandLine(new App()).parse("--help"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); final PrintStream out = new PrintStream(baos); assertTrue(CommandLine.printHelpIfRequested(list, out, out, customColorScheme)); String expected = Help.Ansi.ON.string(String.format("" + "Usage: @|bg_cyan <main class>|@ [@|green -hV|@] [@|green -f|@=@|magenta ARCHIVE|@] @|bg_white POSITIONAL|@%n" + "@|bg_white |@ @|bg_white POSITIONAL|@ positional arg%n" + " @|green -f|@=@|magenta A|@@|magenta RCHIVE|@ the archive file%n" + " @|green -h|@, @|green --help|@ Show this help message and exit.%n" + " @|green -V|@, @|green --version|@ Print version information and exit.%n")); assertEquals(expected, baos.toString()); }
/** Creates and returns a new {@link ColorScheme} initialized with picocli default values: commands are bold, * options and parameters use a yellow foreground, and option parameters use italic. * @param ansi whether the usage help message should contain ANSI escape codes or not * @return a new default color scheme */ public static ColorScheme defaultColorScheme(Ansi ansi) { return new ColorScheme(ansi) .commands(Style.bold) .options(Style.fg_yellow) .parameters(Style.fg_yellow) .optionParams(Style.italic); }
@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 testColorSchemeDefaultConstructorHasAnsiAuto() { CommandLine.Help.ColorScheme colorScheme = new CommandLine.Help.ColorScheme(); assertEquals(Ansi.AUTO, colorScheme.ansi()); }
@Test public void testMinimalParameterRenderer() { Help.MinimalParameterRenderer renderer = new Help.MinimalParameterRenderer(); Text[][] texts = renderer.render(PositionalParamSpec.builder().build(), Help.createMinimalParamLabelRenderer(), new ColorScheme()); assertEquals("", texts[0][1].plainString()); }
@Test public void testSystemPropertiesOverrideExplicitColorScheme() { @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; } Ansi ansi = Ansi.ON; CommandLine.Help.ColorScheme explicit = new CommandLine.Help.ColorScheme(ansi) .commands(Ansi.Style.faint, Ansi.Style.bg_magenta) .options(Ansi.Style.bg_red) .parameters(Ansi.Style.reverse) .optionParams(Ansi.Style.bg_green); // default color scheme assertEquals(ansi.new Text("@|faint,bg(magenta) <main class>|@ [@|bg(red) -v|@] [@|bg(red) -c|@=@|bg(green) <count>|@] @|reverse FILE|@..." + LINESEP), new CommandLine.Help(CommandLine.Model.CommandSpec.forAnnotatedObject(new App(), CommandLine.defaultFactory()), explicit).synopsis(0)); System.setProperty("picocli.color.commands", "blue"); System.setProperty("picocli.color.options", "blink"); System.setProperty("picocli.color.parameters", "red"); System.setProperty("picocli.color.optionParams", "magenta"); assertEquals(ansi.new Text("@|blue <main class>|@ [@|blink -v|@] [@|blink -c|@=@|magenta <count>|@] @|red FILE|@..." + LINESEP), new CommandLine.Help(CommandLine.Model.CommandSpec.forAnnotatedObject(new App(), CommandLine.defaultFactory()), explicit).synopsis(0)); } @Test
/** Creates and returns a new {@link ColorScheme} initialized with picocli default values: commands are bold, * options and parameters use a yellow foreground, and option parameters use italic. * @param ansi whether the usage help message should contain ANSI escape codes or not * @return a new default color scheme */ public static ColorScheme defaultColorScheme(Ansi ansi) { return new ColorScheme(ansi) .commands(Style.bold) .options(Style.fg_yellow) .parameters(Style.fg_yellow) .optionParams(Style.italic); }
@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); }
@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 testMinimalParameterRenderer() { Help.MinimalParameterRenderer renderer = new Help.MinimalParameterRenderer(); Text[][] texts = renderer.render(PositionalParamSpec.builder().build(), Help.createMinimalParamLabelRenderer(), new ColorScheme()); assertEquals("", texts[0][1].plainString()); }
@Test public void testLayoutConstructorCreatesDefaultColumns() { ColorScheme colorScheme = new ColorScheme(); Help.Layout layout = new Help.Layout(colorScheme, 99); TextTable expected = TextTable.forDefaultColumns(Help.Ansi.OFF, 99); 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); } }
@SuppressWarnings("deprecation") @Test public void testAddSubcommand() { @Command(name = "app", mixinStandardHelpOptions = true) class App { } Help help = new Help(new CommandLine(CommandSpec.create()).getCommandSpec(), new Help.ColorScheme(Help.Ansi.OFF)); help.addSubcommand("boo", new App()); assertEquals(1, help.subcommands().size()); assertEquals("app", help.subcommands().get("boo").commandSpec().name()); }
@Test public void testAddAllSubcommands() { Help help = new Help(CommandSpec.create(), new Help.ColorScheme(Help.Ansi.OFF)); help.addAllSubcommands(null); assertTrue(help.subcommands().isEmpty()); } @SuppressWarnings("deprecation")
@Test public void testMinimalOptionRenderer() { Help.MinimalOptionRenderer renderer = new Help.MinimalOptionRenderer(); Text[][] texts = renderer.render(OptionSpec.builder("-x").build(), Help.createMinimalParamLabelRenderer(), new ColorScheme()); assertEquals("", texts[0][1].plainString()); }
@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 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 testColorSchemeDefaultConstructorHasAnsiAuto() { CommandLine.Help.ColorScheme colorScheme = new CommandLine.Help.ColorScheme(); assertEquals(Ansi.AUTO, colorScheme.ansi()); }