Skip to content

Commit

Permalink
[#433] added test and fix bug in new method printHelpIfRequested th…
Browse files Browse the repository at this point in the history
…at accepts a `ColorScheme` parameter.

Closes #433
  • Loading branch information
remkop committed Aug 18, 2018
1 parent ab81874 commit 19a3524
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
7 changes: 4 additions & 3 deletions src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,7 @@ public static boolean printHelpIfRequested(ParseResult parseResult) {
* @see IHelpCommandInitializable
* @since 3.0 */
public static boolean printHelpIfRequested(List<CommandLine> parsedCommands, PrintStream out, PrintStream err, Help.Ansi ansi) {
return printHelpIfRequested(parsedCommands, out, err, new Help.ColorScheme(ansi));
return printHelpIfRequested(parsedCommands, out, err, Help.defaultColorScheme(ansi));
}
/**
* Helper method that may be useful when processing the list of {@code CommandLine} objects that result from successfully
Expand Down Expand Up @@ -8242,10 +8242,11 @@ public static class ColorScheme {
public final List<IStyle> optionParamStyles = new ArrayList<IStyle>();
private final Ansi ansi;

/** Constructs a new ColorScheme with {@link Help.Ansi#AUTO}. */
/** Constructs a new empty ColorScheme with {@link Help.Ansi#AUTO}. */
public ColorScheme() { this(Ansi.AUTO); }

/** Constructs a new ColorScheme with the specified Ansi enabled mode.
/** Constructs a new empty ColorScheme with the specified Ansi enabled mode.
* @see Help#defaultColorScheme(Ansi)
* @param ansi whether to emit ANSI escape codes or not
*/
public ColorScheme(Ansi ansi) {this.ansi = Assert.notNull(ansi, "ansi"); }
Expand Down
19 changes: 13 additions & 6 deletions src/test/java/picocli/CommandLineHelpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3090,22 +3090,29 @@ class App {
}

@Test
public void testPrintHelpIfRequestedCustomColorScheme() throws IOException {
ColorScheme customColorScheme = Help.defaultColorScheme(Help.Ansi.ON).optionParams(Style.fg_magenta);
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 = String.format("Usage: \u001B[1m<main class>\u001B[21m\u001B[0m [\u001B[33m-hV\u001B[39m\u001B[0m] [\u001B[33m-f\u001B[39m\u001B[0m=\u001B[3m\u001B[35mARCHIVE\u001B[39m\u001B[23m\u001B[0m]%n" +
" \u001B[33m-f\u001B[39m\u001B[0m= \u001B[3m\u001B[35mA\u001B[39m\u001B[23m\u001B[0m\u001B[3m\u001B[35mRCHIVE\u001B[39m\u001B[23m\u001B[0m the archive file%n" +
" \u001B[33m-h\u001B[39m\u001B[0m, \u001B[33m--help\u001B[39m\u001B[0m Show this help message and exit.%n" +
" \u001B[33m-V\u001B[39m\u001B[0m, \u001B[33m--version\u001B[39m\u001B[0m Print version information and exit.%n");
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());
}

Expand Down

0 comments on commit 19a3524

Please sign in to comment.