Mercurial > hg > release > thermostat-0.15
changeset 247:8c612ce65625
Improved usage rendering.
Reviewed-by: neugens
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-April/000949.html
line wrap: on
line diff
--- a/agent/src/main/java/com/redhat/thermostat/agent/AgentApplication.java Thu Apr 19 21:15:47 2012 +0200 +++ b/agent/src/main/java/com/redhat/thermostat/agent/AgentApplication.java Thu Apr 19 21:16:00 2012 +0200 @@ -73,10 +73,7 @@ // TODO: Use LocaleResources for i18n-ized strings. private static final String DESCRIPTION = "starts and stops the thermostat agent"; - private static final String USAGE = "agent start|stop\n\n" - + DESCRIPTION + "\n\n\t" - + "With argument 'start', start the agent.\n\t" - + "With argument 'stop', stop the agent.\n"; + private static final String USAGE = DESCRIPTION; private AgentStartupConfiguration configuration; private AgentOptionParser parser;
--- a/agent/src/test/java/com/redhat/thermostat/agent/AgentApplicationTest.java Thu Apr 19 21:15:47 2012 +0200 +++ b/agent/src/test/java/com/redhat/thermostat/agent/AgentApplicationTest.java Thu Apr 19 21:16:00 2012 +0200 @@ -80,10 +80,7 @@ @Test public void testUsage() { String usage = agent.getUsage(); - assertEquals("agent start|stop\n\n" - + "starts and stops the thermostat agent" + "\n\n\t" - + "With argument 'start', start the agent.\n\t" - + "With argument 'stop', stop the agent.\n", usage); + assertEquals("starts and stops the thermostat agent", usage); } @Test
--- a/common/src/main/java/com/redhat/thermostat/cli/CommandLineArgumentsParser.java Thu Apr 19 21:15:47 2012 +0200 +++ b/common/src/main/java/com/redhat/thermostat/cli/CommandLineArgumentsParser.java Thu Apr 19 21:16:00 2012 +0200 @@ -36,6 +36,7 @@ package com.redhat.thermostat.cli; +import java.io.PrintWriter; import java.util.Collection; import java.util.LinkedList; import java.util.List; @@ -43,6 +44,7 @@ import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.GnuParser; +import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.Option; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; @@ -67,7 +69,7 @@ } } - private Options convertToCommonsCLIOptions(List<ArgumentSpec> args) { + private Options convertToCommonsCLIOptions(Collection<ArgumentSpec> args) { Options options = new Options(); for (ArgumentSpec spec : args) { options.addOption(convertSpecToOption(spec)); @@ -81,4 +83,13 @@ option.setArgs(spec.isUsingAdditionalArgument() ? 1 : 0); return option; } + + void printHelp(CommandContext ctx, Command cmd) { + HelpFormatter helpFormatter = new HelpFormatter(); + helpFormatter.setOptPrefix("--"); + PrintWriter pw = new PrintWriter(ctx.getConsole().getOutput()); + Options options = convertToCommonsCLIOptions(cmd.getAcceptedArguments()); + helpFormatter.printHelp(pw, 80, cmd.getName(), cmd.getUsage(), options, 2, 4, null, true); + pw.flush(); + } }
--- a/common/src/main/java/com/redhat/thermostat/cli/HelpCommand.java Thu Apr 19 21:15:47 2012 +0200 +++ b/common/src/main/java/com/redhat/thermostat/cli/HelpCommand.java Thu Apr 19 21:16:00 2012 +0200 @@ -45,10 +45,7 @@ private static final int COMMANDS_COLUMNS_WIDTH = 15; private static final String NAME = "help"; private static final String DESCRIPTION = "show help for a given command or help overview"; - private static final String USAGE = "help [COMMAND]\n\n" - + DESCRIPTION + "\n\n\t" - + "With no arguments, print a list of commands with short help messages.\n\n\t" - + "Given a command, print help for that command.\n"; + private static final String USAGE = DESCRIPTION; @Override public void run(CommandContext ctx) { @@ -87,7 +84,8 @@ private void printCommandUsage(CommandContext ctx, String cmdName) { Command cmd = ctx.getCommandRegistry().getCommand(cmdName); if (cmd != null) { - ctx.getConsole().getOutput().print(cmd.getUsage()); + CommandLineArgumentsParser cliParser = new CommandLineArgumentsParser(); + cliParser.printHelp(ctx, cmd); } else { printCommandSummaries(ctx); }
--- a/common/src/test/java/com/redhat/thermostat/cli/HelpCommandTest.java Thu Apr 19 21:15:47 2012 +0200 +++ b/common/src/test/java/com/redhat/thermostat/cli/HelpCommandTest.java Thu Apr 19 21:16:00 2012 +0200 @@ -116,7 +116,7 @@ cmd.run(ctxFactory.createContext(args)); String actual = ctxFactory.getOutput(); - assertEquals(usage, actual); + assertEquals("usage: test1\ntest usage command 1\n\n", actual); } @Test @@ -145,10 +145,7 @@ @Test public void testUsage() { HelpCommand cmd = new HelpCommand(); - String expected = "help [COMMAND]\n\n" - + "show help for a given command or help overview\n\n\t" - + "With no arguments, print a list of commands with short help messages.\n\n\t" - + "Given a command, print help for that command.\n"; + String expected = "show help for a given command or help overview"; assertEquals(expected, cmd.getUsage()); }
--- a/tools/src/main/java/com/redhat/thermostat/tools/ThermostatService.java Thu Apr 19 21:15:47 2012 +0200 +++ b/tools/src/main/java/com/redhat/thermostat/tools/ThermostatService.java Thu Apr 19 21:16:00 2012 +0200 @@ -65,10 +65,7 @@ // TODO: Use LocaleResources for i18n-ized strings. private static final String DESCRIPTION = "starts and stops the thermostat storage and agent"; - private static final String USAGE = "service start|stop\n\n" - + DESCRIPTION + "\n\n\t" - + "With argument 'start', start the storage amd agent\n\t" - + "With argument 'stop', stop the storage and agent.\n"; + private static final String USAGE = DESCRIPTION; private BasicCommand database; private AgentApplication agent;
--- a/tools/src/main/java/com/redhat/thermostat/tools/cli/ListVMsCommand.java Thu Apr 19 21:15:47 2012 +0200 +++ b/tools/src/main/java/com/redhat/thermostat/tools/cli/ListVMsCommand.java Thu Apr 19 21:16:00 2012 +0200 @@ -58,10 +58,7 @@ // TODO: Localize. private static final String DESCRIPTION = "lists all currently monitored VMs"; - private static final String USAGE = "list-vms --dbUrl URL\n\n" - + DESCRIPTION + "\n\n\t" - + "Options:\n\n" - + "--dbUrl URL the URL of the storage to connect to.\n"; + private static final String USAGE = DESCRIPTION; private static final String DB_URL_ARG = "dbUrl";
--- a/tools/src/main/java/com/redhat/thermostat/tools/db/DBService.java Thu Apr 19 21:15:47 2012 +0200 +++ b/tools/src/main/java/com/redhat/thermostat/tools/db/DBService.java Thu Apr 19 21:16:00 2012 +0200 @@ -59,10 +59,7 @@ // TODO: Use LocaleResources for i18n-ized strings. private static final String DESCRIPTION = "starts and stops the thermostat storage"; - private static final String USAGE = "storage start|stop\n\n" - + DESCRIPTION + "\n\n\t" - + "With argument 'start', start the storage.\n\t" - + "With argument 'stop', stop the storage.\n"; + private static final String USAGE = DESCRIPTION; private DBStartupConfiguration configuration; private DBOptionParser parser;
--- a/tools/src/test/java/com/redhat/thermostat/tools/ThermostatServiceTest.java Thu Apr 19 21:15:47 2012 +0200 +++ b/tools/src/test/java/com/redhat/thermostat/tools/ThermostatServiceTest.java Thu Apr 19 21:16:00 2012 +0200 @@ -78,10 +78,7 @@ @Test public void testUsage() { String usage = thermostatService.getUsage(); - assertEquals("service start|stop\n\n" - + "starts and stops the thermostat storage and agent" + "\n\n\t" - + "With argument 'start', start the storage amd agent\n\t" - + "With argument 'stop', stop the storage and agent.\n", usage); + assertEquals("starts and stops the thermostat storage and agent", usage); } @Test
--- a/tools/src/test/java/com/redhat/thermostat/tools/cli/ListVMsCommandTest.java Thu Apr 19 21:15:47 2012 +0200 +++ b/tools/src/test/java/com/redhat/thermostat/tools/cli/ListVMsCommandTest.java Thu Apr 19 21:16:00 2012 +0200 @@ -182,10 +182,7 @@ @Test public void testUsage() { - String expected = "list-vms --dbUrl URL\n\n" - + "lists all currently monitored VMs\n\n\t" - + "Options:\n\n" - + "--dbUrl URL the URL of the storage to connect to.\n"; + String expected = "lists all currently monitored VMs"; assertEquals(expected, cmd.getUsage()); }
--- a/tools/src/test/java/com/redhat/thermostat/tools/db/DBServiceTest.java Thu Apr 19 21:15:47 2012 +0200 +++ b/tools/src/test/java/com/redhat/thermostat/tools/db/DBServiceTest.java Thu Apr 19 21:16:00 2012 +0200 @@ -248,10 +248,7 @@ public void testUsage() { DBService dbService = new DBService(); String usage = dbService.getUsage(); - assertEquals("storage start|stop\n\n" - + "starts and stops the thermostat storage" + "\n\n\t" - + "With argument 'start', start the storage.\n\t" - + "With argument 'stop', stop the storage.\n", usage); + assertEquals("starts and stops the thermostat storage", usage); } @Test