# HG changeset patch # User Omair Majid # Date 1373653515 14400 # Node ID 23fd98e86b026928f30bd8bfe86016e57f6ef14e # Parent 7538716074c67cc75a39b8bde5ed19b8639054d6 Make command help message environment sensitive Reviewed-by: jerboaa Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-July/007316.html diff -r 7538716074c6 -r 23fd98e86b02 integration-tests/src/test/java/com/redhat/thermostat/itest/CliTest.java --- a/integration-tests/src/test/java/com/redhat/thermostat/itest/CliTest.java Fri Jul 12 10:03:42 2013 -0400 +++ b/integration-tests/src/test/java/com/redhat/thermostat/itest/CliTest.java Fri Jul 12 14:25:15 2013 -0400 @@ -189,7 +189,7 @@ String expectedOut = "Could not parse options: Unrecognized option: --foo\n" + "usage: thermostat shell\n" + " launches the Thermostat interactive shell\n" - + "Note: this command is only supported outside the shell\n" + + "\n" + "thermostat shell\n\n"; assertEquals(expectedOut, stdOut); } diff -r 7538716074c6 -r 23fd98e86b02 launcher/src/main/java/com/redhat/thermostat/launcher/internal/HelpCommand.java --- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/HelpCommand.java Fri Jul 12 10:03:42 2013 -0400 +++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/HelpCommand.java Fri Jul 12 14:25:15 2013 -0400 @@ -132,24 +132,34 @@ PrintWriter pw = new PrintWriter(ctx.getConsole().getOutput()); Options options = info.getOptions(); String usage = APP_NAME + " " + info.getUsage() + "\n" + info.getDescription(); - String header = getAvailabilityNote(info) + "\n" + APP_NAME + " " + info.getName(); + String header = ""; + if (isAvailabilityNoteNeeded(info)) { + header = header + getAvailabilityNote(info); + } + header = header + "\n" + APP_NAME + " " + info.getName(); helpFormatter.printHelp(pw, 80, usage, header, options, 2, 4, null); pw.flush(); } + private boolean isAvailabilityNoteNeeded(CommandInfo info) { + return !info.getEnvironments().contains(currentEnvironment); + } + + /** Describe where command is available */ private String getAvailabilityNote(CommandInfo info) { + String availabilityNote = ""; - // Availability note is only needed if the command is not available everywhere - if (!info.getEnvironments().containsAll(Arrays.asList(Environment.values()))) { - if (info.getEnvironments().contains(Environment.SHELL)) { - availabilityNote = translator.localize(LocaleResources.COMMAND_AVAILABLE_INSIDE_SHELL).getContents(); - } else if (info.getEnvironments().contains(Environment.CLI)) { - availabilityNote = translator.localize(LocaleResources.COMMAND_AVAILABLE_OUTSIDE_SHELL).getContents(); - } else { - throw new AssertionError("Need to handle a third environment"); - } + // there are two mutually exclusive environments: if an availability + // note is needed, it will just be about one + if (info.getEnvironments().contains(Environment.SHELL)) { + availabilityNote = translator.localize(LocaleResources.COMMAND_AVAILABLE_INSIDE_SHELL).getContents(); + } else if (info.getEnvironments().contains(Environment.CLI)) { + availabilityNote = translator.localize(LocaleResources.COMMAND_AVAILABLE_OUTSIDE_SHELL).getContents(); + } else { + throw new AssertionError("Need to handle a third environment"); } + return availabilityNote; }