# HG changeset patch # User Jeff Hammel # Date 1274925394 25200 # Node ID 321721b581f1e549436b3e77c1420c80fd3137d7 # Parent 448c248b3738a402661a2d63f329c745f9fe4c72 general improvements to the command parser diff -r 448c248b3738 -r 321721b581f1 hq/command.py --- a/hq/command.py Wed May 26 08:18:36 2010 -0700 +++ b/hq/command.py Wed May 26 18:56:34 2010 -0700 @@ -13,15 +13,17 @@ def __init__(self, _class, description=None): self._class = _class self.commands = {} - usage = '%prog [options] command [command-options]' - description = description or _class.__doc__ + usage = '%prog [options] [command-options]' + description = description or _class.__doc__.strip() + description += ' Use `%prog help ` to display the usage of a command' + OptionParser.__init__(self, usage=usage, description=description) commands = [ getattr(_class, i) for i in dir(_class) if not i.startswith('_') ] commands = [ method for method in commands if hasattr(method, '__call__') ] - for _command in commands: - self.command(_command) + for command in commands: + self.add_command(command) self.disable_interspersed_args() def print_help(self): @@ -74,7 +76,10 @@ name, args = self.parse(args) # setup - _object = self._class(self, self.options) + options = self.options.__dict__.copy() + _object = self._class(**options) + # XXX should only pass values in options that self._class.__init__ + # needs/wants # command specific args command = self.commands[name] @@ -95,6 +100,10 @@ pprint(retval) return retval + def add_command(self, function): + command = self.command(function) + self.commands[command['name']] = command + def command(self, function): name = function.func_name if function.__doc__: @@ -109,12 +118,12 @@ else: args = argspec.args[1:] optional = None - self.commands[name] = { 'doc': doc, - 'args': args, - 'optional': optional, - 'varargs': argspec.varargs - } - return function # XXX to restructure??? + return { 'name': name, + 'doc': doc, + 'args': args, + 'optional': optional, + 'varargs': argspec.varargs + } def commandargs2str(self, command): if isinstance(command, basestring): @@ -142,7 +151,7 @@ doc = [] option = None for line in lines: - if not line and option: # blank lines terminate [?] + if not line and option: # blank lines terminate [???] break if line.startswith(decoration) and delimeter in line: name, description = line.split(delimeter, 1) @@ -160,11 +169,13 @@ return ('\n'.join(doc), argdict) def command2parser(self, command): - doc, argdict = self.doc2arghelp(self.commands[command]['doc']) - parser = OptionParser('%%prog %s %s' % (command, self.commandargs2str(command)), + if isinstance(command, basestring): + command = self.commands[command] + doc, argdict = self.doc2arghelp(command['doc']) + parser = OptionParser('%%prog %s %s' % (command['name'], self.commandargs2str(command['name'])), description=doc, add_help_option=False) - if self.commands[command]['optional']: - for key, value in self.commands[command]['optional'].items(): + if command['optional']: + for key, value in command['optional'].items(): help = argdict.get(key, '') if value is True: parser.add_option('--no-%s' % key, dest=key, diff -r 448c248b3738 -r 321721b581f1 hq/main.py --- a/hq/main.py Wed May 26 08:18:36 2010 -0700 +++ b/hq/main.py Wed May 26 18:56:34 2010 -0700 @@ -23,7 +23,7 @@ mercurial queue extension front-end policy manager """ - def __init__(self, queue_host=None, network=True) + def __init__(self, queue_host=None, network=True): """initialize global options""" # TODO: look at hgrc file # for [defaults] repository_host @@ -55,12 +55,6 @@ # create a new patch call(['hg', 'qnew', patch]) - def edit(self, patch=None): - """ - edit a patch - - patch: the patch to edit - """ - def commit(self, message): """ commit a patch and push it to the master repository