changeset 592:74175ce83378

Restructure option handling -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Restructure option handling This allows global options to appear before or after the command name Also includes a patch from Andrew Thompson <andrewkt@aktzero.com> to catch invalid global options. manifest hash: 2a1285c0caf04ae79dca10cb899d183d84a6f3d4 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.0 (GNU/Linux) iD8DBQFCx80cywK+sNU5EO8RApJSAJ9U6ijOIbMDAd4lcahY6dXCTPcsNACeKuNT iVhCp9IvacwwuHjAFXsLJEQ= =Q9Qe -----END PGP SIGNATURE-----
author mpm@selenic.com
date Sun, 03 Jul 2005 03:33:48 -0800
parents eb46971fc57f
children ca3c499e94c6
files mercurial/commands.py
diffstat 1 files changed, 30 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/commands.py	Sat Jul 02 22:54:35 2005 -0800
+++ b/mercurial/commands.py	Sun Jul 03 03:33:48 2005 -0800
@@ -978,6 +978,12 @@
     sys.exit(dispatch(sys.argv[1:]))
 
 def dispatch(args):
+    signal.signal(signal.SIGTERM, catchterm)
+
+    def get_ui():
+        return ui.ui(options["verbose"], options["debug"], options["quiet"],
+                     not options["noninteractive"])
+
     options = {}
     opts = [('v', 'verbose', None, 'verbose'),
             ('', 'debug', None, 'debug'),
@@ -989,38 +995,54 @@
             ('', 'version', None, 'output version information and exit'),
             ]
 
-    args = fancyopts.fancyopts(args, opts, options,
-                               'hg [options] <command> [options] [files]')
+    try:
+        args = fancyopts.fancyopts(args, opts, options,
+                                   'hg [options] <command> [options] [files]')
+    except fancyopts.getopt.GetoptError, inst:
+        u = ui.ui()
+        u.warn("hg: %s\n" % (inst))
+        sys.exit(-1)
 
     if not args:
         cmd = "help"
     else:
         cmd, args = args[0], args[1:]
 
-    u = ui.ui(options["verbose"], options["debug"], options["quiet"],
-           not options["noninteractive"])
-
     if options["version"]:
-        show_version(u)
+        show_version(get_ui())
         sys.exit(0)
 
     try:
         i = find(cmd)
     except UnknownCommand:
+        u = get_ui()
         u.warn("hg: unknown command '%s'\n" % cmd)
         help(u)
         sys.exit(1)
 
-    signal.signal(signal.SIGTERM, catchterm)
+    # combine global options into local
+    c = list(i[1])
+    l = len(c)
+    for o in opts:
+        c.append((o[0], o[1], options[o[1]], o[3]))
 
     cmdoptions = {}
     try:
-        args = fancyopts.fancyopts(args, i[1], cmdoptions, i[2])
+        args = fancyopts.fancyopts(args, c, cmdoptions, i[2])
     except fancyopts.getopt.GetoptError, inst:
+        u = get_ui()
         u.warn("hg %s: %s\n" % (cmd, inst))
         help(u, cmd)
         sys.exit(-1)
 
+    # separate global options back out
+    for o in opts:
+        n = o[1]
+        options[n] = cmdoptions[n]
+        del cmdoptions[n]
+
+    u = get_ui()
+
     try:
         try:
             if cmd not in norepo.split():