help: move option text display into a helper function
authorMatt Mackall <mpm@selenic.com>
Thu, 04 Aug 2011 16:12:58 -0500
changeset 15023 157a294444b2
parent 15022 bf1fa4ba582b
child 15027 1e45b92f4fb2
help: move option text display into a helper function
mercurial/commands.py
--- a/mercurial/commands.py	Thu Aug 04 15:36:15 2011 -0500
+++ b/mercurial/commands.py	Thu Aug 04 16:12:58 2011 -0500
@@ -2589,9 +2589,66 @@
 
     Returns 0 if successful.
     """
+
     optlist = []
     textwidth = min(ui.termwidth(), 80) - 2
 
+    # list all option lists
+    def opttext(optlist, width):
+        out = []
+        multioccur = False
+        for title, options in optlist:
+            out.append(("\n%s" % title, None))
+            for option in options:
+                if len(option) == 5:
+                    shortopt, longopt, default, desc, optlabel = option
+                else:
+                    shortopt, longopt, default, desc = option
+                    optlabel = _("VALUE") # default label
+
+                if _("DEPRECATED") in desc and not ui.verbose:
+                    continue
+                if isinstance(default, list):
+                    numqualifier = " %s [+]" % optlabel
+                    multioccur = True
+                elif (default is not None) and not isinstance(default, bool):
+                    numqualifier = " %s" % optlabel
+                else:
+                    numqualifier = ""
+                out.append(("%2s%s" %
+                            (shortopt and "-%s" % shortopt,
+                             longopt and " --%s%s" %
+                             (longopt, numqualifier)),
+                            "%s%s" % (desc,
+                                      default
+                                      and _(" (default: %s)") % default
+                                      or "")))
+        if multioccur:
+            msg = _("\n[+] marked option can be specified multiple times")
+            if ui.verbose and name != 'shortlist':
+                out.append((msg, None))
+            else:
+                out.insert(-1, (msg, None))
+
+        text = ""
+        if out:
+            colwidth = encoding.colwidth
+            # normalize: (opt or message, desc or None, width of opt)
+            entries = [desc and (opt, desc, colwidth(opt)) or (opt, None, 0)
+                       for opt, desc in out]
+            hanging = max([e[2] for e in entries])
+            for opt, desc, width in entries:
+                if desc:
+                    initindent = ' %s%s  ' % (opt, ' ' * (hanging - width))
+                    hangindent = ' ' * (hanging + 3)
+                    text += '%s\n' % (util.wrap(desc, width,
+                                                initindent=initindent,
+                                                hangindent=hangindent))
+                else:
+                    text +=  "%s\n" % opt
+
+        return text
+
     def addglobalopts(aliases):
         if ui.verbose:
             optlist.append((_("global options:"), globalopts))
@@ -2829,57 +2886,7 @@
         for t, desc in topics:
             ui.write(" %-*s  %s\n" % (topics_len, t, desc))
 
-    # list all option lists
-    opt_output = []
-    multioccur = False
-    for title, options in optlist:
-        opt_output.append(("\n%s" % title, None))
-        for option in options:
-            if len(option) == 5:
-                shortopt, longopt, default, desc, optlabel = option
-            else:
-                shortopt, longopt, default, desc = option
-                optlabel = _("VALUE") # default label
-
-            if _("DEPRECATED") in desc and not ui.verbose:
-                continue
-            if isinstance(default, list):
-                numqualifier = " %s [+]" % optlabel
-                multioccur = True
-            elif (default is not None) and not isinstance(default, bool):
-                numqualifier = " %s" % optlabel
-            else:
-                numqualifier = ""
-            opt_output.append(("%2s%s" %
-                               (shortopt and "-%s" % shortopt,
-                                longopt and " --%s%s" %
-                                (longopt, numqualifier)),
-                               "%s%s" % (desc,
-                                         default
-                                         and _(" (default: %s)") % default
-                                         or "")))
-    if multioccur:
-        msg = _("\n[+] marked option can be specified multiple times")
-        if ui.verbose and name != 'shortlist':
-            opt_output.append((msg, None))
-        else:
-            opt_output.insert(-1, (msg, None))
-
-    if opt_output:
-        colwidth = encoding.colwidth
-        # normalize: (opt or message, desc or None, width of opt)
-        entries = [desc and (opt, desc, colwidth(opt)) or (opt, None, 0)
-                   for opt, desc in opt_output]
-        hanging = max([e[2] for e in entries])
-        for opt, desc, width in entries:
-            if desc:
-                initindent = ' %s%s  ' % (opt, ' ' * (hanging - width))
-                hangindent = ' ' * (hanging + 3)
-                ui.write('%s\n' % (util.wrap(desc, textwidth,
-                                             initindent=initindent,
-                                             hangindent=hangindent)))
-            else:
-                ui.write("%s\n" % opt)
+    ui.write(opttext(optlist, textwidth))
 
 @command('identify|id',
     [('r', 'rev', '',