Permalink
Browse files

implemented cli wrapper for regular commands

rational: important of available commands is important for usability
  • Loading branch information...
1 parent 59e6cc3 commit 757d13499f8d0b794837b5c9ac5d14e0174df870 @lirazsiri lirazsiri committed Jun 28, 2010
Showing with 105 additions and 59 deletions.
  1. +78 −0 cliwrapper.py
  2. +20 −0 cmd.py
  3. +7 −59 cmd_internal.py
View
@@ -0,0 +1,78 @@
+import os
+import re
+import sys
+import imp
+
+class _Commands(dict):
+ @staticmethod
+ def _list_commands(paths):
+ commands = set()
+ for path in paths:
+ for file in os.listdir(path):
+ m = re.match(r'^cmd_(.*)\.py[co]?$', file)
+ if not m:
+ continue
+ command = m.group(1)
+ commands.add(command)
+
+ return commands
+
+ @staticmethod
+ def _get_internals_module(name, path):
+ modname = "cmd_" + name
+ args = imp.find_module(modname, path)
+ return imp.load_module(modname, *args)
+
+ def __init__(self, path):
+ for command in self._list_commands(path):
+ self[command] = self._get_internals_module(command, path)
+
+class CliWrapper:
+ DESCRIPTION = ""
+ PATH = None
+ COMMANDS_USAGE_ORDER = []
+
+ @classmethod
+ def _usage(cls, commands, e=None):
+ if e:
+ print >> sys.stderr, "error: " + str(e)
+
+ print >> sys.stderr, "Syntax: %s <command> [arguments]" % sys.argv[0]
+ print >> sys.stderr, cls.DESCRIPTION.strip()
+
+ print >> sys.stderr, "\nCommands: \n"
+
+ command_names = commands.keys()
+ command_names.sort()
+
+ maxlen = max([ len(name) for name in command_names ]) + 2
+ tpl = " %%-%ds %%s" % (maxlen)
+
+ def shortdesc(command):
+ return commands[command].__doc__.strip().split('\n')[0]
+
+ for command in cls.COMMANDS_USAGE_ORDER:
+ if command == '':
+ print >> sys.stderr
+ else:
+ print >> sys.stderr, tpl % (command, shortdesc(command))
+
+ for command in set(commands.keys()) - set(cls.COMMANDS_USAGE_ORDER):
+ print >> sys.stderr, tpl % (command, shortdesc(command))
+
+ sys.exit(1)
+
+ @classmethod
+ def main(cls):
+ commands = _Commands(cls.PATH)
+
+ args = sys.argv[1:]
+ if not args:
+ cls._usage(commands)
+
+ command = args[0]
+ if command not in commands:
+ cls._usage(commands, "no such command")
+
+ sys.argv = args
+ commands[command].main()
View
20 cmd.py
@@ -0,0 +1,20 @@
+#!/usr/bin/python
+"""
+TurnKey Linux Backup and Migration
+"""
+import os
+from cliwrapper import CliWrapper
+
+class CliWrapper(CliWrapper):
+ DESCRIPTION = __doc__
+ PATH = [ os.path.dirname(__file__) ]
+ COMMANDS_USAGE_ORDER = ['init',
+ '',
+ 'passphrase', 'escrow',
+ '',
+ 'backup', 'list', 'restore', 'restore_rollback',
+ '',
+ 'internal']
+
+if __name__ == "__main__":
+ CliWrapper.main()
View
@@ -3,67 +3,15 @@
Execute an internal command
"""
import os
-import re
-import sys
-import imp
+from cliwrapper import CliWrapper
-class _InternalCommands(dict):
- @staticmethod
- def _list_commands(paths):
- commands = set()
- for path in paths:
- for file in os.listdir(path):
- m = re.match(r'^cmd_(.*)\.py[co]?$', file)
- if not m:
- continue
- command = m.group(1)
- commands.add(command)
+import cmd_internals
- return commands
+class CliWrapper(CliWrapper):
+ DESCRIPTION = __doc__
+ PATH = cmd_internals.__path__
- @staticmethod
- def _get_internals_module(name, module_path):
- modname = "cmd_" + name
- args = imp.find_module(modname, module_path)
- return imp.load_module(modname, *args)
-
- def __init__(self):
- import cmd_internals as m
-
- for command in self._list_commands(m.__path__):
- self[command] = self._get_internals_module(command, m.__path__)
-
-COMMANDS = _InternalCommands()
-
-def usage(e=None):
- if e:
- print >> sys.stderr, "error: " + str(e)
-
- print >> sys.stderr, "Syntax: %s <command> [arguments]" % sys.argv[0]
- print >> sys.stderr, __doc__.strip()
-
- print >> sys.stderr, "\nCommands: \n"
-
- command_names = COMMANDS.keys()
- command_names.sort()
-
- for command in command_names:
- command_shortdesc = COMMANDS[command].__doc__.strip().split('\n')[0]
- print >> sys.stderr, " %-16s %s" % (command, command_shortdesc)
-
- sys.exit(1)
-
-def main():
- args = sys.argv[1:]
- if not args:
- usage()
-
- command = args[0]
- if command not in COMMANDS:
- usage("no such command")
+if __name__ == "__main__":
+ CliWrapper.main()
- sys.argv = args
- COMMANDS[command].main()
-if __name__ == "__main__":
- main()

0 comments on commit 757d134

Please sign in to comment.