[Tutor] Handling missing positional arguments

Geoff Dutton geoff.dutton at noaa.gov
Fri Nov 20 16:40:54 CET 2009


On Fri, Nov 20, 2009 at 5:03 AM, Kent Johnson <kent37 at tds.net> wrote:

> On Thu, Nov 19, 2009 at 5:47 PM, Geoff Dutton <geoff.dutton at noaa.gov>
> wrote:
> > 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()
>
> opt.print_help() seems to be what you want here.
>
> Kent
>
>
Exactly!  Thank you.
Geoff
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091120/4300fef1/attachment-0001.htm>


More information about the Tutor mailing list