### Question:
Cli { String name() { return spec.name(); } Cli(CliSpec<CliFlagsT> spec); static int runCli(
            CliSpec<CliOptsT> spec, String[] args); static void printUsage(CliSpec<CliOptsT> spec);  }### Answer:
@Test public void testHelpFlag() throws Exception { assertEquals(0, TestCli.doMain(console, new String[] { "help" })); String buffer1 = new String(consoleBuffer.toByteArray(), UTF_8); ByteArrayOutputStream anotherBuffer = new ByteArrayOutputStream(); PrintStream anotherConsole = new PrintStream(anotherBuffer, true, UTF_8.name()); assertEquals(-1, TestCli.doMain(anotherConsole, new String[] { "-h" })); String buffer2 = new String(anotherBuffer.toByteArray(), UTF_8); assertEquals(buffer1, buffer2); }
@Test public void testPrintCommandUsage() throws Exception { assertEquals(0, TestCli.doMain(console, new String[] { "help", "dog" })); String consoleBufferStr = new String(consoleBuffer.toByteArray(), UTF_8); ByteArrayOutputStream expectedBufferStream = new ByteArrayOutputStream(); PrintStream expectedConsole = new PrintStream(expectedBufferStream, true, UTF_8.name()); expectedConsole.println("command dog"); String expectedBuffer = new String(expectedBufferStream.toByteArray(), UTF_8); assertEquals(expectedBuffer, consoleBufferStr); }
@Test public void testNestedCommandHelpFlag() throws Exception { assertEquals(0, TestCli.doMain(console, new String[] { "nested", "help" })); String buffer1 = new String(consoleBuffer.toByteArray(), UTF_8); ByteArrayOutputStream anotherBuffer = new ByteArrayOutputStream(); PrintStream anotherConsole = new PrintStream(anotherBuffer, true, UTF_8.name()); assertEquals(-1, TestCli.doMain(anotherConsole, new String[] { "nested", "-h" })); String buffer2 = new String(anotherBuffer.toByteArray(), UTF_8); assertEquals(buffer1, buffer2); }
@Test public void testPrintSubCommandUsage() throws Exception { assertEquals(0, TestCli.doMain(console, new String[] { "nested", "help", "cat" })); String consoleBufferStr = new String(consoleBuffer.toByteArray(), UTF_8); ByteArrayOutputStream expectedBufferStream = new ByteArrayOutputStream(); PrintStream expectedConsole = new PrintStream(expectedBufferStream, true, UTF_8.name()); expectedConsole.println("command cat"); String expectedBuffer = new String(expectedBufferStream.toByteArray(), UTF_8); assertEquals(expectedBuffer, consoleBufferStr); }