[PATCH 1 of 5] dispatch: alias --repo to --repository while parsing early options

Yuya Nishihara yuya at tcha.org
Sat Dec 2 02:00:39 EST 2017


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1511597032 -32400
#      Sat Nov 25 17:03:52 2017 +0900
# Node ID 44930515ee3b612477d8b0b5cfe8d24057351a2f
# Parent  2523ab8ba69dfbfd9832f115d9bbc1f9b9ce4745
dispatch: alias --repo to --repository while parsing early options

This prepares for replacing old _early*opt() functions. My initial attempt
was to extend options table to support 'repository|repo' syntax. It worked,
but seemed too invasive. So I decided to add an optional argument to
fancyopts() instead.

This also changes the nevernegate dict to be keyed by a canonical_name,
not by an option-name for clarity.

diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py
--- a/mercurial/dispatch.py
+++ b/mercurial/dispatch.py
@@ -649,7 +649,8 @@ def _parseconfig(ui, config):
 def _earlyparseopts(args):
     options = {}
     fancyopts.fancyopts(args, commands.globalopts, options,
-                        gnu=False, early=True)
+                        gnu=False, early=True,
+                        optaliases={'repository': ['repo']})
     return options
 
 def _earlygetopt(aliases, args, strip=True):
diff --git a/mercurial/fancyopts.py b/mercurial/fancyopts.py
--- a/mercurial/fancyopts.py
+++ b/mercurial/fancyopts.py
@@ -226,7 +226,7 @@ def gnugetopt(args, options, longoptions
     return opts, args
 
 
-def fancyopts(args, options, state, gnu=False, early=False):
+def fancyopts(args, options, state, gnu=False, early=False, optaliases=None):
     """
     read args, parse options, and store options in state
 
@@ -246,8 +246,15 @@ def fancyopts(args, options, state, gnu=
       integer - parameter strings is stored as int
       function - call function with parameter
 
+    optaliases is a mapping from a canonical option name to a list of
+    additional long options. This exists for preserving backward compatibility
+    of early options. If we want to use it extensively, please consider moving
+    the functionality to the options table (e.g separate long options by '|'.)
+
     non-option args are returned
     """
+    if optaliases is None:
+        optaliases = {}
     namelist = []
     shortlist = ''
     argmap = {}
@@ -261,10 +268,13 @@ def fancyopts(args, options, state, gnu=
         else:
             short, name, default, comment = option
         # convert opts to getopt format
-        oname = name
+        onames = [name]
+        onames.extend(optaliases.get(name, []))
         name = name.replace('-', '_')
 
-        argmap['-' + short] = argmap['--' + oname] = name
+        argmap['-' + short] = name
+        for n in onames:
+            argmap['--' + n] = name
         defmap[name] = default
 
         # copy defaults to state
@@ -279,24 +289,24 @@ def fancyopts(args, options, state, gnu=
         if not (default is None or default is True or default is False):
             if short:
                 short += ':'
-            if oname:
-                oname += '='
-        elif oname not in nevernegate:
-            if oname.startswith('no-'):
-                insert = oname[3:]
-            else:
-                insert = 'no-' + oname
-            # backout (as a practical example) has both --commit and
-            # --no-commit options, so we don't want to allow the
-            # negations of those flags.
-            if insert not in alllong:
-                assert ('--' + oname) not in negations
-                negations['--' + insert] = '--' + oname
-                namelist.append(insert)
+            onames = [n + '=' for n in onames]
+        elif name not in nevernegate:
+            for n in onames:
+                if n.startswith('no-'):
+                    insert = n[3:]
+                else:
+                    insert = 'no-' + n
+                # backout (as a practical example) has both --commit and
+                # --no-commit options, so we don't want to allow the
+                # negations of those flags.
+                if insert not in alllong:
+                    assert ('--' + n) not in negations
+                    negations['--' + insert] = '--' + n
+                    namelist.append(insert)
         if short:
             shortlist += short
         if name:
-            namelist.append(oname)
+            namelist.extend(onames)
 
     # parse arguments
     if early:
diff --git a/tests/test-dispatch.t b/tests/test-dispatch.t
--- a/tests/test-dispatch.t
+++ b/tests/test-dispatch.t
@@ -149,6 +149,10 @@ Early options must come first if HGPLAIN
   [255]
   $ HGPLAIN=+strictflags hg --cwd .. -q -Ra log -b default
   0:cb9a9f314b8b
+  $ HGPLAIN=+strictflags hg --cwd .. -q --repository a log -b default
+  0:cb9a9f314b8b
+  $ HGPLAIN=+strictflags hg --cwd .. -q --repo a log -b default
+  0:cb9a9f314b8b
 
 For compatibility reasons, HGPLAIN=+strictflags is not enabled by plain HGPLAIN:
 


More information about the Mercurial-devel mailing list