[PATCH] commands: use a class as decorator to build table incrementally

Adrian Buehlmann adrian at cadifra.com
Wed May 11 12:41:20 CDT 2011


On 2011-05-11 19:27, Matt Mackall wrote:
> On Wed, 2011-05-11 at 18:13 +0200, Adrian Buehlmann wrote:
>> # HG changeset patch
>> # User Adrian Buehlmann <adrian at cadifra.com>
>> # Date 1305097807 -7200
>> # Node ID 8b331aca1943f7840591dd3289e55f99ba164375
>> # Parent  c97d8485b5fa46c94b8afcfab0cde97629ba4086
>> commands: use a class as decorator to build table incrementally
>>
>> this allows to define the table entries near the command function
> 
> Thanks, we probably should have done something like this years ago.
> 
>> diff --git a/mercurial/commands.py b/mercurial/commands.py
>> --- a/mercurial/commands.py
>> +++ b/mercurial/commands.py
>> @@ -17,8 +17,134 @@
>>  import dagparser, context, simplemerge
>>  import random, setdiscovery, treediscovery, dagutil
>>  
>> +table = {}
>> +
>> +class command(object):
>> +    '''decorator for populating the command table'''
>> +    def __init__(self, name, options, synopsis=None):
>> +        self.name = name
>> +        self.options = options
>> +        self.synopsis = synopsis
>> +
>> +    def __call__(self, func):
>> +        if self.synopsis:
>> +            table[self.name] = func, self.options, self.synopsis
>> +        else:
>> +            table[self.name] = func, self.options
>> +        return func
> 
> Does this want to live in cmdutil for use by extensions?

Good idea.

How about adding 'table' as a parameter on command.__init__ and then move
command to cmdutil?

I can modify the decoration calls to include that param, e.g.:

@@ -483,6 +650,13 @@
             cmdutil.bailifchanged(repo)
             return hg.clean(repo, node)
 
+ at command('bookmarks', table,
+    [('f', 'force', False, _('force')),
+    ('r', 'rev', '', _('revision'), _('REV')),
+    ('d', 'delete', False, _('delete a given bookmark')),
+    ('m', 'rename', '', _('rename a given bookmark'), _('NAME')),
+    ('i', 'inactive', False, _('do not mark a new bookmark active'))],
+    _('hg bookmarks [-f] [-d] [-i] [-m NAME] [-r REV] [NAME]'))
 def bookmark(ui, repo, mark=None, rev=None, force=False, delete=False,
              rename=None, inactive=False):
     '''track a line of development with movable markers


More information about the Mercurial-devel mailing list