-commands = {
- 'add': stgit.commands.add,
- 'applied': stgit.commands.applied,
- 'branch': stgit.commands.branch,
- 'delete': stgit.commands.delete,
- 'diff': stgit.commands.diff,
- 'clean': stgit.commands.clean,
- 'clone': stgit.commands.clone,
- 'commit': stgit.commands.commit,
- 'export': stgit.commands.export,
- 'files': stgit.commands.files,
- 'fold': stgit.commands.fold,
- 'goto': stgit.commands.goto,
- 'id': stgit.commands.id,
- 'import': stgit.commands.imprt,
- 'init': stgit.commands.init,
- 'mail': stgit.commands.mail,
- 'new': stgit.commands.new,
- 'patches': stgit.commands.patches,
- 'pick': stgit.commands.pick,
- '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,
- 'show': stgit.commands.show,
- 'status': stgit.commands.status,
- 'top': stgit.commands.top,
- 'unapplied':stgit.commands.unapplied,
- 'uncommit': stgit.commands.uncommit,
- }
+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)
+
+commands = Commands({
+ 'add': 'add',
+ 'applied': 'applied',
+ 'assimilate': 'assimilate',
+ 'branch': 'branch',
+ 'delete': 'delete',
+ 'diff': 'diff',
+ 'clean': 'clean',
+ 'clone': 'clone',
+ 'commit': 'commit',
+ 'export': 'export',
+ 'files': 'files',
+ 'float': 'float',
+ 'fold': 'fold',
+ 'goto': 'goto',
+ 'hide': 'hide',
+ 'id': 'id',
+ 'import': 'imprt',
+ 'init': 'init',
+ 'log': 'log',
+ 'mail': 'mail',
+ 'new': 'new',
+ 'patches': 'patches',
+ 'pick': 'pick',
+ 'pop': 'pop',
+ 'pull': 'pull',
+ 'push': 'push',
+ 'rebase': 'rebase',
+ 'refresh': 'refresh',
+ 'rename': 'rename',
+ 'resolved': 'resolved',
+ 'rm': 'rm',
+ 'series': 'series',
+ 'show': 'show',
+ 'status': 'status',
+ 'sync': 'sync',
+ 'top': 'top',
+ 'unapplied': 'unapplied',
+ 'uncommit': 'uncommit',
+ 'unhide': 'unhide'
+ })