changeset 1524:280587a7c6c2

Help added as a Common Option Reviewed-by: omajid Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2014-October/011226.html
author Lukasz Dracz <ldracz@redhat.com>
date Mon, 20 Oct 2014 13:22:54 -0400
parents df5a0ae22f11
children 84fbf4af48e3
files launcher/src/main/java/com/redhat/thermostat/launcher/internal/CommonOptions.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/HelpCommand.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/LocaleResources.java launcher/src/main/resources/com/redhat/thermostat/launcher/internal/strings.properties launcher/src/test/java/com/redhat/thermostat/launcher/internal/CommonOptionsTest.java launcher/src/test/java/com/redhat/thermostat/launcher/internal/HelpCommandTest.java
diffstat 6 files changed, 29 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/CommonOptions.java	Thu Oct 16 09:41:45 2014 -0400
+++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/CommonOptions.java	Mon Oct 20 13:22:54 2014 -0400
@@ -57,9 +57,11 @@
     // The launcher will auto-add a logLevel option if this special option is
     // specified in the command.properties option section. 
     static final String OPTIONS_COMMON_LOG_OPTION = "AUTO_LOG_OPTION";
-    
+
     static final String LOG_LEVEL_ARG = "logLevel";
     static final String DB_URL_ARG = "dbUrl";
+    static final String HELP_ARG = "help";
+
     static final Set<String> ALL_COMMON_OPTIONS = new HashSet<>(4);
     
     static {
@@ -86,5 +88,13 @@
         logOption.setArgName(LOG_LEVEL_ARG);
         return logOption;
     }
+
+    static Option getHelpOption() {
+        String helpDesc = t.localize(LocaleResources.OPTION_HELP_DESC).getContents();
+        Option helpOption = new Option(null, HELP_ARG, false, helpDesc);
+        helpOption.setRequired(false);
+        helpOption.setArgName(HELP_ARG);
+        return helpOption;
+    }
 }
 
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/HelpCommand.java	Thu Oct 16 09:41:45 2014 -0400
+++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/HelpCommand.java	Mon Oct 20 13:22:54 2014 -0400
@@ -45,6 +45,7 @@
 import java.util.List;
 
 import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Option;
 import org.apache.commons.cli.Options;
 
 import com.redhat.thermostat.common.cli.AbstractCommand;
@@ -137,6 +138,8 @@
             header = header + getAvailabilityNote(info);
         }
         header = header + "\n" + APP_NAME + " " + info.getName();
+        Option help = CommonOptions.getHelpOption();
+        options.addOption(help);
         helpFormatter.printHelp(pw, 80, usage, header, options, 2, 4, null);
         pw.flush();
     }
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/LocaleResources.java	Thu Oct 16 09:41:45 2014 -0400
+++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/LocaleResources.java	Mon Oct 20 13:22:54 2014 -0400
@@ -53,6 +53,7 @@
 
     OPTION_DB_URL_DESC,
     OPTION_LOG_LEVEL_DESC,
+    OPTION_HELP_DESC,
 
     MISSING_OPTION,
     MISSING_OPTIONS,
--- a/launcher/src/main/resources/com/redhat/thermostat/launcher/internal/strings.properties	Thu Oct 16 09:41:45 2014 -0400
+++ b/launcher/src/main/resources/com/redhat/thermostat/launcher/internal/strings.properties	Mon Oct 20 13:22:54 2014 -0400
@@ -17,6 +17,7 @@
 OPTION_LOG_LEVEL_DESC = sets the log level for this invocation. Possible values \
  for <logLevel> in decreasing severity are: SEVERE, WARNING, INFO, CONFIG, FINE, \
  FINER, FINEST and OFF
+OPTION_HELP_DESC= show usage of command
 
 MISSING_OPTION = Missing required option: {0}
 MISSING_OPTIONS = Missing required options: {0}
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/CommonOptionsTest.java	Thu Oct 16 09:41:45 2014 -0400
+++ b/launcher/src/test/java/com/redhat/thermostat/launcher/internal/CommonOptionsTest.java	Mon Oct 20 13:22:54 2014 -0400
@@ -73,5 +73,15 @@
         Option dbUrlOption = options.getOption("dbUrl");
         assertEquals(CommonOptions.DB_URL_ARG, dbUrlOption.getArgName());
     }
+
+    @Test
+    public void canGetHelpOption() {
+        Option logOption = CommonOptions.getHelpOption();
+        Options options = new Options();
+        options.addOption(logOption);
+        assertTrue(options.hasOption("help"));
+        assertFalse(logOption.isRequired());
+        assertFalse(logOption.hasArg());
+    }
 }
 
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/HelpCommandTest.java	Thu Oct 16 09:41:45 2014 -0400
+++ b/launcher/src/test/java/com/redhat/thermostat/launcher/internal/HelpCommandTest.java	Mon Oct 20 13:22:54 2014 -0400
@@ -148,7 +148,8 @@
         assertEquals("usage: thermostat usage of test command\n" +
                      "                  description of test command\n" +
                      "Note: this command is only supported outside the shell\n" +
-                     "thermostat test1\n\n", actual);
+                     "thermostat test1\n" +
+                     "     --help    show usage of command\n", actual);
     }
 
     @Test
@@ -241,6 +242,7 @@
                 + " test4         test command 4\n";
         assertEquals(expected, actual);
     }
+
     @Test
     public void verifyHelpUnknownCmdPrintsSummaries() {
         when(infos.getCommandInfo("test1")).thenThrow(new CommandInfoNotFoundException("test1"));