#12 Respect newlines in 'description'


I always invoke optparse like this:
# specify constants
PROG_VERSION = "Version 0.1, January NN, 2004"
PROG_DESCRIPTION = """tool.py counts lines in each file.\n
Author: Tomi Mickelsson. """ + PROG_VERSION

# parse options and arguments
parser = OptionParser(PROG_USAGE,
option_list = option_list,
version = PROG_VERSION,
description = PROG_DESCRIPTION)
That is, I always specify the description info.
However, I'm not happy with the way description gets
usage: tool.py [-v] arg1 arg2

tool.py counts lines in each file. Author: Tomi
Mickelsson. Version 0.1, January NN, 2004

-v verbose
-h, --help show this help message and exit
My new lines are removed from the description field and
the result looks awful and arbitrary. I want to control
the new lines.

Could there be an option to insert "hard" new lines in
the description field? I wouldn't want to override
OptionParser.format_description function, I want an
easy solution that doesn't pollute my scripts; I use
optparse in all my scripts, it's a very useful module.

It seems that 1.5a2 works the same as earlier versions.


  • Greg Ward

    Greg Ward - 2004-11-06
    • assigned_to: nobody --> gward
    • summary: description field and line feeds --> Respect newlines in 'description'
  • Micah Elliott

    Micah Elliott - 2005-07-15

    Logged In: YES

    I would like to additionally request that newlines be
    honored in all areas, and support for them be placed in all

    I'm going to submit an RFE for an "example" arg to
    OptionParser, which would also need newline support.

    I also recommend that newlines only get inserted where
    requested. So by default without any explicit user-inserted
    newlines, the output is:

    $ tool.py --help
    usage: tool.py [...] <<usage>>
    Counts lines... <<description>>
    options: <<option_list>>
    -h, --help ...
    example: <<example>>
    tool.py -z foo
    Report bugs to <foo@bar.com> <<epilog>>

    * * *

    With this in place, users have much more flexibility in how
    to lay out their help. This is especially useful when using
    a tool like "help2man" which expects newlines in certain
    places in order to create a proper manpage.

    Thanks Greg!

  • Dark Ryder

    Dark Ryder - 2005-08-26

    Logged In: YES

    I ran into this same issue with one of my own scripts. I
    took a look at the source and it's easy to fix -- textwrap
    (which handles all the Optik wrapping) has a
    replace_whitespace argument which defaults to true. I
    hacked up a new HelpFormatter which supports that option.

    I've also ported the changes to Optik 1.5 and will submitted
    the patch to the project. It adds
    "preserve_whitespace=True/False" to the HelpFormatter init
    (defaults to False for backwards compatibility) and also
    adds a SUPPRESS_METAVAR special value which does what it says.


Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.

No, thanks