473,396 Members | 2,002 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,396 software developers and data experts.

An optparse question

T
I have a short program using optparse.OptionParser that prints out help
message with -h flag:

% myprog.py -h
usage: myprog.py [options] input_file

options:
-h, --help show this help message and exit
-v, --verbose print program's version number and exit
-o FILE Output file
My question is, is there a way to print a blank line (or any string)
before "usage: myprog.py [options] input_file" ? I tried using
callbacks without success. I think somehow I need to modify the
behavior of optparse.OptionParser.print_usage() function?

Jul 21 '06 #1
8 3516

T wrote:
I have a short program using optparse.OptionParser that prints out help
message with -h flag:

% myprog.py -h
usage: myprog.py [options] input_file

options:
-h, --help show this help message and exit
-v, --verbose print program's version number and exit
-o FILE Output file
My question is, is there a way to print a blank line (or any string)
before "usage: myprog.py [options] input_file" ? I tried using
callbacks without success. I think somehow I need to modify the
behavior of optparse.OptionParser.print_usage() function?
you can make the usage line anything you want.

....
usage = 'This is a line before the usage line\nusage %prog [options]
input_file'
parser = OptionsParser(usage=usage)
parser.print_help()
....

Jul 21 '06 #2
T
fuzzylollipop wrote:
>
you can make the usage line anything you want.

...
usage = 'This is a line before the usage line\nusage %prog [options]
input_file'
parser = OptionsParser(usage=usage)
parser.print_help()
...
No, that affects the string printed only *after* the "usage = " string.
What I would like to do is insert some string *before* the "usage = "
string, which is right after the command I type at the command prompt.
So I would like to make it look like this:

% myprog.py -h
************ THIS IS NEWLY INSERTED STRING ************
usage: myprog.py [options] input_file
options:
-h, --help show this help message and exit
-v, --verbose print program's version number and exit
-o FILE Output file

Jul 21 '06 #3
No, that affects the string printed only *after* the "usage = " string.
What I would like to do is insert some string *before* the "usage = "
string, which is right after the command I type at the command prompt.
So I would like to make it look like this:
The example was fine (except for a typo) as far as demonstrating the
concept. Try this corrected version:

from optparse import OptionParser

usage = '************ THIS IS NEWLY INSERTED STRING
************\nusage: %prog [options] input_file'
parser = OptionParser(usage=usage)
parser.print_help()

Jul 21 '06 #4
T wrote:
fuzzylollipop wrote:

you can make the usage line anything you want.

...
usage = 'This is a line before the usage line\nusage %prog [options]
input_file'
parser = OptionsParser(usage=usage)
parser.print_help()
...

No, that affects the string printed only *after* the "usage = " string.
What I would like to do is insert some string *before* the "usage = "
string, which is right after the command I type at the command prompt.
So I would like to make it look like this:

% myprog.py -h
************ THIS IS NEWLY INSERTED STRING ************
usage: myprog.py [options] input_file
options:
-h, --help show this help message and exit
-v, --verbose print program's version number and exit
-o FILE Output file
It's possible, but it ain't easy:

from optparse import OptionParser, _, IndentedHelpFormatter

class MyFormatter(IndentedHelpFormatter):
pre_usage = "Hi there!\n"
def format_usage(self, usage):
return _("%susage: %s\n") % (self.pre_usage, usage)

parser = OptionParser(formatter=MyFormatter())
The above filthy hack will print "Hi there!" before the usual usage
message.

Jul 21 '06 #5
dan.g...@gmail.com wrote:
No, that affects the string printed only *after* the "usage = " string.
What I would like to do is insert some string *before* the "usage = "
string, which is right after the command I type at the command prompt.
So I would like to make it look like this:

The example was fine (except for a typo) as far as demonstrating the
concept. Try this corrected version:

from optparse import OptionParser

usage = '************ THIS IS NEWLY INSERTED STRING
************\nusage: %prog [options] input_file'
parser = OptionParser(usage=usage)
parser.print_help()
Nope. That only *nearly* does what T wants. The usage message will
still be printed immediately *after* the 'usage: ' string.
>>parser = OptionParser(usage=usage)
parser.print_help()
usage: ************ THIS IS NEWLY INSERTED STRING************
usage: lopts.py [options] input_file

options:
-h, --help show this help message and exit
I had the same problem, and in order to get something printed before
the usage message, I found one easy-ish way was to subclass the
Formatter passed in to the Parser.

IMHO, optparse does a tricky task well, but it's implemented in a hard
to follow, inflexible manner. My "favorite" pet peeve is that the
options "dictionary" it returns isn't a dict. I wound up doing this to
it to get something [I considered] useful:

o, a = parser.parse_args()
o = o.__dict__.copy()
Peace,
~Simon

Jul 21 '06 #6
T wrote:
fuzzylollipop wrote:
>>you can make the usage line anything you want.

...
usage = 'This is a line before the usage line\nusage %prog [options]
input_file'
parser = OptionsParser(usage=usage)
parser.print_help()
...


No, that affects the string printed only *after* the "usage = " string.
What I would like to do is insert some string *before* the "usage = "
string, which is right after the command I type at the command prompt.
So I would like to make it look like this:

% myprog.py -h
************ THIS IS NEWLY INSERTED STRING ************
usage: myprog.py [options] input_file
options:
-h, --help show this help message and exit
-v, --verbose print program's version number and exit
-o FILE Output file
Do a Google search for "monkey patching". You probably want to
monkey-patch the class's usage method.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Jul 21 '06 #7
Nope. That only *nearly* does what T wants. The usage message will
still be printed immediately *after* the 'usage: ' string.
>parser = OptionParser(usage=usage)
parser.print_help()
usage: ************ THIS IS NEWLY INSERTED STRING************
usage: lopts.py [options] input_file

options:
-h, --help show this help message and exit
Yes, I see what T meant now. Behavior expectations (assumptions) has a
way of clouding one's vision.

Thanks

Jul 21 '06 #8
"T" <ty*****@yahoo.comwrites:
[...]
What I would like to do is insert some string *before* the "usage = "
string, which is right after the command I type at the command prompt.
So I would like to make it look like this:

% myprog.py -h
************ THIS IS NEWLY INSERTED STRING ************
usage: myprog.py [options] input_file
options:
-h, --help show this help message and exit
-v, --verbose print program's version number and exit
-o FILE Output file
HelpFormatter is what you need. Seems undocumented in the official
docs, but doesn't look risky to use (famous last words). Seems just
that nobody got around to documenting it.

import optparse

class NonstandardHelpFormatter(optparse.HelpFormatter):

def __init__(self,
indent_increment=2,
max_help_position=24,
width=None,
short_first=1):
optparse.HelpFormatter.__init__(
self, indent_increment, max_help_position, width, short_first)

def format_usage(self, usage):
return "************ THIS IS NEWLY INSERTED STRING ************\nusage: %s\n" % usage

def format_heading(self, heading):
return "%*s%s:\n" % (self.current_indent, "", heading)

parser = optparse.OptionParser(
usage="%prog [options] input_file",
formatter=NonstandardHelpFormatter())
parser.parse_args()
John
Jul 21 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Hans-Joachim Widmaier | last post by:
I was really pleased when the Optik module found its way into the standard Python battery compartment, as it matched all my option parsing requirements. But, as always, there's really nothing that...
3
by: washu | last post by:
Hi, I'm was going through the module help located on http://docs.python.org/lib/optparse-store-action.html and I tried modifying it a tiny bit and things don't work. If someone could tell me...
5
by: GMTaglia | last post by:
Hi guys, I was wondering why optparse accept an option if in the command line is not *exactly* the one present in the source code....may be an example will explain better.... #!/usr/bin/env...
7
by: Henry Ludemann | last post by:
I've been writing an optparse alternative (using getopt) that is at a stage where I'd be interested in people's opinions. It allows you to easily creating command line interfaces to existing...
3
by: Tomi Silander | last post by:
Hi, this must have been asked 1000 times (or nobody is as stupid as me), but since I could not find the answer, here is the question. My program mitvit.py: -------------- import optparse...
3
by: Karlo Lozovina | last post by:
If I create a file with only one line: --- from optparse import OptionParser --- I get this when I try to run it from "DOS" prompt: Traceback (most recent call last): File "optparse.py",...
3
by: Bob | last post by:
I'd like to setup command line switches that are dependent on other switches, similar to what rpm does listed below. From the grammar below we see that the "query-options" are dependent on the...
0
by: Steven Bethard | last post by:
I feel like I must be reinventing the wheel here, so I figured I'd post to see what other people have been doing for this. In general, I love the optparse interface, but it doesn't do any checks...
0
by: Robert Kern | last post by:
Jeff Keasler wrote: If you code it up with unit tests and documentation, it has a good chance. But in the meantime, you can tell optparse to stop processing options using the standard "--"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.