[PATCH 3 of 3 RFC] revset: port extra() to support keyword arguments

Yuya Nishihara yuya at tcha.org
Mon Jun 29 09:51:31 CDT 2015


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1435499853 -32400
#      Sun Jun 28 22:57:33 2015 +0900
# Node ID a38d5f52cc3695e9b32f5f2ddc2d505cb4505607
# Parent  acaa4d55a0524e607e354cc873b4008f4fe48f70
revset: port extra() to support keyword arguments

This is an example to show how keyword arguments are processed.

diff --git a/mercurial/revset.py b/mercurial/revset.py
--- a/mercurial/revset.py
+++ b/mercurial/revset.py
@@ -840,16 +840,19 @@ def extra(repo, subset, x):
     a regular expression. To match a value that actually starts with `re:`,
     use the prefix `literal:`.
     """
-
+    args = getkwargs(x, 'extra', 'label value')
+    if 'label' not in args:
+        # i18n: "extra" is a keyword
+        raise error.ParseError(_('extra takes at least 1 argument'))
     # i18n: "extra" is a keyword
-    l = getargs(x, 1, 2, _('extra takes at least 1 and at most 2 arguments'))
-    # i18n: "extra" is a keyword
-    label = getstring(l[0], _('first argument to extra must be a string'))
+    label = getstring(args['label'], _('first argument to extra must be '
+                                       'a string'))
     value = None
 
-    if len(l) > 1:
+    if 'value' in args:
         # i18n: "extra" is a keyword
-        value = getstring(l[1], _('second argument to extra must be a string'))
+        value = getstring(args['value'], _('second argument to extra must be '
+                                           'a string'))
         kind, value, matcher = _stringmatcher(value)
 
     def _matchvalue(r):
diff --git a/tests/test-revset.t b/tests/test-revset.t
--- a/tests/test-revset.t
+++ b/tests/test-revset.t
@@ -324,6 +324,25 @@ quoting needed
 
 keyword arguments
 
+  $ log 'extra(branch, value=a)'
+  0
+
+  $ log 'extra(branch, a, b)'
+  hg: parse error: extra takes at most 2 arguments
+  [255]
+  $ log 'extra(a, label=b)'
+  hg: parse error: extra got multiple values for keyword argument 'label'
+  [255]
+  $ log 'extra(label=branch, default)'
+  hg: parse error: extra got an invalid argument
+  [255]
+  $ log 'extra(branch, foo+bar=baz)'
+  hg: parse error: extra got an invalid argument
+  [255]
+  $ log 'extra(unknown=branch)'
+  hg: parse error: extra got an unexpected keyword argument 'unknown'
+  [255]
+
   $ try 'foo=bar|baz'
   (keyvalue
     ('symbol', 'foo')


More information about the Mercurial-devel mailing list