[PATCH 04 of 10] help: adding arguments to options from getopts

Erik Zielke ez at aragost.com
Wed Nov 17 11:11:03 CST 2010


# HG changeset patch
# User Erik Zielke <ez at aragost.com>
# Date 1290012209 -3600
# Node ID b435679ab44f3575b1f70f7900529ee1005f043e
# Parent  494f49235490c88504f183630001824fcb708e60
help: adding arguments to options from getopts

This adds the argument to a option, e.g. the PATTERN [+] part in
--include PATTERN [+]. This is however not done, when called from
gendoc, because then it will not be a valid docstring accordingto
docutils.

diff -r 494f49235490 -r b435679ab44f doc/gendoc.py
--- a/doc/gendoc.py	Tue Nov 09 09:38:25 2010 +0100
+++ b/doc/gendoc.py	Wed Nov 17 17:43:29 2010 +0100
@@ -26,12 +26,14 @@
 def show_doc(ui):
     # print options
     section(ui, _("Options"))
-    for optstr, desc in helpmod.getopts(globalopts):
+    helpconfig = helpmod.createhelpconfig('', False, False)
+
+    for optstr, desc in helpmod.getopts(globalopts, helpconfig):
         ui.write("%s\n    %s\n\n" % (optstr, desc))
 
     # print cmds
     section(ui, _("Commands"))
-    commandprinter(ui, table, subsection)
+    commandprinter(ui, table, subsection, helpconfig)
 
     # print topics
     for names, sec, doc in helpmod.helptable:
@@ -61,9 +63,9 @@
         cmdtable = getattr(mod, 'cmdtable', None)
         if cmdtable:
             subsubsection(ui, _('Commands'))
-            commandprinter(ui, cmdtable, subsubsubsection)
+            commandprinter(ui, cmdtable, subsubsubsection, helpconfig)
 
-def commandprinter(ui, cmdtable, sectionfunc):
+def commandprinter(ui, cmdtable, sectionfunc, helpconfig):
     h = {}
     for c, attr in cmdtable.items():
         f = c.split("|")[0]
@@ -75,7 +77,8 @@
     for f in cmds:
         if f.startswith("debug"):
             continue
-        d = helpmod.getcmd(h[f], cmdtable)
+        helpconfig['name'] = f
+        d = helpmod.getcmd(h[f], cmdtable, helpconfig)
         sectionfunc(ui, d['cmd'])
         helpmod.commandprinter(d, ui)
 
diff -r 494f49235490 -r b435679ab44f mercurial/commands.py
--- a/mercurial/commands.py	Tue Nov 09 09:38:25 2010 +0100
+++ b/mercurial/commands.py	Wed Nov 17 17:43:29 2010 +0100
@@ -1871,7 +1871,7 @@
     """
     option_lists = []
     textwidth = ui.termwidth() - 2
-    helpconfig = help.createhelpconfig(name, ui.verbose)
+    helpconfig = help.createhelpconfig(name, ui.verbose, True)
 
     def addglobalopts(aliases):
         if ui.verbose:
diff -r 494f49235490 -r b435679ab44f mercurial/help.py
--- a/mercurial/help.py	Tue Nov 09 09:38:25 2010 +0100
+++ b/mercurial/help.py	Wed Nov 17 17:43:29 2010 +0100
@@ -10,6 +10,16 @@
 import extensions, util, error
 
 
+def cleancmdsmap(cmdtable):
+    h = {}
+    for c, attr in cmdtable.items():
+        f = c.split("|")[0]
+        f = f.lstrip("^")
+        h[f] = c
+    cmds = h.keys()
+    cmds.sort()
+    return cmds, h
+
 def moduledoc(file):
     '''return the top-level python documentation for the given file
 
@@ -104,8 +114,9 @@
 
     return (shortdesc, desc)
 
-def getopts(opts):
+def getopts(opts, helpconfig):
     for opt in opts:
+        optlabel = None
         if len(opt) == 5:
             shortopt, longopt, default, desc, optlabel = opt
         else:
@@ -116,9 +127,14 @@
         if longopt:
             allopts.append("--%s" % longopt)
         desc += default and _(" (default: %s)") % default or ""
-        yield(", ".join(allopts), desc)
+        if helpconfig['cmdline']:
+            if (isinstance(default, list)):
+                if optlabel is not None:
+                    optlabel.strip()
+                optlabel = "%s [+]" % optlabel
+        yield(", ".join(allopts) + ' ' + (optlabel or ''), desc)
 
-def getcmd(cmd, cmdtable):
+def getcmd(cmd, cmdtable, helpconfig):
     d = {}
     attr = cmdtable[cmd]
     cmds = cmd.lstrip("^").split("|")
@@ -126,7 +142,7 @@
     d['cmd'] = cmds[0]
     d['aliases'] = cmd.split("|")[1:]
     d['desc'] = getdesc(attr[0].__doc__)
-    d['opts'] = list(getopts(attr[1]))
+    d['opts'] = list(getopts(attr[1], helpconfig))
 
     s = 'hg ' + cmds[0]
     if len(attr) > 2:
@@ -187,8 +203,9 @@
 
     return ''.join(versionlines)
 
-def createhelpconfig(name, verbose):
+def createhelpconfig(name, verbose, cmdline):
     helpconfig = {'name': name,
+                  'cmdline': cmdline,
                   'keep': []
                   }
     if verbose:


More information about the Mercurial-devel mailing list