/** * Delegates to {@link #printVersionHelp(PrintStream, Help.Ansi)} with the {@linkplain Help.Ansi#AUTO platform default}. * @param out the printStream to print to * @see #printVersionHelp(PrintStream, Help.Ansi) * @since 0.9.8 */ public void printVersionHelp(PrintStream out) { printVersionHelp(out, Help.Ansi.AUTO); }
/** * If an @Option whose {@code versionHelp} attribute is annotated as true appears in the arguments. * then the script body is not run and this printVersionHelpMessage method is called instead. * The default behavior is to print the {@link CommandLine#printVersionHelp(PrintStream)} to {@code System.out}. * The return value becomes the return value for the Script.run which will be the exit code * if we've been called from the command line. * * @param commandLine The CommandLine instance * @return The value that Script.run should return ({@code null} by default). */ public Object printVersionHelpMessage(CommandLine commandLine) { commandLine.printVersionHelp(System.out); return null; } }
public static String versionString(CommandLine commandLine, Ansi ansi, Object... params) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); commandLine.printVersionHelp(new PrintStream(baos, true), ansi, params); String result = baos.toString(); return result; }
private void verifyVersionWithMarkup(CommandLine commandLine) { String[] args = {"@|bold VALUE1|@", "@|underline VALUE2|@", "VALUE3"}; ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(baos, true); commandLine.printVersionHelp(ps, Ansi.ON, (Object[]) args); String result = baos.toString(); 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); }
@Test public void testCommandLine_printVersionInfo_formatsArguments() { @CommandLine.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); new CommandLine(new Versioned()).printVersionHelp(ps, Ansi.OFF, "VALUE1", "VALUE2", "VALUE3"); String result = baos.toString(); assertEquals(String.format("First line VALUE1%nSecond line VALUE2%nThird line VALUE1 VALUE2%n"), result); }
@Test public void testCommandLine_printVersionInfo_printsArrayOfPlainTextStrings() { @CommandLine.Command(version = {"Versioned Command 1.0", "512-bit superdeluxe", "(c) 2017"}) class Versioned {} ByteArrayOutputStream baos = new ByteArrayOutputStream(); new CommandLine(new Versioned()).printVersionHelp(new PrintStream(baos, true), Ansi.OFF); String result = baos.toString(); assertEquals(String.format("Versioned Command 1.0%n512-bit superdeluxe%n(c) 2017%n"), result); }
@Test public void testCommandLine_printVersionInfo_printsSingleStringWithMarkup() { @CommandLine.Command(version = "@|red 1.0|@") class Versioned {} ByteArrayOutputStream baos = new ByteArrayOutputStream(); new CommandLine(new Versioned()).printVersionHelp(new PrintStream(baos, true), Ansi.ON); String result = baos.toString(); assertEquals(String.format("\u001B[31m1.0\u001B[39m\u001B[0m%n"), result); }
@Test public void testCommandLine_printVersionInfo_printsSinglePlainTextString() { @Command(version = "1.0") class Versioned {} ByteArrayOutputStream baos = new ByteArrayOutputStream(); new CommandLine(new Versioned()).printVersionHelp(new PrintStream(baos, true), Help.Ansi.OFF); String result = baos.toString(); assertEquals(String.format("1.0%n"), result); }
@Test public void testCommandLine_printVersionInfo_printsArrayOfStringsWithMarkup() { @CommandLine.Command(version = { "@|yellow Versioned Command 1.0|@", "@|blue Build 12345|@", "@|red,bg(white) (c) 2017|@" }) class Versioned {} ByteArrayOutputStream baos = new ByteArrayOutputStream(); new CommandLine(new Versioned()).printVersionHelp(new PrintStream(baos, true), Ansi.ON); String result = baos.toString(); assertEquals(String.format("" + "\u001B[33mVersioned Command 1.0\u001B[39m\u001B[0m%n" + "\u001B[34mBuild 12345\u001B[39m\u001B[0m%n" + "\u001B[31m\u001B[47m(c) 2017\u001B[49m\u001B[39m\u001B[0m%n"), result); } @Test
return true; } else if (parsed.isVersionHelpRequested()) { parsed.printVersionHelp(out, colorScheme.ansi); return true; } else if (parsed.getCommandSpec().helpCommand()) {
@Test public void testFailingVersionProvider() { @Command(versionProvider = FailingVersionProvider.class) class App {} CommandLine cmd = new CommandLine(new App()); try { cmd.printVersionHelp(System.out); fail("Expected exception"); } catch (ExecutionException ex) { assertEquals("Could not get version info from " + cmd.getCommandSpec().versionProvider() + ": java.lang.IllegalStateException: sorry can't give you a version", ex.getMessage()); } }
/** * Delegates to {@link #printVersionHelp(PrintStream, Help.Ansi)} with the {@linkplain Help.Ansi#AUTO platform default}. * @param out the printStream to print to * @see #printVersionHelp(PrintStream, Help.Ansi) * @since 0.9.8 */ public void printVersionHelp(PrintStream out) { printVersionHelp(out, Help.Ansi.AUTO); }
public static String versionString(CommandLine commandLine, Ansi ansi, Object... params) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); commandLine.printVersionHelp(new PrintStream(baos, true), ansi, params); String result = baos.toString(); return result; }
private void verifyVersionWithMarkup(CommandLine commandLine) { String[] args = {"@|bold VALUE1|@", "@|underline VALUE2|@", "VALUE3"}; ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(baos, true); commandLine.printVersionHelp(ps, Ansi.ON, (Object[]) args); String result = baos.toString(); 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); }
@Test public void testCommandLine_printVersionInfo_printsArrayOfPlainTextStrings() { @CommandLine.Command(version = {"Versioned Command 1.0", "512-bit superdeluxe", "(c) 2017"}) class Versioned {} ByteArrayOutputStream baos = new ByteArrayOutputStream(); new CommandLine(new Versioned()).printVersionHelp(new PrintStream(baos, true), Ansi.OFF); String result = baos.toString(); assertEquals(String.format("Versioned Command 1.0%n512-bit superdeluxe%n(c) 2017%n"), result); }
@Test public void testCommandLine_printVersionInfo_printsSingleStringWithMarkup() { @CommandLine.Command(version = "@|red 1.0|@") class Versioned {} ByteArrayOutputStream baos = new ByteArrayOutputStream(); new CommandLine(new Versioned()).printVersionHelp(new PrintStream(baos, true), Ansi.ON); String result = baos.toString(); assertEquals(String.format("\u001B[31m1.0\u001B[39m\u001B[0m%n"), result); }
@Test public void testCommandLine_printVersionInfo_formatsArguments() { @CommandLine.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); new CommandLine(new Versioned()).printVersionHelp(ps, Ansi.OFF, "VALUE1", "VALUE2", "VALUE3"); String result = baos.toString(); assertEquals(String.format("First line VALUE1%nSecond line VALUE2%nThird line VALUE1 VALUE2%n"), result); }
@Test public void testCommandLine_printVersionInfo_printsSinglePlainTextString() { @Command(version = "1.0") class Versioned {} ByteArrayOutputStream baos = new ByteArrayOutputStream(); new CommandLine(new Versioned()).printVersionHelp(new PrintStream(baos, true), Help.Ansi.OFF); String result = baos.toString(); assertEquals(String.format("1.0%n"), result); }
@Override public void run() { CommandLine commandLine = new CommandLine(this); if (usageHelpRequested) { CommandLine.usage(this, System.out); return; } if (versionInfoRequested) { commandLine.printVersionHelp(System.out); return; } Application application = cli.getPanda().getPandaLoader().loadFiles(inputFiles); application.launch(); }
@Test public void testFailingVersionProvider() { @Command(versionProvider = FailingVersionProvider.class) class App {} CommandLine cmd = new CommandLine(new App()); try { cmd.printVersionHelp(System.out); fail("Expected exception"); } catch (ExecutionException ex) { assertEquals("Could not get version info from " + cmd.getCommandSpec().versionProvider() + ": java.lang.IllegalStateException: sorry can't give you a version", ex.getMessage()); } }