templater: port pad() to take keyword arguments
authorYuya Nishihara <yuya@tcha.org>
Mon, 03 Apr 2017 22:23:52 +0900
changeset 31887 f7b3677f66cd
parent 31886 bdda942f4b9c
child 31888 1c398f7f4aa4
templater: port pad() to take keyword arguments This is another example where keyword arguments can be actually useful.
mercurial/templater.py
tests/test-command-template.t
--- a/mercurial/templater.py	Mon Apr 03 21:22:39 2017 +0900
+++ b/mercurial/templater.py	Mon Apr 03 22:23:52 2017 +0900
@@ -589,29 +589,30 @@
         return node
     return templatefilters.short(node)
 
-@templatefunc('pad(text, width[, fillchar=\' \'[, left=False]])')
+@templatefunc('pad(text, width[, fillchar=\' \'[, left=False]])',
+              argspec='text width fillchar left')
 def pad(context, mapping, args):
     """Pad text with a
     fill character."""
-    if not (2 <= len(args) <= 4):
+    if 'text' not in args or 'width' not in args:
         # i18n: "pad" is a keyword
         raise error.ParseError(_("pad() expects two to four arguments"))
 
-    width = evalinteger(context, mapping, args[1],
+    width = evalinteger(context, mapping, args['width'],
                         # i18n: "pad" is a keyword
                         _("pad() expects an integer width"))
 
-    text = evalstring(context, mapping, args[0])
+    text = evalstring(context, mapping, args['text'])
 
     left = False
     fillchar = ' '
-    if len(args) > 2:
-        fillchar = evalstring(context, mapping, args[2])
+    if 'fillchar' in args:
+        fillchar = evalstring(context, mapping, args['fillchar'])
         if len(color.stripeffects(fillchar)) != 1:
             # i18n: "pad" is a keyword
             raise error.ParseError(_("pad() expects a single fill character"))
-    if len(args) > 3:
-        left = evalboolean(context, mapping, args[3])
+    if 'left' in args:
+        left = evalboolean(context, mapping, args['left'])
 
     fillwidth = width - encoding.colwidth(color.stripeffects(text))
     if fillwidth <= 0:
--- a/tests/test-command-template.t	Mon Apr 03 21:22:39 2017 +0900
+++ b/tests/test-command-template.t	Mon Apr 03 22:23:52 2017 +0900
@@ -146,6 +146,9 @@
   hg: parse error: can't use a key-value pair in this context
   [255]
 
+  $ hg debugtemplate '{pad("foo", width=10, left=true)}\n'
+         foo
+
 Call function which takes named arguments by filter syntax:
 
   $ hg debugtemplate '{" "|separate}'