How can I roll my own --help output?

"nemir nemiria" <[email protected]> Tue, 20 Jul 2004 04:59:16 +1000
Newsgroups gmane.comp.python.optik.user
Message-ID <[email protected]>
Please bear with me, I am pretty green when it comes to programming.

I wanted to write a littel thing that will report CPU usage.   It needed to take parameters, and it needs to behave like similar things that have been written before it.  What this means is that in response to a -h or --help parameter I need add the version message first,  to put in a little copyright message, print the basic syntax,  then print the full help for the options, and finally follow it up with bugs (if relevent) and a contact blurb.   This probably should happen no matter what other parameters were entered,  I guess.

In case of someone entering a false parameter is should simple print out a one line error and then follow that with the simple syntax summary,  like this:

$> /tmp/check_cpu -n
/tmp/check_cpu: invalid option --n

Usage:  check_cpu -w limit -c limit [-t timeout]
        check_cpu (-h|--help)
        check_cpu (-V|--version)



You can see from the code below that once I have parsed the parameters through optparse, when I run through the extra validation I can do this quite easily.   I am trying hard to figure out how to make the rest of this work like I want.  

I figured out with a google search that I could turn off the bonus help function by specifying add_help_option=0 in the function brackets,  but I don't know where this is documented...  is it add_version_option=0 for the version thing?  What other things can go there?

I am starting to lose sleep over this one,  which is a bit silly,  I guess.   Is this hard to do?  Is it really really hard for a pretty close to beginner programmer?  (I have done a few regex python scripts before tackling this one)


I wrote up the following parameter parsing and validation section in my program:


----

#######################################
###  Define a set of strings to handle
###  any info output requirements.


check_cpu_version = "check_cpu 0.3.1\n"

intro = "This tool comes with ABSOLUTELY NO WARRANTY.  You may redistribute\ncopies of the tool under the terms of the GNU General Public License.\nFor more information about these matters, see the file named COPYING.\nCopyright (c) 2004 Nemir Nemiria\n\n"

preamble = "This tool will check the percent of idle CPU usage on the system it is\nexecuted on and generate an alert if the percentage is below\none of the threshold values.\n\n"

use = "Usage:\tcheck_cpu -w limit -c limit [-t timeout]\n\tcheck_cpu (-h|--help)\n\tcheck_cpu (-V|--version)\n\n"

options = " -w, --warning=PERCENT\n\tExit with WARNING status if idle CPU percentage is less than PERCENT\n -c, --critical=PERCENT\n\tExit with CRITICAL status if idle CPU percentage is less than PERCENT\n -t, --timeout=INTEGER\n\tSeconds before check attempt times out (default: 20)\n -s, --sample=INTEGER\n\tSeconds to use as sample time. (default: 5)\n -h, --help\n\tPrint detailed help screen\n -V, --version\n\tPrint version information\n\n"

bugs = "Timeout does not do what has been documented here.  Rather, it does nothing.\nCannot figure out how to make it do something. Will investigate.\n\n"

query = "Send email to [email protected] if you have questions\nregarding the use of this software.  To submit patches or suggest improvements,\nsend email to \n\nFor questions and suggestions pertaining to the check_cpu plugin,\nplease send email to [email protected].\nIn fact, if you just wanna say hi, send email to [email protected] :-)"

fullHelp = check_cpu_version + intro + preamble + use +  options + bugs + query


#######################################
###  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")
parser.add_option("-w", "--warning", action="store", type="int", dest="warn", default=-1)
parser.add_option("-c", "--critical", action="store", type="int", dest="crit", default=-2)
parser.add_option("-s", "--sample", action="store", type="int", dest="sample", default=5)
parser.add_option("-t", "--timeout", action="store", type="int", dest="timeout", default=20)

(options, args) = parser.parse_args()

critical = options.crit
warning = options.warn
sample = options.sample
timeout = options.timeout


if -3 > critical or 101 < critical:
        print "Critical value is a percentage and must be between 0 and 100\n" + use
        sys.exit(3)

if -2 > warning or 101 < warning:
        print "Warning value is a percentage and must be between 0 and 100\n" + use
        sys.exit(3)

if critical >= warning:
        print "Critical value must be lower than warning value!!\n" + use
        sys.exit(3)


if sample >= timeout:
        print "Sample time must be lower than timeout!!\n" + use
        sys.exit(3)

...
-----

Thanks for your attention!

Nemir

PS -  For bonus points you could give me a hint as to how I could make a timeout thing that would take the passed in (or default) timeout value and break out with an appropriate error once that time period is met!!!!!

^_^
-- 
___________________________________________________________
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