vmm: reworked subcommand/arguments mapping. v0.6.x
authorPascal Volk <neverseen@users.sourceforge.net>
Tue, 02 Feb 2010 02:28:28 +0000
branchv0.6.x
changeset 183 eb4c73d9d0a4
parent 182 84811fcc3c69
child 184 d0425225ce52
vmm: reworked subcommand/arguments mapping.
vmm
--- a/vmm	Mon Feb 01 20:53:39 2010 +0000
+++ b/vmm	Tue Feb 02 02:28:28 2010 +0000
@@ -463,7 +463,27 @@
         os.sys.version.split()[0], _(u'on'), os.uname()[0], __prog__,
         _(u'is free software and comes with ABSOLUTELY NO WARRANTY.')))
 
-#def main():
+def main():
+    subcommand = os.sys.argv[1]
+    known_subcommand = False
+    try:
+        for s, l, f in subcmd_func.__iter__():
+            if subcommand in (s, l):
+                known_subcommand = True
+                f()
+        if not known_subcommand:
+            usage(EXIT.UNKNOWN_COMMAND, _(u'Unknown subcommand: ā€œ%sā€')% argv[1])
+        show_warnings()
+    except (EOFError, KeyboardInterrupt):
+        # TP: We have to cry, because root has killed/interrupted vmm
+        # with Ctrl+C or Ctrl+D.
+        w_err(EXIT.USER_INTERRUPT, _(u'\nOuch!\n'))
+    except (VMME.VMMConfigException, VMME.VMMException), e:
+        if e.code() != ERR.DATABASE_ERROR:
+            w_err(e.code(), _(u'Error: %s') % e.msg())
+        else:
+            w_err(e.code(), unicode(e.msg(), ENCODING, 'replace'))
+
 if __name__ == '__main__':
     __prog__ = os.path.basename(os.sys.argv[0])
     gettext.install(__prog__, '/usr/local/share/locale', unicode=1)
@@ -475,70 +495,35 @@
         usage(EXIT.MISSING_ARGS)
 
     vmm = get_vmm()
-    try:
-        if   argv[1] in (u'cf', u'configure'):
-            configure()
-        elif argv[1] in (u'da', u'domainadd'):
-            domain_add()
-        elif argv[1] in (u'di', u'domaininfo'):
-            domain_info()
-        elif argv[1] in (u'dt', u'domaintransport'):
-            domain_transport()
-        elif argv[1] in (u'dd', u'domaindelete'):
-            domain_delete()
-        elif argv[1] in (u'ada', u'aliasdomainadd'):
-            alias_domain_add()
-        elif argv[1] in (u'adi', u'aliasdomaininfo'):
-            alias_domain_info()
-        elif argv[1] in (u'ads', u'aliasdomainswitch'):
-            alias_domain_switch()
-        elif argv[1] in (u'add', u'aliasdomaindelete'):
-            alias_domain_delete()
-        elif argv[1] in (u'ua', u'useradd'):
-            user_add()
-        elif argv[1] in (u'ui', u'userinfo'):
-            user_info()
-        elif argv[1] in (u'un', u'username'):
-            user_name()
-        elif argv[1] in (u'up', u'userpassword'):
-            user_password()
-        elif argv[1] in (u'ut', u'usertransport'):
-            user_transport()
-        elif argv[1] in (u'u0', u'userdisable'):
-            user_disable()
-        elif argv[1] in (u'u1', u'userenable'):
-            user_enable()
-        elif argv[1] in (u'ud', u'userdelete'):
-            user_delete()
-        elif argv[1] in (u'aa', u'aliasadd'):
-            alias_add()
-        elif argv[1] in (u'ai', u'aliasinfo'):
-            alias_info()
-        elif argv[1] in (u'ad', u'aliasdelete'):
-            alias_delete()
-        elif argv[1] in (u'ra', u'relocatedadd'):
-            relocated_add()
-        elif argv[1] in (u'ri', u'relocatedinfo'):
-            relocated_info()
-        elif argv[1] in (u'rd', u'relocateddelete'):
-            relocated_delete()
-        elif argv[1] in (u'gu', u'getuser'):
-            user_byID()
-        elif argv[1] in (u'ld', u'listdomains'):
-            domain_list()
-        elif argv[1] in (u'h', u'help'):
-            usage()
-        elif argv[1] in (u'v', u'version'):
-            show_version()
-        else:
-            usage(EXIT.UNKNOWN_COMMAND, _(u'Unknown subcommand: ā€œ%sā€')% argv[1])
-        show_warnings()
-    except (EOFError, KeyboardInterrupt):
-        # TP: We have to cry, because root has killed/interrupted vmm
-        # with Ctrl+C or Ctrl+D.
-        w_err(EXIT.USER_INTERRUPT, _(u'\nOuch!\n'))
-    except (VMME.VMMConfigException, VMME.VMMException), e:
-        if e.code() != ERR.DATABASE_ERROR:
-            w_err(e.code(), _(u'Error: %s') % e.msg())
-        else:
-            w_err(e.code(), unicode(e.msg(), ENCODING, 'replace'))
+
+    subcmd_func = (
+        #short  long                 function
+        ('da',  'domainadd',         domain_add),
+        ('di',  'domaininfo',        domain_info),
+        ('dt',  'domaintransport',   domain_transport),
+        ('dd',  'domaindelete',      domain_delete),
+        ('ada', 'aliasdomainadd',    alias_domain_add),
+        ('adi', 'aliasdomaininfo',   alias_domain_info),
+        ('ads', 'aliasdomainswitch', alias_domain_switch),
+        ('add', 'aliasdomaindelete', alias_domain_delete),
+        ('ua',  'useradd',           user_add),
+        ('ui',  'userinfo',          user_info),
+        ('un',  'username',          user_name),
+        ('up',  'userpassword',      user_password),
+        ('ut',  'usertransport',     user_transport),
+        ('u0',  'userdisable',       user_disable),
+        ('u1',  'userenable',        user_enable),
+        ('ud',  'userdelete',        user_delete),
+        ('aa',  'aliasadd',          alias_add),
+        ('ai',  'aliasinfo',         alias_info),
+        ('ad',  'aliasdelete',       alias_delete),
+        ('ra',  'relocatedadd',      relocated_add),
+        ('ri',  'relocatedinfo',     relocated_info),
+        ('rd',  'relocateddelete',   relocated_delete),
+        ('cf',  'configure',         configure),
+        ('gu',  'getuser',           user_byID),
+        ('ld',  'listdomains',       domain_list),
+        ('h',   'help',              usage),
+        ('v',   'version',           show_version),)
+
+    main()