comparison mercurial/revset.py @ 30802:5eb3e4568c94

revset: add default value to getinteger() helper This seems handy.
author Yuya Nishihara <yuya@tcha.org>
date Mon, 09 Jan 2017 17:45:11 +0900
parents 67ee7874e53b
children d389f19f14aa
comparison
equal deleted inserted replaced
30801:67ee7874e53b 30802:5eb3e4568c94
300 pos += 1 300 pos += 1
301 yield ('end', None, pos) 301 yield ('end', None, pos)
302 302
303 # helpers 303 # helpers
304 304
305 _notset = object()
306
305 def getsymbol(x): 307 def getsymbol(x):
306 if x and x[0] == 'symbol': 308 if x and x[0] == 'symbol':
307 return x[1] 309 return x[1]
308 raise error.ParseError(_('not a symbol')) 310 raise error.ParseError(_('not a symbol'))
309 311
310 def getstring(x, err): 312 def getstring(x, err):
311 if x and (x[0] == 'string' or x[0] == 'symbol'): 313 if x and (x[0] == 'string' or x[0] == 'symbol'):
312 return x[1] 314 return x[1]
313 raise error.ParseError(err) 315 raise error.ParseError(err)
314 316
315 def getinteger(x, err): 317 def getinteger(x, err, default=_notset):
318 if not x and default is not _notset:
319 return default
316 try: 320 try:
317 return int(getstring(x, err)) 321 return int(getstring(x, err))
318 except ValueError: 322 except ValueError:
319 raise error.ParseError(err) 323 raise error.ParseError(err)
320 324
1272 """ 1276 """
1273 args = getargsdict(x, 'limit', 'set n offset') 1277 args = getargsdict(x, 'limit', 'set n offset')
1274 if 'set' not in args: 1278 if 'set' not in args:
1275 # i18n: "limit" is a keyword 1279 # i18n: "limit" is a keyword
1276 raise error.ParseError(_("limit requires one to three arguments")) 1280 raise error.ParseError(_("limit requires one to three arguments"))
1277 lim, ofs = 1, 0 1281 # i18n: "limit" is a keyword
1278 if 'n' in args: 1282 lim = getinteger(args.get('n'), _("limit expects a number"), default=1)
1279 # i18n: "limit" is a keyword 1283 # i18n: "limit" is a keyword
1280 lim = getinteger(args['n'], _("limit expects a number")) 1284 ofs = getinteger(args.get('offset'), _("limit expects a number"), default=0)
1281 if 'offset' in args:
1282 # i18n: "limit" is a keyword
1283 ofs = getinteger(args['offset'], _("limit expects a number"))
1284 if ofs < 0: 1285 if ofs < 0:
1285 raise error.ParseError(_("negative offset")) 1286 raise error.ParseError(_("negative offset"))
1286 os = getset(repo, fullreposet(repo), args['set']) 1287 os = getset(repo, fullreposet(repo), args['set'])
1287 result = [] 1288 result = []
1288 it = iter(os) 1289 it = iter(os)