Patchwork [3,of,5] revset: add default value to getinteger() helper

login
register
mail settings
Submitter Yuya Nishihara
Date Jan. 13, 2017, 3:15 p.m.
Message ID <9761a2fa220cfd0c8451.1484320552@mimosa>
Download mbox | patch
Permalink /patch/18204/
State Accepted
Headers show

Comments

Yuya Nishihara - Jan. 13, 2017, 3:15 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@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.

Patch

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'])