VMM/cli/main: Adjusted to new argparse functionality. v0.7.x
authorPascal Volk <user@localhost.localdomain.org>
Sun, 30 Dec 2012 16:12:18 +0000
branchv0.7.x
changeset 666 50148adebee9
parent 665 33d15936b53a
child 667 502561385ddf
VMM/cli/main: Adjusted to new argparse functionality.
VirtualMailManager/cli/main.py
vmm
--- a/VirtualMailManager/cli/main.py	Sun Dec 30 16:10:29 2012 +0000
+++ b/VirtualMailManager/cli/main.py	Sun Dec 30 16:12:18 2012 +0000
@@ -14,10 +14,9 @@
 from VirtualMailManager.config import BadOptionError, ConfigValueError
 from VirtualMailManager.cli import w_err
 from VirtualMailManager.cli.handler import CliHandler
-from VirtualMailManager.constants import DATABASE_ERROR, EX_MISSING_ARGS, \
-     EX_SUCCESS, EX_UNKNOWN_COMMAND, EX_USER_INTERRUPT, INVALID_ARGUMENT
-from VirtualMailManager.cli.subcommands import RunContext, cmd_map, \
-     update_cmd_map, usage
+from VirtualMailManager.constants import DATABASE_ERROR, EX_SUCCESS, \
+     EX_USER_INTERRUPT, INVALID_ARGUMENT
+from VirtualMailManager.cli.subcommands import RunContext, setup_parser
 
 
 _ = lambda msg: msg
@@ -35,27 +34,13 @@
         return handler
 
 
-def run(argv):
-    update_cmd_map()
-    if len(argv) < 2:
-        usage(EX_MISSING_ARGS, _("You must specify a subcommand at least."))
-
-    sub_cmd = argv[1].lower()
-    if sub_cmd in cmd_map:
-        cmd_func = cmd_map[sub_cmd].func
-    else:
-        for cmd in cmd_map.values():
-            if cmd.alias == sub_cmd:
-                cmd_func = cmd.func
-                sub_cmd = cmd.name
-                break
-        else:
-            usage(EX_UNKNOWN_COMMAND, _("Unknown subcommand: '%s'") % sub_cmd)
-
+def run():
+    parser = setup_parser()
+    args = parser.parse_args()
     handler = _get_handler()
-    run_ctx = RunContext(argv, handler, sub_cmd)
+    run_ctx = RunContext(args, handler)
     try:
-        cmd_func(run_ctx)
+        args.func(run_ctx)
     except (EOFError, KeyboardInterrupt):
         # TP: We have to cry, because root has killed/interrupted vmm
         # with Ctrl+C or Ctrl+D.
--- a/vmm	Sun Dec 30 16:10:29 2012 +0000
+++ b/vmm	Sun Dec 30 16:12:18 2012 +0000
@@ -15,4 +15,4 @@
     # Otherwise just remove /usr/local/sbin from sys.path
     sys.path.remove(sys.path[0])
     from VirtualMailManager.cli.main import run
-    sys.exit(run(sys.argv))
+    sys.exit(run())