comparison mercurial/commands.py @ 15203:c7ce651a6bc9

help: generate command help into a single RST string for formatting
author Matt Mackall <mpm@selenic.com>
date Fri, 07 Oct 2011 17:08:24 -0500
parents 0150741caace
children 10f85a735601
comparison
equal deleted inserted replaced
15202:0150741caace 15203:c7ce651a6bc9
2769 data.append((so, lo, desc)) 2769 data.append((so, lo, desc))
2770 2770
2771 rst = minirst.maketable(data, 1) 2771 rst = minirst.maketable(data, 1)
2772 2772
2773 if multioccur: 2773 if multioccur:
2774 rst += _("\n[+] marked option can be specified multiple times") 2774 rst += _("\n[+] marked option can be specified multiple times\n")
2775 2775
2776 return rst 2776 return rst
2777 2777
2778 # list all option lists 2778 # list all option lists
2779 def opttext(optlist, width): 2779 def opttext(optlist, width):
2780 rst = '' 2780 rst = ''
2781 if not optlist: 2781 if not optlist:
2782 return '' 2782 return ''
2783 2783
2784 for title, options in optlist: 2784 for title, options in optlist:
2785 rst += '\n%s\n\n' % title 2785 rst += '\n%s\n' % title
2786 rst += optrst(options) 2786 if options:
2787 rst += '\n' 2787 rst += "\n"
2788 rst += optrst(options)
2789 rst += '\n'
2788 2790
2789 return '\n' + minirst.format(rst, width) 2791 return '\n' + minirst.format(rst, width)
2790 2792
2791 def addglobalopts(optlist, aliases): 2793 def addglobalopts(optlist, aliases):
2792 if ui.quiet: 2794 if ui.quiet:
2809 else: 2811 else:
2810 msg = _('use "hg -v help %s" to show more info') % name 2812 msg = _('use "hg -v help %s" to show more info') % name
2811 optlist.append((msg, ())) 2813 optlist.append((msg, ()))
2812 2814
2813 def helpcmd(name): 2815 def helpcmd(name):
2814 optlist = []
2815 try: 2816 try:
2816 aliases, entry = cmdutil.findcmd(name, table, strict=unknowncmd) 2817 aliases, entry = cmdutil.findcmd(name, table, strict=unknowncmd)
2817 except error.AmbiguousCommand, inst: 2818 except error.AmbiguousCommand, inst:
2818 # py3k fix: except vars can't be used outside the scope of the 2819 # py3k fix: except vars can't be used outside the scope of the
2819 # except block, nor can be used inside a lambda. python issue4617 2820 # except block, nor can be used inside a lambda. python issue4617
2826 if getattr(entry[0], 'badalias', False): 2827 if getattr(entry[0], 'badalias', False):
2827 if not unknowncmd: 2828 if not unknowncmd:
2828 entry[0](ui) 2829 entry[0](ui)
2829 return 2830 return
2830 2831
2832 rst = ""
2833
2831 # synopsis 2834 # synopsis
2832 if len(entry) > 2: 2835 if len(entry) > 2:
2833 if entry[2].startswith('hg'): 2836 if entry[2].startswith('hg'):
2834 ui.write("%s\n" % entry[2]) 2837 rst += "%s\n" % entry[2]
2835 else: 2838 else:
2836 ui.write('hg %s %s\n' % (aliases[0], entry[2])) 2839 rst += 'hg %s %s\n' % (aliases[0], entry[2])
2837 else: 2840 else:
2838 ui.write('hg %s\n' % aliases[0]) 2841 rst += 'hg %s\n' % aliases[0]
2839 2842
2840 # aliases 2843 # aliases
2841 if full and not ui.quiet and len(aliases) > 1: 2844 if full and not ui.quiet and len(aliases) > 1:
2842 ui.write(_("\naliases: %s\n") % ', '.join(aliases[1:])) 2845 rst += _("\naliases: %s\n") % ', '.join(aliases[1:])
2843 2846
2844 # description 2847 # description
2845 doc = gettext(entry[0].__doc__) 2848 doc = gettext(entry[0].__doc__)
2846 if not doc: 2849 if not doc:
2847 doc = _("(no help text available)") 2850 doc = _("(no help text available)")
2850 doc = _('shell alias for::\n\n %s') % entry[0].definition[1:] 2853 doc = _('shell alias for::\n\n %s') % entry[0].definition[1:]
2851 else: 2854 else:
2852 doc = _('alias for: hg %s\n\n%s') % (entry[0].definition, doc) 2855 doc = _('alias for: hg %s\n\n%s') % (entry[0].definition, doc)
2853 if ui.quiet or not full: 2856 if ui.quiet or not full:
2854 doc = doc.splitlines()[0] 2857 doc = doc.splitlines()[0]
2855 keep = ui.verbose and ['verbose'] or [] 2858 rst += "\n" + doc + "\n"
2856 formatted, pruned = minirst.format(doc, textwidth, keep=keep)
2857 ui.write("\n%s" % formatted)
2858
2859 if not ui.quiet:
2860 # options
2861 if entry[1]:
2862 optlist.append((_("options:\n"), entry[1]))
2863 2859
2864 # check if this command shadows a non-trivial (multi-line) 2860 # check if this command shadows a non-trivial (multi-line)
2865 # extension help text 2861 # extension help text
2866 try: 2862 try:
2867 mod = extensions.find(name) 2863 mod = extensions.find(name)
2868 doc = gettext(mod.__doc__) or '' 2864 doc = gettext(mod.__doc__) or ''
2869 if '\n' in doc.strip(): 2865 if '\n' in doc.strip():
2870 msg = _('use "hg help -e %s" to show help for ' 2866 msg = _('use "hg help -e %s" to show help for '
2871 'the %s extension') % (name, name) 2867 'the %s extension') % (name, name)
2872 ui.write('\n%s\n' % msg) 2868 rst += '\n%s\n' % msg
2873 except KeyError: 2869 except KeyError:
2874 pass 2870 pass
2875 2871
2876 addglobalopts(optlist, False) 2872 # options
2877 ui.write(opttext(optlist, textwidth)) 2873 if not ui.quiet and entry[1]:
2874 rst += '\noptions:\n\n'
2875 rst += optrst(entry[1])
2876
2877 if ui.verbose:
2878 rst += '\nglobal options:\n\n'
2879 rst += optrst(globalopts)
2880
2881 keep = ui.verbose and ['verbose'] or []
2882 formatted, pruned = minirst.format(rst, textwidth, keep=keep)
2883 ui.write(formatted)
2884
2885 if not ui.verbose:
2886 if not full:
2887 ui.write(_('\nuse "hg help %s" to show the full help text\n')
2888 % name)
2889 elif not ui.quiet:
2890 ui.write(_('\nuse "hg -v help %s" to show more info\n') % name)
2891
2878 2892
2879 def helplist(select=None): 2893 def helplist(select=None):
2880 # list of commands 2894 # list of commands
2881 if name == "shortlist": 2895 if name == "shortlist":
2882 header = _('basic commands:\n\n') 2896 header = _('basic commands:\n\n')