How to output newline or carriage return with optparse

J

john.m.roach

I'm trying to implement some simple command line options. Some of the
'help' sections are long and I would like to control line breaks. How
do you do this?

Thanks!
 
T

Tim Chase

I'm trying to implement some simple command line options.
Some of the 'help' sections are long and I would like to
control line breaks. How do you do this?

I had this problem earlier and solved it here:

http://groups.google.com/group/comp.lang.python/browse_frm/thread/6df6e6b541a15bc2/09f28e26af0699b1

thanks to a little guidance from Ben Finney on where to look.

It came up again last month here:

http://groups.google.com/group/comp.lang.python/browse_frm/thread/e72deee779d9989b/

which should solve the problem for you.

ASIDE: I've started refactoring this bit out in my local
source...how would I go about contributing it back to the Python
code-base? I didn't get any feedback from posting to the Optik
site. My refactor basically takes an optional pre-processing
function to parse your string into the output that gets passed to
textwrap.wrap() and textwrap.fill(), defaulting to the old
behavior, but offering a function for splitting it into
paragraphs based on newlines.

-tkc
 
S

Steven Bethard

Tim said:
ASIDE: I've started refactoring this bit out in my local source...how
would I go about contributing it back to the Python code-base? I didn't
get any feedback from posting to the Optik site.

You can post a patch to bugs.python.org, but it will probably just get
forwarded to the optik site because the optparse module is supposed to
be automatically generated from the current optik release. My impression
is that optik is mostly unmaintained, which makes getting patches
applied to optik difficult...

STeVe

P.S. FWIW, I think it would be great to get your patch in -- this
feature is requested all the time.
 
T

Tim Chase

ASIDE: I've started refactoring this bit out in my local
You can post a patch to bugs.python.org, but it will probably
just get forwarded to the optik site because the optparse
module is supposed to be automatically generated from the
current optik release. My impression is that optik is mostly
unmaintained, which makes getting patches applied to optik
difficult...

What sorts of things are required? Just a patch? Or do I also
need to include some sort of collection of doctest/unittests or
documentation patches (and if so, against which documentation
source)?

-tkc
 
S

Steven Bethard

Tim said:
What sorts of things are required? Just a patch? Or do I also
need to include some sort of collection of doctest/unittests or
documentation patches (and if so, against which documentation
source)?

Yes, your patch will need to include tests and docs as well. I would
make the patch against the sources from the optik site (though I didn't
see a CVS or SVN repository so you may just have to do it against the
source release files). Add your tests to test/test_optik.py and your
doc fixes to doc/reference.txt (or whichever doc/ file seems most
appropriate).

Thanks for taking this on, BTW.

STeVe
 
J

john.m.roach

I had this problem earlier and solved it here:

http://groups.google.com/group/comp.lang.python/browse_frm/thread/6df...

thanks to a little guidance from Ben Finney on where to look.

It came up again last month here:

http://groups.google.com/group/comp.lang.python/browse_frm/thread/e72...

which should solve the problem for you.

ASIDE: I've started refactoring this bit out in my local
source...how would I go about contributing it back to the Python
code-base? I didn't get any feedback from posting to the Optik
site. My refactor basically takes an optional pre-processing
function to parse your string into the output that gets passed to
textwrap.wrap() and textwrap.fill(), defaulting to the old
behavior, but offering a function for splitting it into
paragraphs based on newlines.

-tkc


Thanks for the help Tim. I just copied and pasted your code into a
file in my $PYTHONPATH (IndentedHelpFormatterWithNL.py), but I'm
getting the following error:

class IndentedHelpFormatterWithNL(IndentedHelpFormatter):
NameError: name 'IndentedHelpFormatter' is not defined


I tried adding: from optparse imoport IndentedHelpFormatter into the
aforementioned file, but no luck again. What am I missing???

Thanks.
 
T

Tim Chase

Thanks for the help Tim. I just copied and pasted your code into a
file in my $PYTHONPATH (IndentedHelpFormatterWithNL.py), but I'm
getting the following error:

class IndentedHelpFormatterWithNL(IndentedHelpFormatter):
NameError: name 'IndentedHelpFormatter' is not defined


I tried adding: from optparse imoport IndentedHelpFormatter into the
aforementioned file, but no luck again. What am I missing???

spelling "import" correctly? :) Also, make sure that, if you've
named your module "IndentedHelpFormatterWithNL.py" that created
your parser with

parser = OptionParser(...
formatter=
IndentedHelpFormatterWithNL.IndentedHelpFormatterWithNL
)

You'll also want to make sure that these two lines:

from optparse import IndentedHelpFormatter
import textwrap

are at the top of the IndentedHelpFormatterWithNL.py file, not at
the top of the file importing the IndentedHelpFormatter.

-tkc
 
J

John

spelling "import" correctly? :) Also, make sure that, if you've
named your module "IndentedHelpFormatterWithNL.py" that created
your parser with

parser = OptionParser(...
formatter=
IndentedHelpFormatterWithNL.IndentedHelpFormatterWithNL
)

You'll also want to make sure that these two lines:

from optparse import IndentedHelpFormatter
import textwrap

are at the top of the IndentedHelpFormatterWithNL.py file, not at
the top of the file importing the IndentedHelpFormatter.

-tkc

That seems to do the trick. I'm sure there are quite a few people out
there who have run into this same problem-- I'm glad you're adding it
to the patches!
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top