tags:

views:

84

answers:

2

Hi there,

is possible to make own help message or attach own event on help option using optparse module in Python?

A: 

You should be able to replace the default help mechanism with your own merely by subclassing OptionParser and overriding the print_help() method.

Peter Hansen
I'm curious as to how this is the accepted response when it is way over-engineered in the way you would do it. There's no need to do this at all as there is already a method on the class to do this.
Lee
@Lee, I believe you misinterpreted the request. Your approach will replace only the "usage" text, but the output to the help option will still display the formatted text showing all the options, defaults, etc. It appears the OP wanted to replace *all* the output and overriding may be the best way to do that.
Peter Hansen
A: 

Sure - just use the params to the OptionParser constructor:

import optparse

help_text = """
Hi, this is a really long help message for %prog. 

It's a pretty ace thing. (C)2010 Stuff etc.
"""
parser = optparse.OptionParser(usage=help_text, version="%prog 1.0 beta")
(options, args) = parser.parse_args()
Lee
Did you mean to use description instead of usage?
Roger Pate
Both are very similar but either works. The only difference is description won't overwrite the 'usage' line. At least, from what I can tell. 'description' also removes all new lines.
Lee
Given that this does not eliminate the automatic output of all the options, I'm not sure it actually addresses the request fully.
Peter Hansen
You can use callback inside optparse.OptionParser function to call your function
Gunslinger_