473,728 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8837
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...@ti m.thechases.com 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 (IndentedHelpFo rmatterWithNL.p y), but I'm
getting the following error:

class IndentedHelpFor matterWithNL(In dentedHelpForma tter):
NameError: name 'IndentedHelpFo rmatter' is not defined
I tried adding: from optparse imoport IndentedHelpFor matter 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 (IndentedHelpFo rmatterWithNL.p y), but I'm
getting the following error:

class IndentedHelpFor matterWithNL(In dentedHelpForma tter):
NameError: name 'IndentedHelpFo rmatter' is not defined
I tried adding: from optparse imoport IndentedHelpFor matter 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 "IndentedHelpFo rmatterWithNL.p y" that created
your parser with

parser = OptionParser(.. .
formatter=
IndentedHelpFor matterWithNL.In dentedHelpForma tterWithNL
)

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

from optparse import IndentedHelpFor matter
import textwrap

are at the top of the IndentedHelpFor matterWithNL.py file, not at
the top of the file importing the IndentedHelpFor matter.

-tkc
Nov 8 '07 #5
On Nov 8, 12:40 pm, Tim Chase <python.l...@ti m.thechases.com wrote:
Thanks for the help Tim. I just copied and pasted your code into a
file in my $PYTHONPATH (IndentedHelpFo rmatterWithNL.p y), but I'm
getting the following error:
class IndentedHelpFor matterWithNL(In dentedHelpForma tter):
NameError: name 'IndentedHelpFo rmatter' is not defined
I tried adding: from optparse imoport IndentedHelpFor matter 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 "IndentedHelpFo rmatterWithNL.p y" that created
your parser with

parser = OptionParser(.. .
formatter=
IndentedHelpFor matterWithNL.In dentedHelpForma tterWithNL
)

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

from optparse import IndentedHelpFor matter
import textwrap

are at the top of the IndentedHelpFor matterWithNL.py file, not at
the top of the file importing the IndentedHelpFor matter.

-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
2859
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 return value or callback option value like store action do? I modify optparse.py Option::take_action def and add there:
4
1980
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 first line of output from cout is lost, even if I clear() and flush(). The only way I have found to get the first line to appear is to put a newline at the start. It seems to be ignoring everything up to and including the first newline.
5
57199
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 could get only 1st line, but it is not returning 2nd line. here's code: string address = null; string ssep = Environment.NewLine; char sep = ssep.ToCharArray();
8
13041
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
3049
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 <xsl:text> </xsl:text> to output a carriage return and line feed but they get stripped at some point. I'm passing a StringWriter to the XslTransform.Transform method. Anyway to get this to actually output the tab and carriage and line feed characters?
5
13367
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
2474
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 ("Artist: " + artist + " CD Name: " + name + " Item Number: " + itemno + " Price: $" +price + " In Stock: " + nstock + " Stock Value: $" + value +
3
2086
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 actions: create, build, import, exp_cmd and interact. create -- Vaguely depreciated, should create a new project, but it is
1
17464
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 with the name field which I don't lile because I am testing the same condition twiceCASE WHEN CHARINDEX(' ',RTRIM(LTRIM(full_name))) > 0 THEN RTRIM(LTRIM(SUBSTRING(full_name,CHARINDEX(' ',full_name)+1,20))) ELSE RTRIM(LTRIM(full_name)) END...
0
8904
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8759
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9424
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9277
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9198
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9135
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6718
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4524
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4793
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.