467,879 Members | 1,210 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,879 developers. It's quick & easy.

Formated String in optparse

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
Jul 18 '05 #1
  • viewed: 2011
Share:
5 Replies
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
Jul 18 '05 #2
If you haven't looked into it, you may like the way class
OptionParser() makes the help text for you.
- Haz

Jul 18 '05 #3
MyHaz wrote:
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
Jul 18 '05 #4
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 'fr' but i want 'fr' (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
Jul 18 '05 #5
Norbert Thek wrote:
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 'fr' but i want 'fr' (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"fr" # what you have (minus unicode) u'f\xb3r'
u"fr" u'f\xfcr' # what you need

One way to get that (wrong) representation:
u"fr".encode("cp1252").decode("cp850")

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 Lwis on de.comp.lang.python only two days ago.

Peter


Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Hans-Joachim Widmaier | last post: by
7 posts views Thread by Henry Ludemann | last post: by
3 posts views Thread by Karlo Lozovina | last post: by
3 posts views Thread by Bob | last post: by
reply views Thread by Steven Bethard | last post: by
reply views Thread by Robert Kern | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.