dispatch: fix handling of incorrect number of arguments
authorMatt Mackall <mpm@selenic.com>
Mon, 18 Jun 2007 13:24:34 -0500
changeset 4621 6fc26982f203
parent 4620 d97fd22a0ea9
child 4622 fff50306e6dd
dispatch: fix handling of incorrect number of arguments
mercurial/cmdutil.py
tests/test-dispatch
tests/test-dispatch.out
--- a/mercurial/cmdutil.py	Mon Jun 18 13:24:34 2007 -0500
+++ b/mercurial/cmdutil.py	Mon Jun 18 13:24:34 2007 -0500
@@ -117,14 +117,6 @@
             ui.warn("\n%r\n" % util.ellipsis(inst[1]))
     except util.Abort, inst:
         ui.warn(_("abort: %s\n") % inst)
-    except TypeError, inst:
-        # was this an argument error?
-        tb = traceback.extract_tb(sys.exc_info()[2])
-        if len(tb) > 2: # no
-            raise
-        ui.debug(inst, "\n")
-        ui.warn(_("%s: invalid arguments\n") % cmd)
-        commands.help_(ui, cmd)
     except SystemExit, inst:
         # Commands shouldn't sys.exit directly, but give a return code.
         # Just in case catch this and and pass exit code to caller.
@@ -324,15 +316,25 @@
     else:
         d = lambda: func(ui, *args, **cmdoptions)
 
-    return runcommand(ui, options, d)
+    return runcommand(ui, options, cmd, d)
 
-def runcommand(ui, options, cmdfunc):
+def runcommand(ui, options, cmd, cmdfunc):
+    def checkargs():
+        try:
+            return cmdfunc()
+        except TypeError, inst:
+            # was this an argument error?
+            tb = traceback.extract_tb(sys.exc_info()[2])
+            if len(tb) != 2: # no
+                raise
+            raise ParseError(cmd, _("invalid arguments"))
+
     if options['profile']:
         import hotshot, hotshot.stats
         prof = hotshot.Profile("hg.prof")
         try:
             try:
-                return prof.runcall(cmdfunc)
+                return prof.runcall(checkargs)
             except:
                 try:
                     ui.warn(_('exception raised - generating '
@@ -356,14 +358,14 @@
         p = lsprof.Profiler()
         p.enable(subcalls=True)
         try:
-            return cmdfunc()
+            return checkargs()
         finally:
             p.disable()
             stats = lsprof.Stats(p.getstats())
             stats.sort()
             stats.pprint(top=10, file=sys.stderr, climit=5)
     else:
-        return cmdfunc()
+        return checkargs()
 
 def bail_if_changed(repo):
     modified, added, removed, deleted = repo.status()[:4]
--- a/tests/test-dispatch	Mon Jun 18 13:24:34 2007 -0500
+++ b/tests/test-dispatch	Mon Jun 18 13:24:34 2007 -0500
@@ -6,6 +6,9 @@
 echo a > a
 hg ci -Ama -d '0 0'
 
+echo "# missing arg"
+hg cat
+
 echo '% [defaults]'
 hg cat a
 cat > $HGRCPATH <<EOF
@@ -13,3 +16,4 @@
 cat = -v
 EOF
 hg cat a
+
--- a/tests/test-dispatch.out	Mon Jun 18 13:24:34 2007 -0500
+++ b/tests/test-dispatch.out	Mon Jun 18 13:24:34 2007 -0500
@@ -1,4 +1,30 @@
 adding a
+# missing arg
+hg cat: invalid arguments
+hg cat [OPTION]... FILE...
+
+output the current or given revision of files
+
+    Print the specified files as they were at the given revision.
+    If no revision is given, the parent of the working directory is used,
+    or tip if no revision is checked out.
+
+    Output may be to a file, in which case the name of the file is
+    given using a format string.  The formatting rules are the same as
+    for the export command, with the following additions:
+
+    %s   basename of file being printed
+    %d   dirname of file being printed, or '.' if in repo root
+    %p   root-relative path name of file being printed
+
+options:
+
+ -o --output   print output to file with formatted name
+ -r --rev      print the given revision
+ -I --include  include names matching the given patterns
+ -X --exclude  exclude names matching the given patterns
+
+use "hg -v help cat" to show global options
 % [defaults]
 a
 a