Make shell syntax help as verbose as one-commands
authorStavros Sachtouris <saxtouri@admin.grnet.gr>
Mon, 18 Nov 2013 11:09:15 +0000 (13:09 +0200)
committerStavros Sachtouris <saxtouri@admin.grnet.gr>
Mon, 18 Nov 2013 11:09:15 +0000 (13:09 +0200)
Refs: #4583

kamaki/cli/argument/__init__.py
kamaki/cli/command_shell.py

index a985bac..8232eaa 100644 (file)
@@ -394,7 +394,9 @@ _arguments = dict(
 class ArgumentParseManager(object):
     """Manage (initialize and update) an ArgumentParser object"""
 
-    def __init__(self, exe, arguments=None, required=None):
+    def __init__(
+            self, exe,
+            arguments=None, required=None, syntax=None, description=None):
         """
         :param exe: (str) the basic command (e.g. 'kamaki')
 
@@ -412,12 +414,17 @@ class ArgumentParseManager(object):
             ['b', 'c']] means that the command required either 'a' and 'b' or
             'a' and 'c' or at least one of 'b', 'c' and could be written as
             [('a', ['b', 'c']), ['b', 'c']]
+        :param syntax: (str) The basic syntax of the arguments. Default:
+            exe <cmd_group> [<cmd_subbroup> ...] <cmd>
+        :param description: (str) The description of the commands or ''
         """
         self.parser = ArgumentParser(
             add_help=False, formatter_class=RawDescriptionHelpFormatter)
         self._exe = exe
-        self.syntax = '%s <cmd_group> [<cmd_subbroup> ...] <cmd>' % exe
+        self.syntax = syntax or (
+            '%s <cmd_group> [<cmd_subbroup> ...] <cmd>' % exe)
         self.required = required
+        self.parser.description = description or ''
         if arguments:
             self.arguments = arguments
         else:
@@ -555,8 +562,8 @@ class ArgumentParseManager(object):
             pkargs = (new_args,) if new_args else ()
             self._parsed, unparsed = self.parser.parse_known_args(*pkargs)
             pdict = vars(self._parsed)
-            diff = set(self.required or []).difference([
-                k for k in pdict if pdict[k] != None])
+            diff = set(self.required or []).difference(
+                [k for k in pdict if pdict[k] not in (None, )])
             if diff:
                 self.print_help()
                 miss = ['/'.join(self.arguments[k].parsed_name) for k in diff]
index abf2405..c6f3305 100644 (file)
@@ -175,10 +175,8 @@ class Shell(Cmd):
         tmp_args.pop('verbose', None)
         tmp_args.pop('silent', None)
         tmp_args.pop('config', None)
-        help_parser = ArgumentParseManager(cmd_name, tmp_args, required)
-        help_parser.parser.description = descr
-        help_parser.syntax = syntax
-        #return help_parser.parser.print_help
+        help_parser = ArgumentParseManager(
+            cmd_name, tmp_args, required, syntax=syntax, description=descr)
         return help_parser.print_help
 
     def _register_command(self, cmd_path):
@@ -213,11 +211,13 @@ class Shell(Cmd):
                             self.auth_base, self.cloud)
                     cmd_parser.update_arguments(instance.arguments)
                     cmd_parser.arguments = instance.arguments
+                    subpath = subcmd.path.split('_')[
+                        (len(cmd.path.split('_')) - 1):]
                     cmd_parser.syntax = '%s %s' % (
-                        subcmd.path.replace('_', ' '), cls.syntax)
+                        ' '.join(subpath), instance.syntax)
                     help_method = self._create_help_method(
-                        cmd.name, cmd_parser.arguments, cmd_parser.required,
-                        subcmd.help, cmd_parser.syntax)
+                        cmd.name, cmd_parser.arguments,
+                        cmd_parser.required, subcmd.help, cmd_parser.syntax)
                     if '-h' in cmd_args or '--help' in cmd_args:
                         help_method()
                         if ldescr.strip():