473,324 Members | 2,193 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

How to output newline or carriage return with optparse

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
5 8806
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: sector119 | last post by:
Hi I use optparse with callback action, my callback function return some value, but optparse does not store this value, options.callback_dest always is None. How can I store callback function...
4
by: rossum | last post by:
I have been looking at exceptions as I need to get better at using them. I came across an interesting effect, demonstrated below. When I ctrl-Z the input to throw an ios_base::failure, the...
5
by: Vamsi | last post by:
Hi, I am trying a basic opearation of splitting a multiline value to an array of single lines(Actually making Address into AddressLine1, AddressLine2). I used Environment.NewLine in split, I...
8
by: John Grandy | last post by:
in VB.NET you can do sb = New StringBuilder("Hello") sb.Append(vbCrLf) What is the C# analogy to vbCrLf ?
2
by: Buddy Ackerman | last post by:
Apparently .NET strips these white space characters (MSXML doesn't) regardless of what the output method is set to. I'm using <xsl:text> </xsl:text> to output a tab character and...
5
by: vflyer | last post by:
I there a quick routine that removes trailing newline chars and carriage return? vFlyer Labs http://www.vflyerlabs.com
9
by: no1zson | last post by:
I have a program whose output goes to a simple jpanel. I am using a toString to get it there, but as it is it shows up as one super long string of data. public String toString() { return...
3
by: Dan | last post by:
I've been using optparse for a while, and I have an option with a number of sub-actions I want to describe in the help section: parser.add_option("-a", "--action", help=\ """Current supported...
1
code green
by: code green | last post by:
I have an address field in a table that contains one, two, three or more lines of an address. I need to split this into three fields for later insertion into CSV. I have done something similar...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.