Re: How can I roll my own --help output?

"nemir nemiria" <[email protected]> Wed, 21 Jul 2004 04:53:49 +1000
Newsgroups gmane.comp.python.optik.user
Message-ID <[email protected]>
For the newbies that follow,   I have managed to roll my own afterall.

Need to read in close detail the doco on callbacks. 





> ----
> 
> #######################################
> ###  Define a set of strings to handle
> ###  any info output requirements.
<snipped the strings for brevity
> 
> fullHelp = check_cpu_version + intro + preamble + use +  options + bugs + query

Need to add the following function definitions:

...
def helpFunc(option, opt, value, parser):
        print fullHelp
        sys.exit(3)

def verFunc(option, opt, value, parser):
        print check_cpu_version
        sys.exit(3)
...




> #######################################
> ###  Parse all the parameters.  Define
> ###  variables for later use.
> 
> 
> 
> parser = OptionParser(add_help_option=0)
> 
> parser.add_option("-h", "--help")
> parser.add_option("-V", "--version")

Change these two lines to:

...
parser.add_option("-h", "--help", action="callback", callback=helpFunc)
parser.add_option("-V", "--version", action="callback", callback=verFunc)
...


And that is all that is required.  The later validation works for the short output requirements when a mistake is made on the command line.  From what I can tell,  this will process the parameters in the order they were defined,  irregardless of the order they appear on the command line.   So if the option parser sees a --help parameter,  it will immediately run the helpFunc() function (which in my case runs a sys.exit,  so doesn't run anything else.)

The only problem left now is that it seems to ignore anything that doesn't appear to be a part of the defined parameter syntax.  Is there anyway to make that invalid input break with an error and the short usage?


Thanks for your attention!

Nemir

-- 
___________________________________________________________
Sign-up for Ads Free at Mail.com
http://promo.mail.com/adsfreejump.htm



-------------------------------------------------------
This SF.Net email is sponsored by BEA Weblogic Workshop
FREE Java Enterprise J2EE developer tools!
Get your free copy of BEA WebLogic Workshop 8.1 today.
http://ads.osdn.com/?ad_id=4721&alloc_id=10040&op=click