Formated String in optparse
Peter Otten
__peter__ at web.de
Thu Mar 31 04:25:13 EST 2005
Norbert Thek wrote:
> Is there an easy way to convince optparse to accept newline in the
> helpstring? and more importand also in the 'desc' string. I tried
> everything
> (from the os.linesep) to \n, \r, \r\n, ...
The "official" way (write your own Formatter class) is a bit tedious indeed.
Here's a hack that seems to work:
import textwrap
import optparse
class TextWrapper:
@staticmethod
def wrap(text, width=70, **kw):
result = []
for line in text.split("\n"):
result.extend(textwrap.wrap(line, width, **kw))
return result
@staticmethod
def fill(text, width=70, **kw):
result = []
for line in text.split("\n"):
result.append(textwrap.fill(line, width, **kw))
return "\n".join(result)
optparse.textwrap = TextWrapper()
parser = optparse.OptionParser(description="""\
einsamer nie als im august
erfuellungsstunde im gelaende
die roten und die goldenen braende
doch wo ist deiner gaerten lust
""")
parser.add_option("-x", "--example", help="""\
die seen hell die himmel weich
die aecker rein und glaenzen leise
doch wo sind sieg und siegsbeweise \
aus dem von dir vertretenen reich \
wo alles sich durch glueck""")
parser.print_help()
You should probably replace the "\n" literals with os.linesep.
Peter
More information about the Python-list
mailing list