From 49dadb3fbc40e6e001a1c4fda19519decde16bbf Mon Sep 17 00:00:00 2001 From: Alfredo Deza Date: Wed, 12 Jul 2017 12:13:35 -0400 Subject: [PATCH] ceph-volume: terminal: add CLI dispatching helpers Signed-off-by: Alfredo Deza --- src/ceph-volume/ceph_volume/terminal.py | 29 +++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/ceph-volume/ceph_volume/terminal.py b/src/ceph-volume/ceph_volume/terminal.py index 7bcb4ddf3a6e7..55ce2d4ca2809 100644 --- a/src/ceph-volume/ceph_volume/terminal.py +++ b/src/ceph-volume/ceph_volume/terminal.py @@ -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 '' -- 2.47.3