473,410 Members | 1,977 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,410 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 8814
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.