Initial commit with init and fetch commands
[mw] / src / mw / cli.py
1 ###
2 # mw - VCS-like nonsense for MediaWiki websites
3 # Copyright (C) 2009  Ian Weller <ian@ianweller.org>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 ###
19
20 import inspect
21 import mw.clicommands
22 import os
23 import sys
24
25 class CLI(object):
26     def __init__(self):
27         self.me = os.path.basename(sys.argv[0])
28         self.commands = {}
29         for name in mw.clicommands.__dict__:
30             if name == 'CommandBase':
31                 continue
32             clazz = mw.clicommands.__dict__[name]
33             if isinstance(clazz, type) and \
34                issubclass(clazz, mw.clicommands.CommandBase):
35                 cmd = clazz()
36                 self.commands[cmd.name] = cmd
37
38     def usage(self):
39         print 'usage: %s [subcommand]' % self.me
40         print
41         for name in self.commands:
42             cmd = self.commands[name]
43             print("\t%-14s %-25s" % (name, cmd.description))
44         print
45         sys.exit(1)
46
47     def main(self):
48         # determine what the subcommand is
49         if len(sys.argv) > 1:
50             if sys.argv[1] in self.commands.keys():
51                 the_command = sys.argv[1] # SWEET ACTION
52             elif sys.argv[1] in ['--help', '-h']:
53                 self.usage()
54             else:
55                 print '%s: invalid subcommand: %s' % (self.me, sys.argv[1])
56                 self.usage()
57         if len(sys.argv) == 1:
58             self.usage()
59         # woo let's go
60         self.commands[the_command].main()

Benjamin Mako Hill || Want to submit a patch?