Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

#12 Respect newlines in 'description'

open
Greg Ward
None
5
2004-11-06
2004-10-28
Anonymous
No

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
formatted:
---
usage: tool.py [-v] arg1 arg2

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

options:
-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.

Discussion

  • 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
    user_id=350137

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

    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
    user_id=297861

    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.