[Tutor] Handling missing positional arguments

Geoff Dutton geoff.dutton at noaa.gov
Thu Nov 19 23:47:45 CET 2009


Hi Group,

I have been following the Tutor group for awhile but this is my first
post....

Do you have recommendations for handling missing positional arguments?  I'm
a huge fan of OptionParser module and use it in several programs to handle
options, but there must be an eligant way of handling missing arguments and
alerting the user.  Here is how I have done it ...

import sys
from subprocess import call
from optparse import OptionParser

if __name__=='__main__':

    opt = OptionParser(
        usage = "usage: %prog [options] site year",
        description = "Count number of ITX files from a station for a given
year."
    )
    opt.add_option("-e", action="store",
            dest="email_add", help="send table as email")
    opt.add_option("-l", '--lenght', action="store", default="30",
            dest="len", help="Number of lines in table.")

    (options, args) = opt.parse_args()

    if len(args) != 2:
        call(["countinj.py", "-h"])
        sys.exit()

    (site, year) = args

    <snip>


I like the "help" that OptionParser creates for my program when called with
the -h or --help option. Thus when len(args) is not correct I call the
program using the call function in the module subprocess.  But.... Do I
really need go to this trouble?  Is there a better way?

Thanks,
Geoff

-- 
NOAA Earth System Research Laboratory
Global Monitoring Division
325 Broadway, R/GMD1
Boulder, CO 80305
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091119/a26034da/attachment.htm>


More information about the Tutor mailing list