You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by jd...@apache.org on 2008/09/29 10:02:05 UTC

svn commit: r700018 - in /geronimo/gshell/trunk/gshell-commands/gshell-builtins/src/main: java/org/apache/geronimo/gshell/commands/builtins/ resources/org/apache/geronimo/gshell/commands/builtins/

Author: jdillon
Date: Mon Sep 29 01:02:04 2008
New Revision: 700018

URL: http://svn.apache.org/viewvc?rev=700018&view=rev
Log:
Simplify the 'set' command by not going with the 'set a=b' syntax, instead using 'set a b' or 'set a "b c d e"'.  This removes some hacks to deal with quoting.  May bring the = stuff back once the parser can handle it.

Modified:
    geronimo/gshell/trunk/gshell-commands/gshell-builtins/src/main/java/org/apache/geronimo/gshell/commands/builtins/SetAction.java
    geronimo/gshell/trunk/gshell-commands/gshell-builtins/src/main/java/org/apache/geronimo/gshell/commands/builtins/UnsetAction.java
    geronimo/gshell/trunk/gshell-commands/gshell-builtins/src/main/resources/org/apache/geronimo/gshell/commands/builtins/SetAction.properties

Modified: geronimo/gshell/trunk/gshell-commands/gshell-builtins/src/main/java/org/apache/geronimo/gshell/commands/builtins/SetAction.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-commands/gshell-builtins/src/main/java/org/apache/geronimo/gshell/commands/builtins/SetAction.java?rev=700018&r1=700017&r2=700018&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-commands/gshell-builtins/src/main/java/org/apache/geronimo/gshell/commands/builtins/SetAction.java (original)
+++ geronimo/gshell/trunk/gshell-commands/gshell-builtins/src/main/java/org/apache/geronimo/gshell/commands/builtins/SetAction.java Mon Sep 29 01:02:04 2008
@@ -24,12 +24,12 @@
 import org.apache.geronimo.gshell.command.CommandAction;
 import org.apache.geronimo.gshell.command.CommandContext;
 import org.apache.geronimo.gshell.command.Variables;
+import org.apache.geronimo.gshell.i18n.MessageSource;
 import org.apache.geronimo.gshell.io.IO;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.util.Iterator;
-import java.util.List;
 import java.util.Properties;
 
 /**
@@ -42,7 +42,7 @@
 {
     private final Logger log = LoggerFactory.getLogger(getClass());
 
-    enum Mode
+    private enum Mode
     {
         VARIABLE,
         PROPERTY
@@ -54,18 +54,39 @@
     @Option(name="-v", aliases={"--verbose"})
     private boolean verbose;
 
-    @Argument
-    private List<String> args = null;
+    @Argument(index=0)
+    private String name;
+
+    @Argument(index=1)
+    private String value;
 
     public Object execute(final CommandContext context) throws Exception {
         assert context != null;
+        IO io = context.getIo();
+        MessageSource messages = context.getCommand().getMessages();
 
-        // No args... list all properties or variables
-        if (args == null || args.size() == 0) {
+        if (name == null) {
             return displayList(context);
         }
+        else if (value == null) {
+            io.error("Missing required argument: {}", messages.getMessage("command.argument.value.token"));
+            return Result.FAILURE;
+        }
+
+        switch (mode) {
+            case PROPERTY:
+                log.debug("Setting system property: {}={}", name, value);
+                System.setProperty(name, value);
+                break;
+
+            case VARIABLE:
+                Variables vars = context.getVariables();
+                log.info("Setting variable: {}={}", name, value);
+                vars.parent().set(name, value);
+                break;
+        }
 
-        return set(context);
+        return Result.SUCCESS;
     }
 
     private Object displayList(final CommandContext context) throws Exception {
@@ -117,84 +138,4 @@
 
         return Result.SUCCESS;
     }
-
-    private Object set(final CommandContext context) throws Exception {
-        assert context != null;
-        //
-        // FIXME: This does not jive well with the parser, and stuff like foo = "b a r"
-        //
-
-        //
-        // NOTE: May want to make x=b part of the CL grammar
-        //
-
-        for (Object arg : args) {
-            String namevalue = String.valueOf(arg);
-
-            switch (mode) {
-                case PROPERTY:
-                    setProperty(namevalue);
-                    break;
-
-                case VARIABLE:
-                    Variables variables = context.getVariables();
-                    setVariable(variables, namevalue);
-                    break;
-            }
-        }
-
-        return Result.SUCCESS;
-    }
-
-    class NameValue
-    {
-        String name;
-        String value;
-    }
-
-    private NameValue parse(final String input) {
-        NameValue nv = new NameValue();
-
-        int i = input.indexOf("=");
-        int firstDoubleQuote = input.indexOf("\"");
-        int firstSingleQuote = input.indexOf("'");
-
-        if (i == -1) {
-            nv.name = input;
-            nv.value = "true";
-        }
-        else if (firstDoubleQuote != -1) {
-        	nv.name = input.substring(0,i);
-        	nv.value = input.substring(firstDoubleQuote + 1, input.length()-1); 
-        } 
-        else if (firstSingleQuote != -1) {
-        	nv.name = input.substring(0,i);
-        	nv.value = input.substring(firstSingleQuote + 1, input.length()-1); 
-        } 
-        else {
-            nv.name = input.substring(0, i);
-            nv.value = input.substring(i + 1, input.length());
-        }
-
-        nv.name = nv.name.trim();
-
-        return nv;
-    }
-
-    private void setProperty(final String namevalue) {
-        NameValue nv = parse(namevalue);
-
-        log.info("Setting system property: {}={}", nv.name, nv.value);
-
-        System.setProperty(nv.name, nv.value);
-    }
-
-    private void setVariable(final Variables vars, final String namevalue) {
-        NameValue nv = parse(namevalue);
-
-        log.info("Setting variable: {}={}", nv.name, nv.value);
-
-        // Command vars always has a parent, set only makes sence when setting in parent's scope
-        vars.parent().set(nv.name, nv.value);
-    }
 }

Modified: geronimo/gshell/trunk/gshell-commands/gshell-builtins/src/main/java/org/apache/geronimo/gshell/commands/builtins/UnsetAction.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-commands/gshell-builtins/src/main/java/org/apache/geronimo/gshell/commands/builtins/UnsetAction.java?rev=700018&r1=700017&r2=700018&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-commands/gshell-builtins/src/main/java/org/apache/geronimo/gshell/commands/builtins/UnsetAction.java (original)
+++ geronimo/gshell/trunk/gshell-commands/gshell-builtins/src/main/java/org/apache/geronimo/gshell/commands/builtins/UnsetAction.java Mon Sep 29 01:02:04 2008
@@ -39,7 +39,7 @@
 {
     private final Logger log = LoggerFactory.getLogger(getClass());
 
-    enum Mode
+    private enum Mode
     {
         VARIABLE,
         PROPERTY

Modified: geronimo/gshell/trunk/gshell-commands/gshell-builtins/src/main/resources/org/apache/geronimo/gshell/commands/builtins/SetAction.properties
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-commands/gshell-builtins/src/main/resources/org/apache/geronimo/gshell/commands/builtins/SetAction.properties?rev=700018&r1=700017&r2=700018&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-commands/gshell-builtins/src/main/resources/org/apache/geronimo/gshell/commands/builtins/SetAction.properties (original)
+++ geronimo/gshell/trunk/gshell-commands/gshell-builtins/src/main/resources/org/apache/geronimo/gshell/commands/builtins/SetAction.properties Mon Sep 29 01:02:04 2008
@@ -29,7 +29,11 @@
 
 command.option.verbose=Enable verbose output
 
-command.argument.args=Variable definition
+command.argument.name=Variable or property name
+command.argument.name.token=NAME
+
+command.argument.value=Variable or property value
+command.argument.value.token=VALUE
 
 command.manual=\
   TODO: set manual
\ No newline at end of file