# HG changeset patch
# User Yuya Nishihara <y...@tcha.org>
# Date 1483951511 -32400
#      Mon Jan 09 17:45:11 2017 +0900
# Node ID 9761a2fa220cfd0c84513a2b2bdda80f700ab634
# Parent  029a72c55b871155beaeadccbaa8652bf4e5d7ce
revset: add default value to getinteger() helper

This seems handy.

diff --git a/mercurial/revset.py b/mercurial/revset.py
--- a/mercurial/revset.py
+++ b/mercurial/revset.py
@@ -302,6 +302,8 @@ def tokenize(program, lookup=None, symin
 
 # helpers
 
+_notset = object()
+
 def getsymbol(x):
     if x and x[0] == 'symbol':
         return x[1]
@@ -312,7 +314,9 @@ def getstring(x, err):
         return x[1]
     raise error.ParseError(err)
 
-def getinteger(x, err):
+def getinteger(x, err, default=_notset):
+    if not x and default is not _notset:
+        return default
     try:
         return int(getstring(x, err))
     except ValueError:
@@ -1274,13 +1278,10 @@ def limit(repo, subset, x):
     if 'set' not in args:
         # i18n: "limit" is a keyword
         raise error.ParseError(_("limit requires one to three arguments"))
-    lim, ofs = 1, 0
-    if 'n' in args:
-        # i18n: "limit" is a keyword
-        lim = getinteger(args['n'], _("limit expects a number"))
-    if 'offset' in args:
-        # i18n: "limit" is a keyword
-        ofs = getinteger(args['offset'], _("limit expects a number"))
+    # i18n: "limit" is a keyword
+    lim = getinteger(args.get('n'), _("limit expects a number"), default=1)
+    # i18n: "limit" is a keyword
+    ofs = getinteger(args.get('offset'), _("limit expects a number"), 
default=0)
     if ofs < 0:
         raise error.ParseError(_("negative offset"))
     os = getset(repo, fullreposet(repo), args['set'])
_______________________________________________
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel

Reply via email to