-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathhelp2rst
executable file
·120 lines (89 loc) · 3.41 KB
/
help2rst
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python3
# Copyright 2019 Regents of The University of Michigan.
# This file is part of geo-omics-scripts.
# Geo-omics-scripts is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# Geo-omics-scripts is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
# You should have received a copy of the GNU General Public License along
# with Geo-omics-scripts. If not, see <https://www.gnu.org/licenses/>.
"""
Generate docs from programs help printout
"""
import argparse
from pathlib import Path
import re
import subprocess
from string import Template
import sys
from textwrap import wrap, indent
DEFAULT_TEMPLATE = 'docs/template.txt'
OPTION_TMPL = Template("""\
.. option:: $args
$help
""")
HELP_RE = (r'^usage: (?P<program>[-\w]+) (?P<usage_args>.*?)'
r'\n\n(?P<description_1>*)$'
r'\n\npositional arguments:\n(?P<positional_args>.*)'
r'\n\noptional arguments:\n(?P<optional_args>.*?)'
r'\n\n(?P<description_2>.*)')
OPTION_RE = r'^ (?P<args>[^\n]*?[^\s])( +|\n)(?P<help>.*?)(?=(^ [^\s]|\Z))'
def parse_help(help_text):
"""
Parse help and return dictionary of text elements
"""
m = re.match(HELP_RE, help_text, re.MULTILINE | re.DOTALL)
if m is None:
raise RuntimeError('Failed to parse help text')
return m.groupdict()
def fix_options(opt_section):
ret = ''
for m in re.finditer(OPTION_RE, opt_section, re.MULTILINE | re.DOTALL):
sub = m.groupdict()
# rm existing formatting:
sub['help'] = re.sub(r' \s+', ' ', sub['help']).strip()
sub['help'] = '\n'.join(wrap(sub['help']))
sub['help'] = indent(sub['help'], ' ')
ret += OPTION_TMPL.substitute(sub)
ret = ret.rstrip()
return ret
def main():
argp = argparse.ArgumentParser(description=__doc__)
argp.add_argument(
'program',
nargs='?',
help='Path and name of the program. If this is not given, then the '
'program expects the help text to be provided via stdin.',
)
argp.add_argument(
'-t', '--template',
default=DEFAULT_TEMPLATE,
help='Path to template file.',
)
args = argp.parse_args()
if args.program is None:
help_text = sys.stdin.read()
else:
prog = Path(args.program)
if not prog.is_file():
argp.error('Program file not found: {}'.format(prog))
p = subprocess.run([str(prog), '-h'], stdout=subprocess.PIPE)
help_text = p.stdout.decode()
template = Path(args.template)
if not template.is_file():
argp.error('Template file not found: {}'.format(template))
template = Template(template.read_text())
# print(help_text)
sub = parse_help(help_text)
sub['usage_args'] = re.sub(r'\s+', ' ', sub['usage_args']) # rm newlines
sub['positional_args'] = fix_options(sub['positional_args'])
sub['optional_args'] = fix_options(sub['optional_args'])
sub['header_line'] = \
'=' * (len(sub['program']) + 3 + len(sub['description_1']))
print(template.substitute(**sub))
if __name__ == '__main__':
main()