[Tutor] Handling missing positional arguments
Kent Johnson
kent37 at tds.net
Fri Nov 20 13:03:43 CET 2009
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
>
> (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
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
More information about the Tutor
mailing list