Formated String in optparse

N

Norbert Thek

Hi

I'm using Python 24 on Windows > (2k)

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, ...

Norbert
 
P

Peter Otten

Norbert said:
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
 
M

MyHaz

If you haven't looked into it, you may like the way class
OptionParser() makes the help text for you.


- Haz
 
P

Peter Otten

MyHaz said:
If you haven't looked into it, you may like the way class
OptionParser() makes the help text for you.

What do you mean?

To clarify: OptionParser's help message in the default format is

"""
usage: discard_newline.py [options]

einsamer nie als im august erfuellungsstunde im gelaende die roten und die
goldenen braende doch wo ist deiner gaerten lust

options:
-h, --help show this help message and exit
-x EXAMPLE, --example=EXAMPLE
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
"""


whereas that same OptionParser with the tweaked optparse.textwrap (my
TextWrapper instance replacing the textwrap module from the standard
library) will output

"""
usage: preserve_newline.py [options]

einsamer nie als im august
erfuellungsstunde im gelaende
die roten und die goldenen braende
doch wo ist deiner gaerten lust


options:
-h, --help show this help message and exit
-x EXAMPLE, --example=EXAMPLE
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
"""

I guess that both poets in residence and limmerickistas will prefer the
latter form :)

Peter
 
N

Norbert Thek

Thank You for your help, its working!

Now I have an additional question.
The problem is the encoding of the Text
I'm using German, Can you tell me how to encode
the textstring that the Windows commandline shows the special letters
right?
For exampel i get 'f³r' but i want 'für' (maybe reader with only an english
enabled browser wouldn't see a difference..)

I tried to work with the encode method of string but It didn't work for me
some hint what to do?


Norbert
 
P

Peter Otten

Norbert said:
Thank You for your help, its working!

Now I have an additional question.

....which would warrant a separate thread...
The problem is the encoding of the Text
I'm using German, Can you tell me how to encode
the textstring that the Windows commandline shows the special letters
right?
For exampel i get 'f³r' but i want 'für' (maybe reader with only an
english enabled browser wouldn't see a difference..)

I tried to work with the encode method of string but It didn't work for me
some hint what to do? u'f\xb3r'
u'f\xfcr' # what you need

One way to get that (wrong) representation:
u'f\xb3r'

So it could be that you are interpreting cp1252 ("German" windows) as cp850
("German" DOS).

Try the following script:

# -*- coding: cp1252 -*-
text = u"text with umlauts äöü ÄÖÜ ß".encode("cp850")
print text


If that works correctly, you can prepare every string literal in your
program in the same way. Or redirect sys.stdout

sys.stdout = codecs.getwriter("cp850")(sys.stdout)

as posted by Martin von Löwis on de.comp.lang.python only two days ago.

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top