Juan Hernandez has uploaded a new change for review.

Change subject: cli: Accept intermixed arguments and options
......................................................................

cli: Accept intermixed arguments and options

Currently the syntax of a command requires all the arguments to appear
before all the options. This complicates things when the value of an
option is required by the autocompletion mechanism in order to decide
which are the acceptable arguments. This patch changes the parsing rules
so that arguments and options can be freely intermixed.

Change-Id: I8c1f6982e7beddb17a4668f5719294a8aaae3c2e
Bug-Url: https://bugzilla.redhat.com/1047513
Signed-off-by: Juan Hernandez <[email protected]>
---
M src/cli/context.py
M src/cli/parser.py
2 files changed, 35 insertions(+), 31 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine-cli refs/changes/66/22866/1

diff --git a/src/cli/context.py b/src/cli/context.py
index 1701673..cd1fa44 100644
--- a/src/cli/context.py
+++ b/src/cli/context.py
@@ -383,7 +383,21 @@
 #        if parsed[0] == '!':
 #            self._execute_shell_command(parsed[1])
 #            return
-        name, args, opts, redirections, pipeline = parsed
+
+        # The parser produces a list of parameters that include both the
+        # arguments and the options. We need to move them to separate
+        # lists to ease later processing. Arguments are plain strings and
+        # options are pairs containing the name of the option and the value,
+        # so it is easy to separate them checking their type.
+        name, parameters, redirections, pipeline = parsed
+        args = []
+        opts = []
+        for parameter in parameters:
+            if type(parameter) is str:
+                args.append(parameter)
+            else:
+                opts.append(parameter)
+
         if self.settings.get('cli:autopage'):
             if pipeline:
                 pipeline += '| %s' % self.__pager
diff --git a/src/cli/parser.py b/src/cli/parser.py
index 30f6b24..4158a99 100644
--- a/src/cli/parser.py
+++ b/src/cli/parser.py
@@ -22,8 +22,10 @@
 class Parser(PLYParser):
     """A parser for CLI commands. The general form of a CLI comand is:
 
-    <command> [arguments] [options] [redirection] [pipeline]
+    <command> [parameter] [redirection] [pipeline]
     ! <command>
+
+    A parameter can be an argument or an option.
 
     This parser supports quoted strings for arguments and options, and
     multi-line inputs.
@@ -139,17 +141,17 @@
             p[0] = p[1] + [p[2]]
 
     def p_command(self, p):
-        """command : name argument_list option_list redirections pipeline 
heredoc eol
+        """command : name parameter_list redirections pipeline heredoc eol
                    | BANG SHELL eol
                    | eol
         """
-        if len(p) == 8:
-            if p[6]:
-                for i in range(len(p[4])):
-                    if p[4][i][0] == '<<':
-                        p[4][i] = ('<<', p[6])
+        if len(p) == 7:
+            if p[5]:
+                for i in range(len(p[3])):
+                    if p[3][i][0] == '<<':
+                        p[3][i] = ('<<', p[5])
                         break
-            p[0] = (p[1], p[2], p[3], p[4], p[5])
+            p[0] = (p[1], p[2], p[3], p[4])
         elif len(p) == 4:
             p[0] = (p[1], p[2])
         elif len(p) == 2:
@@ -159,18 +161,20 @@
         """name : WORD"""
         p[0] = p[1]
 
-    def p_argument_list(self, p):
-        """argument_list : empty
-                         | argument
-                         | argument_list argument
+    def p_parameter_list(self, p):
+        """parameter_list : empty
+                          | parameter_list parameter
         """
         if len(p) == 2:
-            if p[1] is None:
-                p[0] = []
-            else:
-                p[0] = [p[1]]
+            p[0] = []
         else:
             p[0] = p[1] + [p[2]]
+
+    def p_parameter(self, p):
+        """parameter : argument
+                     | option
+        """
+        p[0] = p[1]
 
     def p_argument(self, p):
         """argument : IPADDR
@@ -180,20 +184,6 @@
                     | NUMBER
         """
         p[0] = p[1]
-
-    def p_option_list(self, p):
-        """option_list : empty
-                       | option
-                       | option_list option
-
-        """
-        if len(p) == 2:
-            if p[1] is None:
-                p[0] = []
-            else:
-                p[0] = [p[1]]
-        else:
-            p[0] = p[1] + [p[2]]
 
     def p_option(self, p):
         """option : OPTION option_value


-- 
To view, visit http://gerrit.ovirt.org/22866
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I8c1f6982e7beddb17a4668f5719294a8aaae3c2e
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine-cli
Gerrit-Branch: master
Gerrit-Owner: Juan Hernandez <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to