diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py
index c9b2a4d69e66884603c9345466ab842149e14dc7_bWVyY3VyaWFsL2Rpc3BhdGNoLnB5..9c9e0b4b2ca7df1c8764466b9ca3670935bacd47_bWVyY3VyaWFsL2Rpc3BhdGNoLnB5 100644
--- a/mercurial/dispatch.py
+++ b/mercurial/dispatch.py
@@ -470,6 +470,7 @@
     config parsing and commands. besides, use handlecommandexception to handle
     uncaught exceptions.
     """
+    detailed_exit_code = -1
     try:
         return scmutil.callcatch(ui, func)
     except error.AmbiguousCommand as inst:
@@ -473,8 +474,9 @@
     try:
         return scmutil.callcatch(ui, func)
     except error.AmbiguousCommand as inst:
+        detailed_exit_code = 10
         ui.warn(
             _(b"hg: command '%s' is ambiguous:\n    %s\n")
             % (inst.prefix, b" ".join(inst.matches))
         )
     except error.CommandError as inst:
@@ -476,8 +478,9 @@
         ui.warn(
             _(b"hg: command '%s' is ambiguous:\n    %s\n")
             % (inst.prefix, b" ".join(inst.matches))
         )
     except error.CommandError as inst:
+        detailed_exit_code = 10
         if inst.command:
             ui.pager(b'help')
             msgbytes = pycompat.bytestr(inst.message)
@@ -487,6 +490,7 @@
             ui.warn(_(b"hg: %s\n") % inst.message)
             ui.warn(_(b"(use 'hg help -v' for a list of global options)\n"))
     except error.UnknownCommand as inst:
+        detailed_exit_code = 10
         nocmdmsg = _(b"hg: unknown command '%s'\n") % inst.command
         try:
             # check if the command is in a disabled extension
@@ -515,7 +519,10 @@
         if not handlecommandexception(ui):
             raise
 
-    return -1
+    if ui.configbool(b'ui', b'detailed-exit-code'):
+        return detailed_exit_code
+    else:
+        return -1
 
 
 def aliasargs(fn, givenargs):
diff --git a/tests/test-alias.t b/tests/test-alias.t
index c9b2a4d69e66884603c9345466ab842149e14dc7_dGVzdHMvdGVzdC1hbGlhcy50..9c9e0b4b2ca7df1c8764466b9ca3670935bacd47_dGVzdHMvdGVzdC1hbGlhcy50 100644
--- a/tests/test-alias.t
+++ b/tests/test-alias.t
@@ -468,9 +468,9 @@
   $ hg i
   hg: command 'i' is ambiguous:
       idalias idaliaslong idaliasshell identify import incoming init
-  [255]
+  [10]
   $ hg id
   042423737847 tip
   $ hg ida
   hg: command 'ida' is ambiguous:
       idalias idaliaslong idaliasshell
@@ -472,9 +472,9 @@
   $ hg id
   042423737847 tip
   $ hg ida
   hg: command 'ida' is ambiguous:
       idalias idaliaslong idaliasshell
-  [255]
+  [10]
   $ hg idalias
   042423737847 tip
   $ hg idaliasl
@@ -484,7 +484,7 @@
   $ hg parentsshell
   hg: command 'parentsshell' is ambiguous:
       parentsshell1 parentsshell2
-  [255]
+  [10]
   $ hg parentsshell1
   one
   $ hg parentsshell2
@@ -533,7 +533,7 @@
   $ hg --cwd .. subalias > /dev/null
   hg: unknown command 'subalias'
   (did you mean idalias?)
-  [255]
+  [10]
   $ hg -R .. subalias > /dev/null
   hg: unknown command 'subalias'
   (did you mean idalias?)
@@ -537,7 +537,7 @@
   $ hg -R .. subalias > /dev/null
   hg: unknown command 'subalias'
   (did you mean idalias?)
-  [255]
+  [10]
 
 
 shell alias defined in other repo
@@ -545,7 +545,7 @@
   $ hg mainalias > /dev/null
   hg: unknown command 'mainalias'
   (did you mean idalias?)
-  [255]
+  [10]
   $ hg -R .. mainalias
   main
   $ hg --cwd .. mainalias
@@ -555,7 +555,7 @@
   $ hg --cwd .. manalias
   hg: unknown command 'manalias'
   (did you mean one of idalias, mainalias, manifest?)
-  [255]
+  [10]
 
 shell aliases with escaped $ chars
 
@@ -593,7 +593,7 @@
   $ hg reba
   hg: command 'reba' is ambiguous:
       rebase rebate
-  [255]
+  [10]
   $ hg rebat
   this is rebate
   $ hg rebat --foo-bar
@@ -650,10 +650,10 @@
    -T --template TEMPLATE display with template
   
   (use 'hg rt -h' to show more help)
-  [255]
+  [10]
 
 invalid global arguments for normal commands, aliases, and shell aliases
 
   $ hg --invalid root
   hg: option --invalid not recognized
   (use 'hg help -v' for a list of global options)
@@ -654,10 +654,10 @@
 
 invalid global arguments for normal commands, aliases, and shell aliases
 
   $ hg --invalid root
   hg: option --invalid not recognized
   (use 'hg help -v' for a list of global options)
-  [255]
+  [10]
   $ hg --invalid mylog
   hg: option --invalid not recognized
   (use 'hg help -v' for a list of global options)
@@ -661,7 +661,7 @@
   $ hg --invalid mylog
   hg: option --invalid not recognized
   (use 'hg help -v' for a list of global options)
-  [255]
+  [10]
   $ hg --invalid blank
   hg: option --invalid not recognized
   (use 'hg help -v' for a list of global options)
@@ -665,7 +665,7 @@
   $ hg --invalid blank
   hg: option --invalid not recognized
   (use 'hg help -v' for a list of global options)
-  [255]
+  [10]
 
 environment variable changes in alias commands
 
diff --git a/tests/test-basic.t b/tests/test-basic.t
index c9b2a4d69e66884603c9345466ab842149e14dc7_dGVzdHMvdGVzdC1iYXNpYy50..9c9e0b4b2ca7df1c8764466b9ca3670935bacd47_dGVzdHMvdGVzdC1iYXNpYy50 100644
--- a/tests/test-basic.t
+++ b/tests/test-basic.t
@@ -67,8 +67,8 @@
   $ hg unknown -q 1>&-
   hg: unknown command 'unknown'
   (did you mean debugknown?)
-  [255]
+  [10]
 
   $ hg version -q 2>&-
   Mercurial Distributed SCM * (glob)
   $ hg unknown -q 2>&-
@@ -71,8 +71,8 @@
 
   $ hg version -q 2>&-
   Mercurial Distributed SCM * (glob)
   $ hg unknown -q 2>&-
-  [255]
+  [10]
 
   $ hg commit -m test
 
diff --git a/tests/test-completion.t b/tests/test-completion.t
index c9b2a4d69e66884603c9345466ab842149e14dc7_dGVzdHMvdGVzdC1jb21wbGV0aW9uLnQ=..9c9e0b4b2ca7df1c8764466b9ca3670935bacd47_dGVzdHMvdGVzdC1jb21wbGV0aW9uLnQ= 100644
--- a/tests/test-completion.t
+++ b/tests/test-completion.t
@@ -242,7 +242,7 @@
   $ hg debugcomplete --options s
   hg: command 's' is ambiguous:
       serve shelve showconfig status summary
-  [255]
+  [10]
 
 Show all commands + options
   $ hg debugcommands
diff --git a/tests/test-dispatch.t b/tests/test-dispatch.t
index c9b2a4d69e66884603c9345466ab842149e14dc7_dGVzdHMvdGVzdC1kaXNwYXRjaC50..9c9e0b4b2ca7df1c8764466b9ca3670935bacd47_dGVzdHMvdGVzdC1kaXNwYXRjaC50 100644
--- a/tests/test-dispatch.t
+++ b/tests/test-dispatch.t
@@ -29,7 +29,7 @@
    -T --template TEMPLATE   display with template
   
   (use 'hg cat -h' to show more help)
-  [255]
+  [10]
 
 Missing parameter for early option:
 
@@ -110,7 +110,7 @@
   hg log: option -b not recognized
   error in definition for alias 'log': --config may only be given on the command
   line
-  [255]
+  [10]
 
   $ hg log -b '--config=defaults.log=--config=hooks.pre-log=false'
   abort: option --config may not be abbreviated
diff --git a/tests/test-extension.t b/tests/test-extension.t
index c9b2a4d69e66884603c9345466ab842149e14dc7_dGVzdHMvdGVzdC1leHRlbnNpb24udA==..9c9e0b4b2ca7df1c8764466b9ca3670935bacd47_dGVzdHMvdGVzdC1leHRlbnNpb24udA== 100644
--- a/tests/test-extension.t
+++ b/tests/test-extension.t
@@ -1018,7 +1018,7 @@
   multirevs command
   
   (use 'hg multirevs -h' to show more help)
-  [255]
+  [10]
 
 
 
@@ -1848,7 +1848,7 @@
   *** (use @command decorator to register 'deprecatedcmd')
   hg: unknown command 'deprecatedcmd'
   (use 'hg help' for a list of commands)
-  [255]
+  [10]
 
  the extension shouldn't be loaded at all so the mq works:
 
@@ -1905,4 +1905,4 @@
   *** (use b'' to make it byte string)
   hg: unknown command 'dummy'
   (did you mean summary?)
-  [255]
+  [10]
diff --git a/tests/test-fastannotate.t b/tests/test-fastannotate.t
index c9b2a4d69e66884603c9345466ab842149e14dc7_dGVzdHMvdGVzdC1mYXN0YW5ub3RhdGUudA==..9c9e0b4b2ca7df1c8764466b9ca3670935bacd47_dGVzdHMvdGVzdC1mYXN0YW5ub3RhdGUudA== 100644
--- a/tests/test-fastannotate.t
+++ b/tests/test-fastannotate.t
@@ -123,7 +123,7 @@
   $ hg fastannotate --config fastannotate.modes=fctx -h -q
   hg: unknown command 'fastannotate'
   (did you mean *) (glob)
-  [255]
+  [10]
 
 rename
 
diff --git a/tests/test-help.t b/tests/test-help.t
index c9b2a4d69e66884603c9345466ab842149e14dc7_dGVzdHMvdGVzdC1oZWxwLnQ=..9c9e0b4b2ca7df1c8764466b9ca3670935bacd47_dGVzdHMvdGVzdC1oZWxwLnQ= 100644
--- a/tests/test-help.t
+++ b/tests/test-help.t
@@ -593,7 +593,7 @@
    -n --dry-run             do not perform actions, just print output
   
   (use 'hg add -h' to show more help)
-  [255]
+  [10]
 
 Test ambiguous command help
 
@@ -763,9 +763,9 @@
   $ hg skjdfks
   hg: unknown command 'skjdfks'
   (use 'hg help' for a list of commands)
-  [255]
+  [10]
 
 Typoed command gives suggestion
   $ hg puls
   hg: unknown command 'puls'
   (did you mean one of pull, push?)
@@ -767,9 +767,9 @@
 
 Typoed command gives suggestion
   $ hg puls
   hg: unknown command 'puls'
   (did you mean one of pull, push?)
-  [255]
+  [10]
 
 Not enabled extension gets suggested
 
@@ -780,7 +780,7 @@
       rebase        command to move sets of revisions to a different ancestor
   
   (use 'hg help extensions' for information on enabling extensions)
-  [255]
+  [10]
 
 Disabled extension gets suggested
   $ hg --config extensions.rebase=! rebase
@@ -790,7 +790,7 @@
       rebase        command to move sets of revisions to a different ancestor
   
   (use 'hg help extensions' for information on enabling extensions)
-  [255]
+  [10]
 
 Checking that help adapts based on the config:
 
@@ -804,8 +804,8 @@
   $ hg .log
   hg: unknown command '.log'
   (did you mean log?)
-  [255]
+  [10]
 
   $ hg log.
   hg: unknown command 'log.'
   (did you mean log?)
@@ -808,8 +808,8 @@
 
   $ hg log.
   hg: unknown command 'log.'
   (did you mean log?)
-  [255]
+  [10]
   $ hg pu.lh
   hg: unknown command 'pu.lh'
   (did you mean one of pull, push?)
@@ -813,7 +813,7 @@
   $ hg pu.lh
   hg: unknown command 'pu.lh'
   (did you mean one of pull, push?)
-  [255]
+  [10]
 
   $ cat > helpext.py <<EOF
   > import os
diff --git a/tests/test-narrow-trackedcmd.t b/tests/test-narrow-trackedcmd.t
index c9b2a4d69e66884603c9345466ab842149e14dc7_dGVzdHMvdGVzdC1uYXJyb3ctdHJhY2tlZGNtZC50..9c9e0b4b2ca7df1c8764466b9ca3670935bacd47_dGVzdHMvdGVzdC1uYXJyb3ctdHJhY2tlZGNtZC50 100644
--- a/tests/test-narrow-trackedcmd.t
+++ b/tests/test-narrow-trackedcmd.t
@@ -118,7 +118,7 @@
                                    web.cacerts config)
   
   (use 'hg tracked -h' to show more help)
-  [255]
+  [10]
   $ hg tracked --import-rules doesnotexist
   abort: cannot read narrowspecs from '$TESTTMP/narrow/doesnotexist': $ENOENT$
   [50]
diff --git a/tests/test-qrecord.t b/tests/test-qrecord.t
index c9b2a4d69e66884603c9345466ab842149e14dc7_dGVzdHMvdGVzdC1xcmVjb3JkLnQ=..9c9e0b4b2ca7df1c8764466b9ca3670935bacd47_dGVzdHMvdGVzdC1xcmVjb3JkLnQ= 100644
--- a/tests/test-qrecord.t
+++ b/tests/test-qrecord.t
@@ -105,7 +105,7 @@
   interactively record a new patch
   
   (use 'hg qrecord -h' to show more help)
-  [255]
+  [10]
 
 qrecord patch (mq not present)
 
diff --git a/tests/test-share-safe.t b/tests/test-share-safe.t
index c9b2a4d69e66884603c9345466ab842149e14dc7_dGVzdHMvdGVzdC1zaGFyZS1zYWZlLnQ=..9c9e0b4b2ca7df1c8764466b9ca3670935bacd47_dGVzdHMvdGVzdC1zaGFyZS1zYWZlLnQ= 100644
--- a/tests/test-share-safe.t
+++ b/tests/test-share-safe.t
@@ -92,7 +92,7 @@
       extdiff       command to allow external programs to compare revisions
   
   (use 'hg help extensions' for information on enabling extensions)
-  [255]
+  [10]
 
   $ echo "[extensions]" >> ../source/.hg/hgrc
   $ echo "extdiff=" >> ../source/.hg/hgrc
diff --git a/tests/test-status-color.t b/tests/test-status-color.t
index c9b2a4d69e66884603c9345466ab842149e14dc7_dGVzdHMvdGVzdC1zdGF0dXMtY29sb3IudA==..9c9e0b4b2ca7df1c8764466b9ca3670935bacd47_dGVzdHMvdGVzdC1zdGF0dXMtY29sb3IudA== 100644
--- a/tests/test-status-color.t
+++ b/tests/test-status-color.t
@@ -394,7 +394,7 @@
   $ hg unknowncommand > /dev/null
   hg: unknown command 'unknowncommand'
   (use 'hg help' for a list of commands)
-  [255]
+  [10]
 
 color coding of error message without curses
 
@@ -402,6 +402,6 @@
   $ PYTHONPATH=`pwd`:$PYTHONPATH hg unknowncommand > /dev/null
   hg: unknown command 'unknowncommand'
   (use 'hg help' for a list of commands)
-  [255]
+  [10]
 
   $ cd ..
diff --git a/tests/test-strict.t b/tests/test-strict.t
index c9b2a4d69e66884603c9345466ab842149e14dc7_dGVzdHMvdGVzdC1zdHJpY3QudA==..9c9e0b4b2ca7df1c8764466b9ca3670935bacd47_dGVzdHMvdGVzdC1zdHJpY3QudA== 100644
--- a/tests/test-strict.t
+++ b/tests/test-strict.t
@@ -16,7 +16,7 @@
   $ hg an a
   hg: unknown command 'an'
   (use 'hg help' for a list of commands)
-  [255]
+  [10]
   $ hg annotate a
   0: a
 
diff --git a/tests/test-strip.t b/tests/test-strip.t
index c9b2a4d69e66884603c9345466ab842149e14dc7_dGVzdHMvdGVzdC1zdHJpcC50..9c9e0b4b2ca7df1c8764466b9ca3670935bacd47_dGVzdHMvdGVzdC1zdHJpcC50 100644
--- a/tests/test-strip.t
+++ b/tests/test-strip.t
@@ -782,7 +782,7 @@
       --mq                    operate on patch repository
   
   (use 'hg debugstrip -h' to show more help)
-  [255]
+  [10]
 
   $ cd ..