help2rst.py: Support Python 3.4

This commit is contained in:
Tatsuhiro Tsujikawa 2015-06-07 12:55:22 +09:00
parent 6d537c419e
commit e857e99df8
1 changed files with 26 additions and 25 deletions

View File

@ -4,6 +4,7 @@
# script to produce rst file from program's help output.
from __future__ import unicode_literals
from __future__ import print_function
import sys
import re
import argparse
@ -44,8 +45,8 @@ def help2man(infile):
line = infile.readline().strip()
m = re.match(r'^Usage: (.*)', line)
if not m:
print 'usage line is invalid. Expected following lines:'
print 'Usage: cmdname ...'
print('usage line is invalid. Expected following lines:')
print('Usage: cmdname ...')
sys.exit(1)
synopsis = m.group(1).split(' ', 1)
if len(synopsis) == 2:
@ -60,7 +61,7 @@ def help2man(infile):
break
description.append(line)
print '''
print('''
.. GENERATED by help2rst.py. DO NOT EDIT DIRECTLY.
.. program:: {cmdname}
@ -79,7 +80,7 @@ DESCRIPTION
{description}
'''.format(cmdname=cmdname, args=args,
cmdnameunderline='=' * (len(cmdname) + 3),
synopsis=synopsis, description=format_text('\n'.join(description)))
synopsis=synopsis, description=format_text('\n'.join(description))))
in_arg = False
in_footer = False
@ -88,16 +89,16 @@ DESCRIPTION
line = line.rstrip()
if not line.strip() and in_arg:
print ''
print()
continue
if line.startswith(' ') and in_arg:
if not line.startswith(arg_indent):
sys.stderr.write('warning: argument description is not indented correctly. We need {} spaces as indentation.\n'.format(len(arg_indent)))
print '{}'.format(format_arg_text(line[len(arg_indent):]))
print('{}'.format(format_arg_text(line[len(arg_indent):])))
continue
if in_arg:
print ''
print()
in_arg = False
if line == '--':
@ -105,22 +106,22 @@ DESCRIPTION
continue
if in_footer:
print line.strip()
print(line.strip())
continue
if line == 'Options:':
print 'OPTIONS'
print '-------'
print ''
print('OPTIONS')
print('-------')
print()
continue
if line.startswith(' <'):
# positional argument
m = re.match(r'^(?:\s+)([a-zA-Z0-9-_<>]+)(.*)', line)
argname, rest = m.group(1), m.group(2)
print '.. describe:: {}'.format(argname)
print ''
print '{}'.format(format_arg_text(rest.strip()))
print('.. describe:: {}'.format(argname))
print()
print('{}'.format(format_arg_text(rest.strip())))
in_arg = True
continue
@ -128,9 +129,9 @@ DESCRIPTION
# positional argument
m = re.match(r'^(?:\s+)(\([a-zA-Z0-9-_<> ]+\))(.*)', line)
argname, rest = m.group(1), m.group(2)
print '.. describe:: {}'.format(argname)
print ''
print '{}'.format(format_arg_text(rest.strip()))
print('.. describe:: {}'.format(argname))
print()
print('{}'.format(format_arg_text(rest.strip())))
in_arg = True
continue
@ -140,23 +141,23 @@ DESCRIPTION
r'^(?:\s+)(-\S+?(?:, -\S+?)*)($| .*)',
line)
argname, rest = m.group(1), m.group(2)
print '.. option:: {}'.format(argname)
print ''
print('.. option:: {}'.format(argname))
print()
rest = rest.strip()
if len(rest):
print '{}'.format(format_arg_text(rest))
print('{}'.format(format_arg_text(rest)))
in_arg = True
continue
if not line.startswith(' ') and line.endswith(':'):
# subsection
subsec = line.strip()[:-1]
print '{}'.format(subsec)
print '{}'.format('~' * len(subsec))
print ''
print('{}'.format(subsec))
print('{}'.format('~' * len(subsec)))
print()
continue
print line.strip()
print(line.strip())
def format_text(text):
# escape *
@ -186,6 +187,6 @@ if __name__ == '__main__':
args = parser.parse_args()
help2man(sys.stdin)
if args.include:
print ''
print()
with open(args.include) as f:
sys.stdout.write(f.read())