| 1 |
#!/usr/bin/python3 |
| 2 |
|
| 3 |
import sys |
| 4 |
import re |
| 5 |
|
| 6 |
def get_name(lines): |
| 7 |
for line in lines: |
| 8 |
if line.startswith('Usage: '): |
| 9 |
return line[7:].strip().split()[0] |
| 10 |
pass |
| 11 |
|
| 12 |
def get_usage(lines): |
| 13 |
for line in lines: |
| 14 |
if line.startswith('Usage: '): |
| 15 |
return line[7:].strip() |
| 16 |
pass |
| 17 |
|
| 18 |
def get_description(lines): |
| 19 |
return lines[0].strip() |
| 20 |
pass |
| 21 |
|
| 22 |
def get_options(lines): |
| 23 |
name = None |
| 24 |
options = {} |
| 25 |
option_line = re.compile(r'^([^:]*[Oo]ptions):') |
| 26 |
flag_line = re.compile(r'^\s*--([^\s]+)(?: (arg)( \(=[^\s]+\))?)?\s+([^\s].*)$') |
| 27 |
# flag_line = re.compile(r'^\s*--([^\s]+)(?: (arg))?\s+([^\s].*)$') |
| 28 |
short_flag_line = re.compile(r'^\s*-(\w) \[ --([^\s]+) \](?: (arg)( \(=[^\s]+\))?)?\s+([^\s].*)$') |
| 29 |
current_option = False |
| 30 |
for line in lines: |
| 31 |
if line.strip() == '': |
| 32 |
current_option = False |
| 33 |
continue; |
| 34 |
|
| 35 |
m = option_line.match(line) |
| 36 |
if m: |
| 37 |
name = m.group(1) |
| 38 |
options[name] = [] |
| 39 |
current_option = False |
| 40 |
continue |
| 41 |
|
| 42 |
m = flag_line.match(line) |
| 43 |
if m: |
| 44 |
options[name].append([m.group(1),m.group(2),m.group(3),m.group(4)]) |
| 45 |
current_option = True |
| 46 |
continue |
| 47 |
|
| 48 |
m = short_flag_line.match(line) |
| 49 |
if m: |
| 50 |
options[name].append([m.group(2),m.group(1),m.group(3),m.group(4),m.group(5)]) |
| 51 |
current_option = True |
| 52 |
continue; |
| 53 |
|
| 54 |
if current_option: |
| 55 |
options[name][-1][-1] = options[name][-1][-1] + ' ' + line.strip() |
| 56 |
return options |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
def get_examples(lines): |
| 61 |
# Remove lines up to 'Examples:' |
| 62 |
while lines and not lines[0].startswith('Examples:'): |
| 63 |
lines = lines[1:] |
| 64 |
if not lines: |
| 65 |
return None |
| 66 |
lines = lines[1:] |
| 67 |
|
| 68 |
out_lines = [] |
| 69 |
in_quote = False |
| 70 |
for line in lines: |
| 71 |
if line.startswith('%'): |
| 72 |
if not in_quote: |
| 73 |
in_quote = True |
| 74 |
out_lines = out_lines + ['```'] |
| 75 |
elif in_quote: |
| 76 |
in_quote = False |
| 77 |
out_lines = out_lines + ['```'] |
| 78 |
|
| 79 |
out_lines = out_lines + [line] |
| 80 |
if in_quote: |
| 81 |
out_lines = out_lines + ['```'] |
| 82 |
|
| 83 |
return '\n'.join(out_lines) |
| 84 |
|
| 85 |
|
| 86 |
help = {} |
| 87 |
|
| 88 |
lines = sys.stdin.readlines() |
| 89 |
lines = [x.strip() for x in lines] |
| 90 |
|
| 91 |
help['name'] = get_name(lines) |
| 92 |
help['usage'] = get_usage(lines) |
| 93 |
help['description'] = get_description(lines) |
| 94 |
help['options'] = get_options(lines) |
| 95 |
help['examples'] = get_examples(lines) |
| 96 |
|
| 97 |
print('% {}(1)'.format(help['name'])) |
| 98 |
print('% Benjamin Redelings') |
| 99 |
print('% Feb 2018') |
| 100 |
print() |
| 101 |
print('# NAME\n\n**{}** - {}\n'.format(help['name'],help['description'])) |
| 102 |
|
| 103 |
synopsis = help['usage'] |
| 104 |
synopsis = re.sub(r'^([^\s]+)',r'**\1**',synopsis) |
| 105 |
synopsis = re.sub(r'<([^\s>][^>]*[^\s>])>',r'_\1_',synopsis) |
| 106 |
print('# SYNOPSIS\n\n{}\n'.format(synopsis)) |
| 107 |
|
| 108 |
print('# DESCRIPTION\n\n{}\n'.format(help['description'])) |
| 109 |
|
| 110 |
for option_group in help['options']: |
| 111 |
options = help['options'][option_group] |
| 112 |
if len(options): |
| 113 |
print('# {}:'.format(option_group).upper()) |
| 114 |
for option in options: |
| 115 |
# print(option) |
| 116 |
arg = ' _arg_' if option[-3] is not None else '' |
| 117 |
value = option[-2] |
| 118 |
value = value if value else '' |
| 119 |
has_short = (len(option) == 5) |
| 120 |
description = option[-1].replace('[','\\[').replace(']','\\]') |
| 121 |
|
| 122 |
# Change <arg> to _arg> |
| 123 |
description = re.sub(r'<([^\s>][^>]*[^\s>])>',r'_\1_',description) |
| 124 |
|
| 125 |
if has_short: |
| 126 |
print("**-{}**{}{}, ".format(option[1],arg,value),end="") |
| 127 |
print("**--{}**{}{}\n: {}\n".format(option[0],arg,value,description)) |
| 128 |
print() |
| 129 |
|
| 130 |
if help['examples'] is not None: |
| 131 |
print("# EXAMPLES:\n {}\n".format(help['examples'])) |
| 132 |
|
| 133 |
print("# REPORTING BUGS:\n BAli-Phy online help: <http://www.bali-phy.org/docs.php>.\n\nPlease send bug reports to <[email protected]>.\n"); |
| 134 |
|
| 135 |
#print("# SEE ALSO:") |