@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 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()); }
@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
@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()); }
@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
@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); }
/** 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); }
/** 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); }