FELIX-1729, FELIX-1730: add the each / if commands, make sure the arguments are displayed in the correct order in the help system

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@823609 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/karaf/shell/commands/pom.xml b/karaf/shell/commands/pom.xml
index 3cae361..2ea685f 100644
--- a/karaf/shell/commands/pom.xml
+++ b/karaf/shell/commands/pom.xml
@@ -46,6 +46,10 @@
             <artifactId>org.apache.felix.karaf.shell.console</artifactId>
         </dependency>
         <dependency>
+            <groupId>org.apache.felix.gogo</groupId>
+            <artifactId>org.apache.felix.gogo.runtime</artifactId>
+        </dependency>
+        <dependency>
             <groupId>org.apache.felix</groupId>
             <artifactId>org.osgi.core</artifactId>
             <scope>provided</scope>
diff --git a/karaf/shell/commands/src/main/java/org/apache/felix/karaf/shell/commands/EachAction.java b/karaf/shell/commands/src/main/java/org/apache/felix/karaf/shell/commands/EachAction.java
new file mode 100644
index 0000000..eec57fa
--- /dev/null
+++ b/karaf/shell/commands/src/main/java/org/apache/felix/karaf/shell/commands/EachAction.java
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.felix.karaf.shell.commands;
+
+import java.util.Collection;
+import java.util.Collections;
+
+import org.apache.felix.gogo.commands.Argument;
+import org.apache.felix.gogo.commands.Command;
+import org.apache.felix.karaf.shell.console.OsgiCommandSupport;
+import org.osgi.service.command.Function;
+
+/**
+ * Execute a closure on a list of arguments.
+ */
+@Command(scope = "shell", name = "each", description = "Execute a closure on a list of arguments.")
+public class EachAction extends OsgiCommandSupport {
+
+    @Argument(name = "values", index = 0, multiValued = false, required = true, description = "The collection of arguments to iterate on")
+    Collection<Object> values;
+
+    @Argument(name = "function", index = 1, multiValued = false, required = true, description = "The function to execute")
+    Function function;
+
+    @Override
+    protected Object doExecute() throws Exception {
+        for (Object v : values) {
+            function.execute(session, Collections.singletonList(v));
+        }
+        return null;
+    }
+}
diff --git a/karaf/shell/commands/src/main/java/org/apache/felix/karaf/shell/commands/IfAction.java b/karaf/shell/commands/src/main/java/org/apache/felix/karaf/shell/commands/IfAction.java
new file mode 100644
index 0000000..d331ad0
--- /dev/null
+++ b/karaf/shell/commands/src/main/java/org/apache/felix/karaf/shell/commands/IfAction.java
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.felix.karaf.shell.commands;
+
+import org.apache.felix.gogo.commands.Argument;
+import org.apache.felix.gogo.commands.Command;
+import org.apache.felix.karaf.shell.console.OsgiCommandSupport;
+import org.osgi.service.command.Function;
+
+/**
+ * Execute a closure on a list of arguments.
+ */
+@Command(scope = "shell", name = "if", description = "If/Then/Else block.")
+public class IfAction extends OsgiCommandSupport {
+
+    @Argument(name = "condition", index = 0, multiValued = false, required = true, description = "The condition")
+    Function condition;
+
+    @Argument(name = "ifTrue", index = 1, multiValued = false, required = true, description = "The function to execute if the condition is true")
+    Function ifTrue;
+
+    @Argument(name = "ifFalse", index = 2, multiValued = false, required = false, description = "The function to execute if the condition is false")
+    Function ifFalse;
+
+    @Override
+    protected Object doExecute() throws Exception {
+        Object result = condition.execute(session, null);
+        if (isTrue(result)) {
+            return ifTrue.execute(session, null);
+        } else {
+            if (ifFalse != null) {
+                return ifFalse.execute(session, null);
+            }
+        }
+        return null;
+    }
+
+    private boolean isTrue(Object result) {
+        if (result == null) {
+            return false;
+        }
+        if (result instanceof String && ((String) result).equals("")) {
+            return false;
+        }
+        if (result instanceof Boolean) {
+            return ((Boolean) result).booleanValue();
+        }
+        return true;
+    }
+
+}
\ No newline at end of file
diff --git a/karaf/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml b/karaf/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml
index 596a1a8..b54758c 100644
--- a/karaf/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml
+++ b/karaf/shell/commands/src/main/resources/OSGI-INF/blueprint/shell-commands.xml
@@ -23,6 +23,9 @@
         <command name="shell/cat">
             <action class="org.apache.felix.karaf.shell.commands.CatAction"/>
         </command>
+        <command name="shell/each">
+            <action class="org.apache.felix.karaf.shell.commands.EachAction"/>
+        </command>
         <command name="shell/echo">
             <action class="org.apache.felix.karaf.shell.commands.EchoAction"/>
         </command>
@@ -37,6 +40,9 @@
             <action class="org.apache.felix.karaf.shell.commands.HistoryAction"/>
         </command>
         -->
+        <command name="shell/if">
+            <action class="org.apache.felix.karaf.shell.commands.IfAction"/>
+        </command>
         <command name="shell/info">
             <action class="org.apache.felix.karaf.shell.commands.InfoAction"/>
         </command>
diff --git a/karaf/shell/console/src/main/java/org/apache/felix/karaf/shell/console/commands/BlueprintCommand.java b/karaf/shell/console/src/main/java/org/apache/felix/karaf/shell/console/commands/BlueprintCommand.java
index b40f415..1ae8192 100644
--- a/karaf/shell/console/src/main/java/org/apache/felix/karaf/shell/console/commands/BlueprintCommand.java
+++ b/karaf/shell/console/src/main/java/org/apache/felix/karaf/shell/console/commands/BlueprintCommand.java
@@ -18,6 +18,9 @@
 package org.apache.felix.karaf.shell.console.commands;
 
 import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
 import java.util.List;
 import java.util.Set;
 import java.util.HashSet;
@@ -82,8 +85,14 @@
         }
 
         @Override
-        protected void printUsage(Command command, Set<Option> options, Set<Argument> arguments, PrintStream out)
+        protected void printUsage(Command command, Set<Option> options, Set<Argument> args, PrintStream out)
         {
+            List<Argument> arguments = new ArrayList<Argument>(args);
+            Collections.sort(arguments, new Comparator<Argument>() {
+                public int compare(Argument o1, Argument o2) {
+                    return Integer.valueOf(o1.index()).compareTo(Integer.valueOf(o2.index()));
+                }
+            });
             options = new HashSet<Option>(options);
             options.add(HELP);
             if (command != null && (command.description() != null || command.name() != null))