I hate the chain rule... Gotta love the quotient rule, product rule and the power rule, tho... Oh, and Maple pown3s


0 comment(s) | link to this entry | edit this entry

Few years back I had read Guido's entry on writing a nice "main" for python. When optparse came out for python 2.3 I had revised it and have been using the exact same code for of main for a long long time now... I find myself copying/pasting the same shit all the time so I thought I'd put it here for copying/pasting. Maybe others would find it interesting?

The code is as follows. You'd need to modify _USAGE, _OPTIONS, run, and perhaps the exception handling when calling run. Other than that you should be able to use this as is.

How do you guys write your main?


###############################################################################
#
###############################################################################
import sys
import traceback
from optparse import OptionParser


###############################################################################
#
###############################################################################
_USAGE = """
%prog param1 param2 --option1=value
"""

_OPTIONS = (
    (("--option1",),
     {"action" : "store",
      "type" : "string",
      "dest" : "option1",
      "help" : "this is an option",
      "default": "default value"
      }),
    )
               

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


###############################################################################
#
###############################################################################
def run(param1, param2, in_repos=None):
    pass
    

###############################################################################
#
###############################################################################
def main(argv=None):
    if argv is None:
        argv = sys.argv

    try:
        parser = OptionParser(usage=_USAGE)

        for (t, d) in _OPTIONS:
            apply(parser.add_option, t, d)
        
        try:
            (opts, args) = parser.parse_args(argv[1:])
        except SystemExit:
            
            raise
        # ---------------------------------------------------------------------
        except:
            traceback.print_exc()
            
            raise Usage("Unhandled exception")
        # ---------------------------------------------------------------------
        else:
            try:

                return apply(run, args, opts.__dict__)
            # -----------------------------------------------------------------
            except SystemExit:

                raise
            # -----------------------------------------------------------------
            except:
                traceback.print_exc()

 
    except Usage, e:
        print >> sys.stderr, e.msg        

        return 2


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


1 comment(s) | link to this entry | edit this entry

Want some more? Dig in to the archive for past entries.