]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
ceph-volume: terminal: add CLI dispatching helpers
authorAlfredo Deza <adeza@redhat.com>
Wed, 12 Jul 2017 16:13:35 +0000 (12:13 -0400)
committerAlfredo Deza <adeza@redhat.com>
Fri, 4 Aug 2017 14:25:58 +0000 (10:25 -0400)
Signed-off-by: Alfredo Deza <adeza@redhat.com>
src/ceph-volume/ceph_volume/terminal.py

index 7bcb4ddf3a6e72660f17ac45b1b9271d133caf24..55ce2d4ca280946b90e2ad5bcf23890430d4589b 100644 (file)
@@ -120,3 +120,32 @@ def warning(msg):
 
 def success(msg):
     return _Write(prefix=green_arrow).raw(msg)
+
+
+def dispatch(mapper, argv=None):
+    argv = argv or sys.argv
+    for count, arg in enumerate(argv, 1):
+        if arg in mapper.keys():
+            instance = mapper.get(arg)(argv[count:])
+            if hasattr(instance, 'main'):
+                instance.main()
+                raise SystemExit(0)
+
+
+def subhelp(mapper):
+    """
+    Look at every value of every key in the mapper and will output any
+    ``class.help`` possible to return it as a string that will be sent to
+    stdout.
+    """
+    help_text_lines = []
+    for key, value in mapper.items():
+        try:
+            help_text = value.help
+        except AttributeError:
+            continue
+        help_text_lines.append("%-24s %s" % (key, help_text))
+
+    if help_text_lines:
+        return "Available subcommands:\n\n%s" % '\n'.join(help_text_lines)
+    return ''