Skip to content

Commit

Permalink
#164 Support format patterns in version string and printVersionHelp
Browse files Browse the repository at this point in the history
Closes #164
  • Loading branch information
remkop committed Aug 14, 2017
1 parent 70a282b commit 0e63404
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
== 1.0.0 - (Work in progress) Public release

* (Work in progress) #121 Added support for command line complete: picocli can generate bash completion scripts that generate completion matches based on `@Option` and `@Command` annotations.
* #164 Support format patterns in version string and printVersionHelp
* #166 Fixed bug where adjacent markup sections resulted in incorrect ANSI escape sequences

== 0.9.8 - Bugfix and enhancements release for public review. API may change.
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,24 @@ public void printVersionHelp(PrintStream out, Help.Ansi ansi) {
out.println(ansi.new Text(versionInfo));
}
}
/**
* Prints version information from the {@link Command#version()} annotation to the specified {@code PrintStream}.
* Each element of the array of version strings is {@linkplain String#format(String, Object...) formatted} with the
* specified parameters, and printed on a separate line. Both version strings and parameters may contain
* <a href="http://picocli.info/#_usage_help_with_styles_and_colors">markup for colors and style</a>.
* @param out the printStream to print to
* @param ansi whether the usage message should include ANSI escape codes or not
* @param params Arguments referenced by the format specifiers in the version strings
* @see Command#version()
* @see Option#versionHelp()
* @see #isVersionHelpRequested()
* @since 1.0.0
*/
public void printVersionHelp(PrintStream out, Help.Ansi ansi, Object... params) {
for (String versionInfo : versionLines) {
out.println(ansi.new Text(String.format(versionInfo, params)));
}
}

/**
* Delegates to {@link #run(Runnable, PrintStream, Help.Ansi, String...)} with {@link Help.Ansi#AUTO}.
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/picocli/CommandLineHelpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1630,4 +1630,31 @@ class Versioned {}
"\u001B[34mBuild 12345\u001B[39m\u001B[0m%n" +
"\u001B[31m\u001B[47m(c) 2017\u001B[49m\u001B[39m\u001B[0m%n"), result);
}
@Test
public void testCommandLine_printVersionInfo_formatsArguments() throws Exception {
@Command(version = {"First line %1$s", "Second line %2$s", "Third line %s %s"}) class Versioned {}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos, true, "UTF8");
new CommandLine(new Versioned()).printVersionHelp(ps, Help.Ansi.OFF, "VALUE1", "VALUE2", "VALUE3");
String result = baos.toString("UTF8");
assertEquals(String.format("First line VALUE1%nSecond line VALUE2%nThird line VALUE1 VALUE2%n"), result);
}

@Test
public void testCommandLine_printVersionInfo_withMarkupAndParameterContainingMarkup() throws Exception {
@Command(version = {
"@|yellow Versioned Command 1.0|@",
"@|blue Build 12345|@%1$s",
"@|red,bg(white) (c) 2017|@%2$s" })
class Versioned {}
String[] args = {"@|bold VALUE1|@", "@|underline VALUE2|@", "VALUE3"};
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos, true, "UTF8");
new CommandLine(new Versioned()).printVersionHelp(ps, Help.Ansi.ON, args);
String result = baos.toString("UTF8");
assertEquals(String.format("" +
"\u001B[33mVersioned Command 1.0\u001B[39m\u001B[0m%n" +
"\u001B[34mBuild 12345\u001B[39m\u001B[0m\u001B[1mVALUE1\u001B[21m\u001B[0m%n" +
"\u001B[31m\u001B[47m(c) 2017\u001B[49m\u001B[39m\u001B[0m\u001B[4mVALUE2\u001B[24m\u001B[0m%n"), result);
}
}

0 comments on commit 0e63404

Please sign in to comment.