Allow runtime args when invoking interactive shell
authorStavros Sachtouris <saxtouri@admin.grnet.gr>
Mon, 13 May 2013 13:53:20 +0000 (16:53 +0300)
committerStavros Sachtouris <saxtouri@admin.grnet.gr>
Mon, 13 May 2013 13:53:20 +0000 (16:53 +0300)
Refs: #3716

Behavior After This:
kamaki -XXX -YY ... parses -XXX -YY
If not recognized: syntax error
If recognized, group them into shell args (e.g. -h) and command args (e.g. -d)
Whenever a command is run in this shell instance, apply the command args

Practically, users may pass the -d or -v args to the shell and enjoy streaming
debug or verbosity logs, without having to pass -d or -v args on each command

kamaki/cli/command_shell.py

index fec2820..7be59a7 100644 (file)
@@ -41,6 +41,9 @@ from kamaki.cli.utils import print_dict, split_input
 from kamaki.cli.history import History
 from kamaki.cli.errors import CLIError
 from kamaki.clients import ClientError
+from kamaki.cli.logger import add_file_logger
+
+log = add_file_logger(__name__)
 
 
 def _init_shell(exe_string, parser):
@@ -156,6 +159,19 @@ class Shell(Cmd):
     def _restore(self, oldcontext):
         self.__dict__ = oldcontext
 
+    @staticmethod
+    def _create_help_method(cmd_name, args, descr):
+        tmp_args = dict(args)
+        tmp_args.pop('options', None)
+        tmp_args.pop('debug', None)
+        tmp_args.pop('verbose', None)
+        tmp_args.pop('include', None)
+        tmp_args.pop('silent', None)
+        tmp_args.pop('config', None)
+        help_parser = ArgumentParseManager(cmd_name, tmp_args)
+        help_parser.parser.description = descr
+        return help_parser.parser.print_help
+
     def _register_command(self, cmd_path):
         cmd = self.cmd_tree.get_command(cmd_path)
         arguments = self._parser.arguments
@@ -168,14 +184,8 @@ class Shell(Cmd):
             """
             subcmd, cmd_args = cmd.parse_out(split_input(line))
             self._history.add(' '.join([cmd.path.replace('_', ' '), line]))
-            tmp_args = dict(self._parser.arguments)
-            tmp_args.pop('options', None)
-            tmp_args.pop('debug', None)
-            tmp_args.pop('verbose', None)
-            tmp_args.pop('include', None)
-            tmp_args.pop('silent', None)
-            cmd_parser = ArgumentParseManager(cmd.name, dict(tmp_args))
-
+            cmd_parser = ArgumentParseManager(
+                cmd.name, dict(self._parser.arguments))
             cmd_parser.parser.description = subcmd.help
 
             # exec command or change context
@@ -194,8 +204,10 @@ class Shell(Cmd):
                     cmd_parser.arguments = instance.arguments
                     cmd_parser.syntax = '%s %s' % (
                         subcmd.path.replace('_', ' '), cls.syntax)
+                    help_method = self._create_help_method(
+                        cmd.name, cmd_parser.arguments, subcmd.help)
                     if '-h' in cmd_args or '--help' in cmd_args:
-                        cmd_parser.parser.print_help()
+                        help_method()
                         if ldescr.strip():
                             print('\nDetails:')
                             print('%s' % ldescr)
@@ -208,10 +220,7 @@ class Shell(Cmd):
                             name,
                             arg.default)
 
-                    exec_cmd(
-                        instance,
-                        cmd_parser.unparsed,
-                        cmd_parser.parser.print_help)
+                    exec_cmd(instance, cmd_parser.unparsed, help_method)
                         #[term for term in cmd_parser.unparsed\
                         #    if not term.startswith('-')],
                 except (ClientError, CLIError) as err: