Mercurial > mozilla > hg > ProfileManager
diff 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 |
line wrap: on
line diff
--- a/profilemanager/main.py Sun Apr 04 18:49:55 2010 -0400 +++ b/profilemanager/main.py Mon Apr 05 08:55:44 2010 -0700 @@ -4,73 +4,28 @@ import sys from manager import ProfileManager -from optparse import OptionGroup -from optparse import OptionParser -from command import commands, commandargs2str, command2parser - -# could go in commands -def print_help(parser): - parser.print_help() - # short descriptions for commands - command_descriptions = [dict(name=i, - description = commands[i]['doc'].strip().split('\n',1)[0]) - for i in sorted(commands.keys())] - max_len = max([len(i['name']) for i in command_descriptions]) - description = "Commands: \n%s" % ('\n'.join([' %s%s %s' % (description['name'], ' ' * (max_len - len(description['name'])), description['description']) - for description in command_descriptions])) - - print - print description - -def main(args=sys.argv[1:]): +from command import CommandParser - # global option parsing - usage = '%prog <options> command <command-options>' - parser = OptionParser(usage, description='run `%prog help` to display commands') - parser.add_option('-c', '--config', dest='config', - help="specify a profile.ini [default: $HOME/.mozilla/firefox/profiles.ini]") - parser.disable_interspersed_args() - options, args = parser.parse_args(args) - - # help/sanity check -- should probably be separated - if not len(args): - print_help(parser) - sys.exit(0) - if args[0] == 'help': - if len(args) == 2: - if args[1] in commands: - name = args[1] - commandparser = command2parser(name) - commandparser.print_help() - else: - parser.error("No command '%s'" % args[1]) - else: - print_help(parser) - sys.exit(0) - command = args[0] - if command not in commands: - parser.error("No command '%s'" % command) - - # XXX to move it its own method -- this is the only program-specific code +def create_profilemanager(parser, options): + """create the profile manager from parsed arguments""" if options.config is None: # XXX unix-specific options.config = os.path.join(os.environ['HOME'], '.mozilla/firefox/profiles.ini') if not os.path.exists(options.config): parser.error('%s does not exist' % options.config) - manager = ProfileManager(options.config) + return ProfileManager(options.config) + +def main(args=sys.argv[1:]): - # command specific args - name = args[0] - command = commands[name] - commandparser = command2parser(name) - command_options, command_args = commandparser.parse_args(args[1:]) - if len(command_args) < len(command['args']): - commandparser.error("Not enough arguments given") - if len(command_args) != len(command['args']) and not command['varargs']: - commandparser.error("Too many arguments given") - - # invoke the command - getattr(manager, name)(*command_args, **command_options.__dict__) + # global option parsing + commands = [ ProfileManager.clone, + ProfileManager.backup, + ProfileManager.restore, + ProfileManager.merge ] + parser = CommandParser(commands, setup=create_profilemanager) + parser.add_option('-c', '--config', dest='config', + help="specify a profile.ini [default: $HOME/.mozilla/firefox/profiles.ini]") + parser.invoke(args) if __name__ == '__main__': main()