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