Python noob SOS (any [former?] Perlheads out there?)

samwyse dejanews at email.com
Sat Feb 2 23:01:58 EST 2008


kj wrote:
> I'd written a Perl module to facilitate the writing of scripts.
> It contained all my boilerplate code for parsing and validating
> command-line options, generating of accessor functions for these
> options, printing of the help message and of the full documentation,
> testing, etc.

A lot of people seem to have gotten hung up on this paragraph.  kj 
remarks in one reply that he *does* use optparse, but there still seems 
to be much confusion.  I've long done something similar, so I'll attempt 
to clarify.  For most of the programming languages that I use, my first 
exercise is to write a program that accepts a 'getopt' string and 
creates a program around it.  Here's a simple version that I just 
whipped out; the version that I commonly use is a bit more complicated.

"""%s [-h] [optionstring]

The program generates boilerplate for a Python script. It accepts a
single argument consisting of a string of option letters that you
wish the script to recognize, with options that require an argument
followed by a colon (i.e., the same format that Unix getopt() uses).

Written by samwyse
Created on Feb 02, 2008
"""

import sys
import getopt

class UserError(Exception):
     def __init__(self, msg):
         self.msg = msg

class Usage(UserError):
     pass

def main(argv=None):
     if argv is None:
         argv = sys.argv
     # parse command line options
     try:
         try:
             opts, args = getopt.getopt(argv[1:], "h")
         except getopt.error, msg:
              raise Usage(msg)
         # process options
         for o, a in opts:
             if o == '-h':
                 print __doc__ % argv[0]
                 return 0
     except UserError, err:
         print >>sys.stderr, err.msg
         print >>sys.stderr, "for help use --help"
         if type(err) is Usage:
             return 2
         else:
             return 1

     # process arguments
     for arg in args:
         # process() is defined elsewhere
         process(arg)

#----#----#----#----#----#----#

def process(shortopts):
     from time import strftime

     prolog = '''\
"""Module docstring.

This serves as a long usage message.

Written by %(author)s
Created on %(date)s
"""

import sys
import getopt

class UserError(Exception):
     def __init__(self, msg):
         self.msg = msg

class Usage(UserError):
     pass

def main(argv=None):
     if argv is None:
         argv = sys.argv
     # parse command line options
     try:
         try:
             opts, args = getopt.getopt(argv[1:], "%(opts)s")
         except getopt.error, msg:
              raise Usage(msg)
         # process options
         for o, a in opts:'''

     has_arg = '''\
             if o == '-%(opt)s':
                 opt_%(opt)s = a'''

     no_arg = '''\
             if o == '-%(opt)s':
                 opt_%(opt)s = True'''

     epilog = '''\
     except UserError, err:
         print >>sys.stderr, err.msg
         print >>sys.stderr, "for help use --help"
         if type(err) is Usage:
             return 2
         else:
             return 1

     # process arguments
     for arg in args:
         # process() is defined elsewhere
         process(arg)

if __name__ == "__main__":
     sys.exit(main())
'''

     values = {
         'date': strftime('%b %d, %Y'),
         'author': 'samwyse',
         'opts': shortopts
         }
     print prolog % values
     for i in range(len(shortopts)):
         c = shortopts[i]
         if c != ':':
             if shortopts.startswith(':', i+1):
                 print has_arg % { 'opt': c }
             else:
                 print no_arg % { 'opt': c }
     print epilog % values

#----#----#----#----#----#----#

if __name__ == "__main__":
     sys.exit(main())



More information about the Python-list mailing list