comparison mercurial/cmdutil.py @ 4622:6fc26982f203

dispatch: fix handling of incorrect number of arguments
author Matt Mackall <mpm@selenic.com>
date Mon, 18 Jun 2007 13:24:34 -0500
parents 5fd7cc897542
children e6d105a51ec7
comparison
equal deleted inserted replaced
4621:d97fd22a0ea9 4622:6fc26982f203
115 ui.warn(_(" empty string\n")) 115 ui.warn(_(" empty string\n"))
116 else: 116 else:
117 ui.warn("\n%r\n" % util.ellipsis(inst[1])) 117 ui.warn("\n%r\n" % util.ellipsis(inst[1]))
118 except util.Abort, inst: 118 except util.Abort, inst:
119 ui.warn(_("abort: %s\n") % inst) 119 ui.warn(_("abort: %s\n") % inst)
120 except TypeError, inst:
121 # was this an argument error?
122 tb = traceback.extract_tb(sys.exc_info()[2])
123 if len(tb) > 2: # no
124 raise
125 ui.debug(inst, "\n")
126 ui.warn(_("%s: invalid arguments\n") % cmd)
127 commands.help_(ui, cmd)
128 except SystemExit, inst: 120 except SystemExit, inst:
129 # Commands shouldn't sys.exit directly, but give a return code. 121 # Commands shouldn't sys.exit directly, but give a return code.
130 # Just in case catch this and and pass exit code to caller. 122 # Just in case catch this and and pass exit code to caller.
131 return inst.code 123 return inst.code
132 except: 124 except:
322 raise 314 raise
323 d = lambda: func(ui, repo, *args, **cmdoptions) 315 d = lambda: func(ui, repo, *args, **cmdoptions)
324 else: 316 else:
325 d = lambda: func(ui, *args, **cmdoptions) 317 d = lambda: func(ui, *args, **cmdoptions)
326 318
327 return runcommand(ui, options, d) 319 return runcommand(ui, options, cmd, d)
328 320
329 def runcommand(ui, options, cmdfunc): 321 def runcommand(ui, options, cmd, cmdfunc):
322 def checkargs():
323 try:
324 return cmdfunc()
325 except TypeError, inst:
326 # was this an argument error?
327 tb = traceback.extract_tb(sys.exc_info()[2])
328 if len(tb) != 2: # no
329 raise
330 raise ParseError(cmd, _("invalid arguments"))
331
330 if options['profile']: 332 if options['profile']:
331 import hotshot, hotshot.stats 333 import hotshot, hotshot.stats
332 prof = hotshot.Profile("hg.prof") 334 prof = hotshot.Profile("hg.prof")
333 try: 335 try:
334 try: 336 try:
335 return prof.runcall(cmdfunc) 337 return prof.runcall(checkargs)
336 except: 338 except:
337 try: 339 try:
338 ui.warn(_('exception raised - generating ' 340 ui.warn(_('exception raised - generating '
339 'profile anyway\n')) 341 'profile anyway\n'))
340 except: 342 except:
354 'lsprof not available - install from ' 356 'lsprof not available - install from '
355 'http://codespeak.net/svn/user/arigo/hack/misc/lsprof/')) 357 'http://codespeak.net/svn/user/arigo/hack/misc/lsprof/'))
356 p = lsprof.Profiler() 358 p = lsprof.Profiler()
357 p.enable(subcalls=True) 359 p.enable(subcalls=True)
358 try: 360 try:
359 return cmdfunc() 361 return checkargs()
360 finally: 362 finally:
361 p.disable() 363 p.disable()
362 stats = lsprof.Stats(p.getstats()) 364 stats = lsprof.Stats(p.getstats())
363 stats.sort() 365 stats.sort()
364 stats.pprint(top=10, file=sys.stderr, climit=5) 366 stats.pprint(top=10, file=sys.stderr, climit=5)
365 else: 367 else:
366 return cmdfunc() 368 return checkargs()
367 369
368 def bail_if_changed(repo): 370 def bail_if_changed(repo):
369 modified, added, removed, deleted = repo.status()[:4] 371 modified, added, removed, deleted = repo.status()[:4]
370 if modified or added or removed or deleted: 372 if modified or added or removed or deleted:
371 raise util.Abort(_("outstanding uncommitted changes")) 373 raise util.Abort(_("outstanding uncommitted changes"))