Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to output newline or carriage return with optparse

Reply
Thread Tools

How to output newline or carriage return with optparse

 
 
john.m.roach@gmail.com
Guest
Posts: n/a
 
      11-08-2007
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!

 
Reply With Quote
 
 
 
 
Tim Chase
Guest
Posts: n/a
 
      11-08-2007
> 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




 
Reply With Quote
 
 
 
 
Steven Bethard
Guest
Posts: n/a
 
      11-08-2007
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.
 
Reply With Quote
 
Tim Chase
Guest
Posts: n/a
 
      11-08-2007
>> 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...


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



 
Reply With Quote
 
Steven Bethard
Guest
Posts: n/a
 
      11-08-2007
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...

>
> 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
 
Reply With Quote
 
john.m.roach@gmail.com
Guest
Posts: n/a
 
      11-08-2007
On Nov 8, 11:46 am, Tim Chase <(E-Mail Removed)> wrote:
> > 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.

 
Reply With Quote
 
Tim Chase
Guest
Posts: n/a
 
      11-08-2007
> 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


 
Reply With Quote
 
John
Guest
Posts: n/a
 
      11-08-2007
On Nov 8, 12:40 pm, Tim Chase <(E-Mail Removed)> wrote:
> > 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!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Routine to remove extra newline chars and carriage return vflyer Javascript 5 06-01-2007 02:00 AM
Newline & carriage return characters over SOAP HTTP =?Utf-8?B?V2ViTWF0cml4?= ASP .Net 0 03-13-2006 08:40 PM
Newline & carriage return charecters over SOAP HTTP WebMatrix ASP .Net Web Services 0 03-10-2006 03:51 PM
Help: Delete a single carriage return in a file, but not a double carriage return? Steve Anderson Perl Misc 3 06-21-2004 12:48 AM



Advertisments