Created
September 8, 2009 10:03
-
-
Save skriticos/182826 to your computer and use it in GitHub Desktop.
manpage like formatter function for python 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python3 | |
# Note: this was just a little toying, rst2man is much better. | |
import string | |
import textwrap | |
def _fmt_help(name, title, description, *args): | |
""" | |
IN | |
_fmt_help('foo', 'does something', 'the description', | |
['bar', 'do bar'], | |
[':baz', 'baz required parm'], | |
['q|quit', 'quit it'] | |
) | |
OUT | |
NAME | |
foo -- does something | |
SYNOPSIS | |
foo [bar] baz [q|quit] | |
DESCRIPTION | |
the description | |
OPTIONS | |
bar | |
do bar | |
baz | |
baz required parm | |
q|quit | |
quit it | |
""" | |
o = '\n' | |
o += 'NAME\n\t' | |
o += name + ' -- ' + title + '\n\n' | |
o += 'SYNOPSIS\n' | |
s = str() | |
s += name | |
for i in range(len(args)): | |
a = args[i][0] | |
if a[0] == ':': | |
s += ' ' + a[1:] | |
else: | |
s += ' [' + a + ']' | |
sl = textwrap.wrap(s, 64) | |
for i in range(len(sl)): | |
o += '\t' + sl[i] + '\n' | |
o += '\n' | |
o += 'DESCRIPTION' | |
dt = textwrap.wrap(description, 64) | |
for i in range(len(dt)): | |
o += '\n\t' + dt[i] | |
o += '\n\n' | |
o += 'OPTIONS' | |
for i in range(len(args)): | |
a = args[i] | |
if a[0][0] == ':': | |
a[0] = a[0][1:] | |
o += '\n\t' + a[0] | |
odt = textwrap.wrap(a[1], 60) | |
for j in range(len(odt)): | |
o += '\n\t ' + odt[j] | |
o += '\n' | |
return o | |
def __test_fmt_help(): | |
desc = ('the description ' + | |
'-- this must be a long text to test the description formating'+ | |
' routine of the help formatting function test' | |
) | |
obaz = ('baz option description' + | |
'-- also long to test option detail wrapping formatter ' + | |
'which should work fine hopefully.. blah..' | |
) | |
print(_fmt_help('foo', 'does something', desc, | |
['bar', 'do bar'], | |
[':baz', obaz], | |
['q|quit', 'quit it'] | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment