Show More
@@ -1,160 +1,171 | |||
|
1 | 1 | import os, sys, textwrap |
|
2 | 2 | # import from the live mercurial repo |
|
3 | 3 | sys.path.insert(0, "..") |
|
4 | 4 | # fall back to pure modules if required C extensions are not available |
|
5 | 5 | sys.path.append(os.path.join('..', 'mercurial', 'pure')) |
|
6 | 6 | from mercurial import demandimport; demandimport.enable() |
|
7 | 7 | from mercurial import minirst |
|
8 | 8 | from mercurial.commands import table, globalopts |
|
9 | 9 | from mercurial.i18n import gettext, _ |
|
10 | from mercurial.help import helptable | |
|
10 | from mercurial.help import helptable, loaddoc | |
|
11 | 11 | from mercurial import extensions |
|
12 | 12 | from mercurial import util |
|
13 | 13 | |
|
14 | 14 | def get_desc(docstr): |
|
15 | 15 | if not docstr: |
|
16 | 16 | return "", "" |
|
17 | 17 | # sanitize |
|
18 | 18 | docstr = docstr.strip("\n") |
|
19 | 19 | docstr = docstr.rstrip() |
|
20 | 20 | shortdesc = docstr.splitlines()[0].strip() |
|
21 | 21 | |
|
22 | 22 | i = docstr.find("\n") |
|
23 | 23 | if i != -1: |
|
24 | 24 | desc = docstr[i + 2:] |
|
25 | 25 | else: |
|
26 | 26 | desc = shortdesc |
|
27 | 27 | |
|
28 | 28 | desc = textwrap.dedent(desc) |
|
29 | 29 | |
|
30 | 30 | return (shortdesc, desc) |
|
31 | 31 | |
|
32 | 32 | def get_opts(opts): |
|
33 | 33 | for opt in opts: |
|
34 | 34 | if len(opt) == 5: |
|
35 | 35 | shortopt, longopt, default, desc, optlabel = opt |
|
36 | 36 | else: |
|
37 | 37 | shortopt, longopt, default, desc = opt |
|
38 | 38 | allopts = [] |
|
39 | 39 | if shortopt: |
|
40 | 40 | allopts.append("-%s" % shortopt) |
|
41 | 41 | if longopt: |
|
42 | 42 | allopts.append("--%s" % longopt) |
|
43 | 43 | desc += default and _(" (default: %s)") % default or "" |
|
44 | 44 | yield (", ".join(allopts), desc) |
|
45 | 45 | |
|
46 | 46 | def get_cmd(cmd, cmdtable): |
|
47 | 47 | d = {} |
|
48 | 48 | attr = cmdtable[cmd] |
|
49 | 49 | cmds = cmd.lstrip("^").split("|") |
|
50 | 50 | |
|
51 | 51 | d['cmd'] = cmds[0] |
|
52 | 52 | d['aliases'] = cmd.split("|")[1:] |
|
53 | 53 | d['desc'] = get_desc(gettext(attr[0].__doc__)) |
|
54 | 54 | d['opts'] = list(get_opts(attr[1])) |
|
55 | 55 | |
|
56 | 56 | s = 'hg ' + cmds[0] |
|
57 | 57 | if len(attr) > 2: |
|
58 | 58 | if not attr[2].startswith('hg'): |
|
59 | 59 | s += ' ' + attr[2] |
|
60 | 60 | else: |
|
61 | 61 | s = attr[2] |
|
62 | 62 | d['synopsis'] = s.strip() |
|
63 | 63 | |
|
64 | 64 | return d |
|
65 | 65 | |
|
66 | 66 | def showdoc(ui): |
|
67 | 67 | # print options |
|
68 | 68 | ui.write(minirst.section(_("Options"))) |
|
69 | 69 | for optstr, desc in get_opts(globalopts): |
|
70 | 70 | ui.write("%s\n %s\n\n" % (optstr, desc)) |
|
71 | 71 | |
|
72 | 72 | # print cmds |
|
73 | 73 | ui.write(minirst.section(_("Commands"))) |
|
74 | 74 | commandprinter(ui, table, minirst.subsection) |
|
75 | 75 | |
|
76 | 76 | # print help topics |
|
77 | 77 | # The config help topic is included in the hgrc.5 man page. |
|
78 | 78 | helpprinter(ui, helptable, minirst.section, exclude=['config']) |
|
79 | 79 | |
|
80 | 80 | ui.write(minirst.section(_("Extensions"))) |
|
81 | 81 | ui.write(_("This section contains help for extensions that are " |
|
82 | 82 | "distributed together with Mercurial. Help for other " |
|
83 | 83 | "extensions is available in the help system.")) |
|
84 | 84 | ui.write("\n\n" |
|
85 | 85 | ".. contents::\n" |
|
86 | 86 | " :class: htmlonly\n" |
|
87 | 87 | " :local:\n" |
|
88 | 88 | " :depth: 1\n\n") |
|
89 | 89 | |
|
90 | 90 | for extensionname in sorted(allextensionnames()): |
|
91 | 91 | mod = extensions.load(None, extensionname, None) |
|
92 | 92 | ui.write(minirst.subsection(extensionname)) |
|
93 | 93 | ui.write("%s\n\n" % gettext(mod.__doc__)) |
|
94 | 94 | cmdtable = getattr(mod, 'cmdtable', None) |
|
95 | 95 | if cmdtable: |
|
96 | 96 | ui.write(minirst.subsubsection(_('Commands'))) |
|
97 | 97 | commandprinter(ui, cmdtable, minirst.subsubsubsection) |
|
98 | 98 | |
|
99 | def showtopic(ui, topic): | |
|
100 | extrahelptable = [ | |
|
101 | (["common"], '', loaddoc('common')), | |
|
102 | (["hg.1"], '', loaddoc('hg.1')), | |
|
103 | (["hgignore.5"], '', loaddoc('hgignore.5')), | |
|
104 | (["hgrc.5"], '', loaddoc('hgrc.5')), | |
|
105 | (["hgignore.5.gendoc"], '', loaddoc('hgignore')), | |
|
106 | (["hgrc.5.gendoc"], '', loaddoc('config')), | |
|
107 | ] | |
|
108 | helpprinter(ui, helptable + extrahelptable, None, include=[topic]) | |
|
109 | ||
|
99 | 110 | def helpprinter(ui, helptable, sectionfunc, include=[], exclude=[]): |
|
100 | 111 | for names, sec, doc in helptable: |
|
101 | 112 | if exclude and names[0] in exclude: |
|
102 | 113 | continue |
|
103 | 114 | if include and names[0] not in include: |
|
104 | 115 | continue |
|
105 | 116 | for name in names: |
|
106 | 117 | ui.write(".. _%s:\n" % name) |
|
107 | 118 | ui.write("\n") |
|
108 | 119 | if sectionfunc: |
|
109 | 120 | ui.write(sectionfunc(sec)) |
|
110 | 121 | if util.safehasattr(doc, '__call__'): |
|
111 | 122 | doc = doc() |
|
112 | 123 | ui.write(doc) |
|
113 | 124 | ui.write("\n") |
|
114 | 125 | |
|
115 | 126 | def commandprinter(ui, cmdtable, sectionfunc): |
|
116 | 127 | h = {} |
|
117 | 128 | for c, attr in cmdtable.items(): |
|
118 | 129 | f = c.split("|")[0] |
|
119 | 130 | f = f.lstrip("^") |
|
120 | 131 | h[f] = c |
|
121 | 132 | cmds = h.keys() |
|
122 | 133 | cmds.sort() |
|
123 | 134 | |
|
124 | 135 | for f in cmds: |
|
125 | 136 | if f.startswith("debug"): |
|
126 | 137 | continue |
|
127 | 138 | d = get_cmd(h[f], cmdtable) |
|
128 | 139 | ui.write(sectionfunc(d['cmd'])) |
|
129 | 140 | # synopsis |
|
130 | 141 | ui.write("::\n\n") |
|
131 | 142 | synopsislines = d['synopsis'].splitlines() |
|
132 | 143 | for line in synopsislines: |
|
133 | 144 | # some commands (such as rebase) have a multi-line |
|
134 | 145 | # synopsis |
|
135 | 146 | ui.write(" %s\n" % line) |
|
136 | 147 | ui.write('\n') |
|
137 | 148 | # description |
|
138 | 149 | ui.write("%s\n\n" % d['desc'][1]) |
|
139 | 150 | # options |
|
140 | 151 | opt_output = list(d['opts']) |
|
141 | 152 | if opt_output: |
|
142 | 153 | opts_len = max([len(line[0]) for line in opt_output]) |
|
143 | 154 | ui.write(_("Options:\n\n")) |
|
144 | 155 | for optstr, desc in opt_output: |
|
145 | 156 | if desc: |
|
146 | 157 | s = "%-*s %s" % (opts_len, optstr, desc) |
|
147 | 158 | else: |
|
148 | 159 | s = optstr |
|
149 | 160 | ui.write("%s\n" % s) |
|
150 | 161 | ui.write("\n") |
|
151 | 162 | # aliases |
|
152 | 163 | if d['aliases']: |
|
153 | 164 | ui.write(_(" aliases: %s\n\n") % " ".join(d['aliases'])) |
|
154 | 165 | |
|
155 | 166 | |
|
156 | 167 | def allextensionnames(): |
|
157 | 168 | return extensions.enabled().keys() + extensions.disabled().keys() |
|
158 | 169 | |
|
159 | 170 | if __name__ == "__main__": |
|
160 | 171 | showdoc(sys.stdout) |
General Comments 0
You need to be logged in to leave comments.
Login now