By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,173 Members | 796 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,173 IT Pros & Developers. It's quick & easy.

How to output newline or carriage return with optparse

P: n/a
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!

Nov 8 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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....f28e26af0699b1

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....deee779d9989b/

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


Nov 8 '07 #2

P: n/a
Tim Chase wrote:
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.
Nov 8 '07 #3

P: n/a
On Nov 8, 11:46 am, Tim Chase <python.l...@tim.thechases.comwrote:
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....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....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.

Nov 8 '07 #4

P: n/a
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.IndentedHelpFormatterW ithNL
)

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
Nov 8 '07 #5

P: n/a
On Nov 8, 12:40 pm, Tim Chase <python.l...@tim.thechases.comwrote:
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.IndentedHelpFormatterW ithNL
)

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!

Nov 8 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.