comparison profilemanager/main.py @ 1:979315ed0816

mucho cleanup on optionparser stuff
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 05 Apr 2010 08:55:44 -0700
parents 7301d534bc6c
children 35dc297efa25
comparison
equal deleted inserted replaced
0:7301d534bc6c 1:979315ed0816
2 2
3 import os 3 import os
4 import sys 4 import sys
5 5
6 from manager import ProfileManager 6 from manager import ProfileManager
7 from optparse import OptionGroup 7 from command import CommandParser
8 from optparse import OptionParser
9 from command import commands, commandargs2str, command2parser
10 8
11 # could go in commands 9 def create_profilemanager(parser, options):
12 def print_help(parser): 10 """create the profile manager from parsed arguments"""
13 parser.print_help()
14 # short descriptions for commands
15 command_descriptions = [dict(name=i,
16 description = commands[i]['doc'].strip().split('\n',1)[0])
17 for i in sorted(commands.keys())]
18 max_len = max([len(i['name']) for i in command_descriptions])
19 description = "Commands: \n%s" % ('\n'.join([' %s%s %s' % (description['name'], ' ' * (max_len - len(description['name'])), description['description'])
20 for description in command_descriptions]))
21
22 print
23 print description
24
25 def main(args=sys.argv[1:]):
26
27 # global option parsing
28 usage = '%prog <options> command <command-options>'
29 parser = OptionParser(usage, description='run `%prog help` to display commands')
30 parser.add_option('-c', '--config', dest='config',
31 help="specify a profile.ini [default: $HOME/.mozilla/firefox/profiles.ini]")
32 parser.disable_interspersed_args()
33 options, args = parser.parse_args(args)
34
35 # help/sanity check -- should probably be separated
36 if not len(args):
37 print_help(parser)
38 sys.exit(0)
39 if args[0] == 'help':
40 if len(args) == 2:
41 if args[1] in commands:
42 name = args[1]
43 commandparser = command2parser(name)
44 commandparser.print_help()
45 else:
46 parser.error("No command '%s'" % args[1])
47 else:
48 print_help(parser)
49 sys.exit(0)
50 command = args[0]
51 if command not in commands:
52 parser.error("No command '%s'" % command)
53
54 # XXX to move it its own method -- this is the only program-specific code
55 if options.config is None: 11 if options.config is None:
56 # XXX unix-specific 12 # XXX unix-specific
57 options.config = os.path.join(os.environ['HOME'], '.mozilla/firefox/profiles.ini') 13 options.config = os.path.join(os.environ['HOME'], '.mozilla/firefox/profiles.ini')
58 if not os.path.exists(options.config): 14 if not os.path.exists(options.config):
59 parser.error('%s does not exist' % options.config) 15 parser.error('%s does not exist' % options.config)
60 manager = ProfileManager(options.config) 16 return ProfileManager(options.config)
61 17
62 # command specific args 18 def main(args=sys.argv[1:]):
63 name = args[0]
64 command = commands[name]
65 commandparser = command2parser(name)
66 command_options, command_args = commandparser.parse_args(args[1:])
67 if len(command_args) < len(command['args']):
68 commandparser.error("Not enough arguments given")
69 if len(command_args) != len(command['args']) and not command['varargs']:
70 commandparser.error("Too many arguments given")
71 19
72 # invoke the command 20 # global option parsing
73 getattr(manager, name)(*command_args, **command_options.__dict__) 21 commands = [ ProfileManager.clone,
22 ProfileManager.backup,
23 ProfileManager.restore,
24 ProfileManager.merge ]
25 parser = CommandParser(commands, setup=create_profilemanager)
26 parser.add_option('-c', '--config', dest='config',
27 help="specify a profile.ini [default: $HOME/.mozilla/firefox/profiles.ini]")
28 parser.invoke(args)
74 29
75 if __name__ == '__main__': 30 if __name__ == '__main__':
76 main() 31 main()