-commands = {
- 'add': stgit.commands.add,
- 'applied': stgit.commands.applied,
- 'delete': stgit.commands.delete,
- 'diff': stgit.commands.diff,
- 'clean': stgit.commands.clean,
- 'export': stgit.commands.export,
- 'files': stgit.commands.files,
- 'init': stgit.commands.init,
- 'mail': stgit.commands.mail,
- 'new': stgit.commands.new,
- 'pop': stgit.commands.pop,
- 'pull': stgit.commands.pull,
- 'push': stgit.commands.push,
- 'refresh': stgit.commands.refresh,
- 'rename': stgit.commands.rename,
- 'resolved': stgit.commands.resolved,
- 'rm': stgit.commands.rm,
- 'series': stgit.commands.series,
- 'status': stgit.commands.status,
- 'top': stgit.commands.top,
- 'unapplied':stgit.commands.unapplied,
- }
+class Commands(dict):
+ """Commands class. It performs on-demand module loading
+ """
+ def canonical_cmd(self, key):
+ """Return the canonical name for a possibly-shortenned
+ command name.
+ """
+ candidates = [cmd for cmd in self.keys() if cmd.startswith(key)]
+
+ if not candidates:
+ print >> sys.stderr, 'Unknown command: %s' % key
+ print >> sys.stderr, ' Try "%s help" for a list of ' \
+ 'supported commands' % prog
+ sys.exit(1)
+ elif len(candidates) > 1:
+ print >> sys.stderr, 'Ambiguous command: %s' % key
+ print >> sys.stderr, ' Candidates are: %s' \
+ % ', '.join(candidates)
+ sys.exit(1)
+
+ return candidates[0]
+
+ def __getitem__(self, key):
+ """Return the command python module name based.
+ """
+ global prog
+
+ cmd_mod = self.get(key) or self.get(self.canonical_cmd(key))
+
+ __import__('stgit.commands.' + cmd_mod)
+ return getattr(stgit.commands, cmd_mod)