##// END OF EJS Templates
gendoc: refactor get_cmd...
Erik Zielke -
r12756:13f0acfa default
parent child Browse files
Show More
@@ -1,119 +1,123
1 import os, sys
1 import os, sys
2 # import from the live mercurial repo
2 # import from the live mercurial repo
3 sys.path.insert(0, "..")
3 sys.path.insert(0, "..")
4 # fall back to pure modules if required C extensions are not available
4 # fall back to pure modules if required C extensions are not available
5 sys.path.append(os.path.join('..', 'mercurial', 'pure'))
5 sys.path.append(os.path.join('..', 'mercurial', 'pure'))
6 from mercurial import demandimport; demandimport.enable()
6 from mercurial import demandimport; demandimport.enable()
7 from mercurial import encoding
7 from mercurial import encoding
8 from mercurial.commands import table, globalopts
8 from mercurial.commands import table, globalopts
9 from mercurial.i18n import _
9 from mercurial.i18n import _
10 from mercurial.help import helptable
10 from mercurial.help import helptable
11
11
12 def get_desc(docstr):
12 def get_desc(docstr):
13 if not docstr:
13 if not docstr:
14 return "", ""
14 return "", ""
15 # sanitize
15 # sanitize
16 docstr = docstr.strip("\n")
16 docstr = docstr.strip("\n")
17 docstr = docstr.rstrip()
17 docstr = docstr.rstrip()
18 shortdesc = docstr.splitlines()[0].strip()
18 shortdesc = docstr.splitlines()[0].strip()
19
19
20 i = docstr.find("\n")
20 i = docstr.find("\n")
21 if i != -1:
21 if i != -1:
22 desc = docstr[i + 2:]
22 desc = docstr[i + 2:]
23 else:
23 else:
24 desc = " %s" % shortdesc
24 desc = " %s" % shortdesc
25 return (shortdesc, desc)
25 return (shortdesc, desc)
26
26
27 def get_opts(opts):
27 def get_opts(opts):
28 for opt in opts:
28 for opt in opts:
29 if len(opt) == 5:
29 if len(opt) == 5:
30 shortopt, longopt, default, desc, optlabel = opt
30 shortopt, longopt, default, desc, optlabel = opt
31 else:
31 else:
32 shortopt, longopt, default, desc = opt
32 shortopt, longopt, default, desc = opt
33 allopts = []
33 allopts = []
34 if shortopt:
34 if shortopt:
35 allopts.append("-%s" % shortopt)
35 allopts.append("-%s" % shortopt)
36 if longopt:
36 if longopt:
37 allopts.append("--%s" % longopt)
37 allopts.append("--%s" % longopt)
38 desc += default and _(" (default: %s)") % default or ""
38 desc += default and _(" (default: %s)") % default or ""
39 yield(", ".join(allopts), desc)
39 yield(", ".join(allopts), desc)
40
40
41 def get_cmd(cmd):
41 def get_cmd(cmd, cmdtable):
42 d = {}
42 d = {}
43 attr = table[cmd]
43 attr = cmdtable[cmd]
44 cmds = cmd.lstrip("^").split("|")
44 cmds = cmd.lstrip("^").split("|")
45
45
46 d['cmd'] = cmds[0]
46 d['cmd'] = cmds[0]
47 d['aliases'] = cmd.split("|")[1:]
47 d['aliases'] = cmd.split("|")[1:]
48 d['desc'] = get_desc(attr[0].__doc__)
48 d['desc'] = get_desc(attr[0].__doc__)
49 d['opts'] = list(get_opts(attr[1]))
49 d['opts'] = list(get_opts(attr[1]))
50
50
51 s = 'hg ' + cmds[0]
51 s = 'hg ' + cmds[0]
52 if len(attr) > 2:
52 if len(attr) > 2:
53 if not attr[2].startswith('hg'):
53 if not attr[2].startswith('hg'):
54 s += ' ' + attr[2]
54 s += ' ' + attr[2]
55 else:
55 else:
56 s = attr[2]
56 s = attr[2]
57 d['synopsis'] = s.strip()
57 d['synopsis'] = s.strip()
58
58
59 return d
59 return d
60
60
61 def show_doc(ui):
61 def show_doc(ui):
62 def section(s):
62 def section(s):
63 ui.write("%s\n%s\n\n" % (s, "-" * encoding.colwidth(s)))
63 ui.write("%s\n%s\n\n" % (s, "-" * encoding.colwidth(s)))
64 def subsection(s):
64 def subsection(s):
65 ui.write("%s\n%s\n\n" % (s, '"' * encoding.colwidth(s)))
65 ui.write("%s\n%s\n\n" % (s, '"' * encoding.colwidth(s)))
66
66
67 # print options
67 # print options
68 section(_("Options"))
68 section(_("Options"))
69 for optstr, desc in get_opts(globalopts):
69 for optstr, desc in get_opts(globalopts):
70 ui.write("%s\n %s\n\n" % (optstr, desc))
70 ui.write("%s\n %s\n\n" % (optstr, desc))
71
71
72 # print cmds
72 # print cmds
73 section(_("Commands"))
73 section(_("Commands"))
74 commandprinter(ui, table)
75
76 # print topics
77 for names, sec, doc in helptable:
78 for name in names:
79 ui.write(".. _%s:\n" % name)
80 ui.write("\n")
81 section(sec)
82 if hasattr(doc, '__call__'):
83 doc = doc()
84 ui.write(doc)
85 ui.write("\n")
86
87 def commandprinter(ui, cmdtable):
74 h = {}
88 h = {}
75 for c, attr in table.items():
89 for c, attr in cmdtable.items():
76 f = c.split("|")[0]
90 f = c.split("|")[0]
77 f = f.lstrip("^")
91 f = f.lstrip("^")
78 h[f] = c
92 h[f] = c
79 cmds = h.keys()
93 cmds = h.keys()
80 cmds.sort()
94 cmds.sort()
81
95
82 for f in cmds:
96 for f in cmds:
83 if f.startswith("debug"):
97 if f.startswith("debug"):
84 continue
98 continue
85 d = get_cmd(h[f])
99 d = get_cmd(h[f], cmdtable)
86 # synopsis
100 # synopsis
87 ui.write(".. _%s:\n\n" % d['cmd'])
101 ui.write(".. _%s:\n\n" % d['cmd'])
88 ui.write("``%s``\n" % d['synopsis'].replace("hg ","", 1))
102 ui.write("``%s``\n" % d['synopsis'].replace("hg ","", 1))
89 # description
103 # description
90 ui.write("%s\n\n" % d['desc'][1])
104 ui.write("%s\n\n" % d['desc'][1])
91 # options
105 # options
92 opt_output = list(d['opts'])
106 opt_output = list(d['opts'])
93 if opt_output:
107 if opt_output:
94 opts_len = max([len(line[0]) for line in opt_output])
108 opts_len = max([len(line[0]) for line in opt_output])
95 ui.write(_(" options:\n\n"))
109 ui.write(_(" options:\n\n"))
96 for optstr, desc in opt_output:
110 for optstr, desc in opt_output:
97 if desc:
111 if desc:
98 s = "%-*s %s" % (opts_len, optstr, desc)
112 s = "%-*s %s" % (opts_len, optstr, desc)
99 else:
113 else:
100 s = optstr
114 s = optstr
101 ui.write(" %s\n" % s)
115 ui.write(" %s\n" % s)
102 ui.write("\n")
116 ui.write("\n")
103 # aliases
117 # aliases
104 if d['aliases']:
118 if d['aliases']:
105 ui.write(_(" aliases: %s\n\n") % " ".join(d['aliases']))
119 ui.write(_(" aliases: %s\n\n") % " ".join(d['aliases']))
106
120
107 # print topics
108 for names, sec, doc in helptable:
109 for name in names:
110 ui.write(".. _%s:\n" % name)
111 ui.write("\n")
112 section(sec)
113 if hasattr(doc, '__call__'):
114 doc = doc()
115 ui.write(doc)
116 ui.write("\n")
117
121
118 if __name__ == "__main__":
122 if __name__ == "__main__":
119 show_doc(sys.stdout)
123 show_doc(sys.stdout)
General Comments 0
You need to be logged in to leave comments. Login now