OptParse is a module introduced in Python2.3 that makes it easy to write command line tools. See "[[Option parsing tools]]" for others. Note that OptParse is considered deprecated (in Python 2.7 and 3.2) in favor of the [[http://docs.python.org/3/library/argparse.html|argparse]] module. You give a description of the options that the program can receive, and OptParse will do reasonable stuff for you. For example: {{{ #!python import optparse if __name__=="__main__": parser = optparse.OptionParser("usage: %prog [options] arg1 arg2") parser.add_option("-H", "--host", dest="hostname", default="127.0.0.1", type="string", help="specify hostname to run on") parser.add_option("-p", "--port", dest="portnum", default=80, type="int", help="port number to run on") (options, args) = parser.parse_args() if len(args) != 2: parser.error("incorrect number of arguments") hostname = options.hostname portnum = options.portnum }}} ''args'' contains your fixed arguments, ''options'' contains your values. For example, {{{options.portnum}}} would contain the integer {{{80}}}, in the example above. == References == * [[http://docs.python.org/lib/module-optparse.html|Official Python optparse Documentation]] == Complaints == optparse does not support 'required' arguments. The documentation justifies this by saying 'options are optional'. But look at python and its use of keyword arguments. And look at optparse iself! It has the required option 'action'! optparse is a utility! Not a way to enforce a philosphy. See [[http://mail.python.org/pipermail/getopt-sig/2002-February/000016.html|this thread]] in the retired [[http://mail.python.org/mailman/listinfo/getopt-sig|getopt-sig]] mailing list. I also seem to remember a more protracted discussion about "required" arguments/options. ''-- David Boddie''