Make 'mw help' work properly
[mw] / src / mw / cli.py
index 7f5b4bcd00cc15003212a3fc02408a0e3c0d223b..ef33d7357b41a267edb7237f28038a892fc0932a 100644 (file)
@@ -1,6 +1,6 @@
 ###
 # mw - VCS-like nonsense for MediaWiki websites
-# Copyright (C) 2009  Ian Weller <ian@ianweller.org>
+# Copyright (C) 2010  Ian Weller <ian@ianweller.org>
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
 # GNU General Public License for more details.
 #
 # You should have received a copy of the GNU General Public License along
-# with this program; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# with this program.  If not, see <http://www.gnu.org/licenses/>.
 ###
 
-import inspect
 import mw.clicommands
 import os
 import sys
 
+
 class CLI(object):
+
     def __init__(self):
         self.me = os.path.basename(sys.argv[0])
         self.commands = {}
+        self.shortcuts = {}
         for name in mw.clicommands.__dict__:
             if name == 'CommandBase':
                 continue
@@ -34,22 +35,32 @@ class CLI(object):
                issubclass(clazz, mw.clicommands.CommandBase):
                 cmd = clazz()
                 self.commands[cmd.name] = cmd
+                self.shortcuts[cmd.name] = cmd.shortcuts
+        self.all_commands = {}
+        self.all_commands.update(self.commands)
+        for command in self.shortcuts:
+            for shortcut in self.shortcuts[command]:
+                self.all_commands[shortcut] = self.commands[command]
 
     def usage(self):
         print 'usage: %s [subcommand]' % self.me
         print
         for name in self.commands:
             cmd = self.commands[name]
-            print("\t%-14s %-25s" % (name, cmd.description))
+            if len(cmd.shortcuts) > 0:
+                full = name + ' (' + ' '.join(cmd.shortcuts) + ')'
+            else:
+                full = name
+            print("\t%-14s %-25s" % (full, cmd.description))
         print
         sys.exit(1)
 
     def main(self):
         # determine what the subcommand is
         if len(sys.argv) > 1:
-            if sys.argv[1] in self.commands.keys():
+            if sys.argv[1] in self.all_commands:
                 the_command = sys.argv[1] # SWEET ACTION
-            elif sys.argv[1] in ['--help', '-h']:
+            elif sys.argv[1] in ['--help', '-h', 'help']:
                 self.usage()
             else:
                 print '%s: invalid subcommand: %s' % (self.me, sys.argv[1])
@@ -57,4 +68,4 @@ class CLI(object):
         if len(sys.argv) == 1:
             self.usage()
         # woo let's go
-        self.commands[the_command].main()
+        self.all_commands[the_command].main()

Benjamin Mako Hill || Want to submit a patch?