changeset 2424:d3945e6e5b8b

Allow LauncherImpl to recognize subcommand-specific options Reviewed-by: jerboaa Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2016-August/020620.html
author Andrew Azores <aazores@redhat.com>
date Mon, 22 Aug 2016 13:22:35 -0400
parents c966ceaa5905
children 47c53031d2bc
files launcher/src/main/java/com/redhat/thermostat/launcher/internal/LauncherImpl.java launcher/src/test/java/com/redhat/thermostat/launcher/internal/LauncherImplTest.java
diffstat 2 files changed, 26 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/LauncherImpl.java	Tue Aug 23 11:25:07 2016 -0400
+++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/LauncherImpl.java	Mon Aug 22 13:22:35 2016 -0400
@@ -48,6 +48,7 @@
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
+import org.apache.commons.cli.Option;
 import org.apache.commons.cli.Options;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleException;
@@ -347,6 +348,11 @@
             }
         }
         Options options = cmdInfo.getOptions();
+        for (PluginConfiguration.Subcommand subcommand : cmdInfo.getSubcommands()) {
+            for (Option option : (Collection<Option>) subcommand.getOptions().getOptions()) {
+                options.addOption(option);
+            }
+        }
         Arguments args = null;
         try {
             args = parseCommandArguments(cmdArgs, options);
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/LauncherImplTest.java	Tue Aug 23 11:25:07 2016 -0400
+++ b/launcher/src/test/java/com/redhat/thermostat/launcher/internal/LauncherImplTest.java	Mon Aug 22 13:22:35 2016 -0400
@@ -51,6 +51,7 @@
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.EnumSet;
 import java.util.List;
 import java.util.concurrent.ExecutorService;
@@ -104,7 +105,8 @@
     private static final String name3 = "test3";
     private static final String name4 = "test4";
     private static SecurityManager secMan;
-    
+    private CommandInfo info1;
+
     @BeforeClass
     public static void beforeClassSetUp() {
         // Launcher calls System.exit(). This causes issues for unit testing.
@@ -159,7 +161,7 @@
         setupCommandContextFactory();
 
         TestCommand cmd1 = new TestCommand(new TestCmd1());
-        CommandInfo info1 = mock(CommandInfo.class);
+        info1 = mock(CommandInfo.class);
         when(info1.getName()).thenReturn(name1);
         when(info1.getUsage()).thenReturn(name1 + " <--arg1 <arg>> [--arg2 <arg>]");
         Options options1 = new Options();
@@ -388,6 +390,22 @@
     }
 
     @Test
+    public void testSubcommandOptionRecognized() {
+        PluginConfiguration.Subcommand subInfo = mock(PluginConfiguration.Subcommand.class);
+        when(subInfo.getName()).thenReturn("sub");
+        when(subInfo.getDescription()).thenReturn("subcommand description");
+
+        Options subOptions = mock(Options.class);
+        Option optOption = new Option("o", "opt", false, "mock opt option");
+        when(subOptions.getOptions()).thenReturn(Collections.singleton(optOption));
+        when(subInfo.getOptions()).thenReturn(subOptions);
+
+        when(info1.getSubcommands()).thenReturn(Collections.singletonList(subInfo));
+        String expected = "foo, bar";
+        runAndVerifyCommand(new String[] {"test1", "sub", "--opt", "--arg1", "foo", "--arg2", "bar"}, expected, false);
+    }
+
+    @Test
     public void testBadOption() {
         String expected = "Could not parse options: Unrecognized option: --argNotAccepted\n"
                 + "usage: thermostat test1 <--arg1 <arg>> [--arg2 <arg>]\n"