Differences between revisions 1 and 2
Revision 1 as of 2004-04-05 04:17:44
Size: 320
Editor: dsl254-010-130
Comment:
Revision 2 as of 2004-07-19 08:10:14
Size: 844
Editor: dsl254-010-130
Comment: example OptParse use
Deletions are marked like this. Additions are marked like this.
Line 5: Line 5:
Official Python 2.3 Opt``Parse Documentation: http://www.python.org/doc/current/lib/module-optparse.html For example:

{{{
#!python
import optparse

if __name__=="__main__":
    parser = optparse.OptionParser()
    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()
    hostname = options.hostname
    portnum = options.portnum
}}}

 * [http://www.python.org/doc/current/lib/module-optparse.html Official Python 2.3 OptParse Documentation]

OptParse is a module introduced in Python2.3 that makes it easy to write command line tools.

You give a description of the options that the program can receive, and OptParse will do reasonable stuff for you.

For example:

   1 import optparse
   2 
   3 if __name__=="__main__":
   4     parser = optparse.OptionParser()
   5     parser.add_option( "-H", "--host", dest="hostname", default="127.0.0.1",
   6                        type="string", help = "specify hostname to run on" )
   7     parser.add_option( "-p", "--port", dest="portnum", default=80,
   8                        type="int", help = "port number to run on" )
   9 
  10     (options, args) = parser.parse_args()
  11     hostname = options.hostname
  12     portnum = options.portnum

OptParse (last edited 2019-09-12 18:41:55 by MatsWichmann)

Unable to edit the page? See the FrontPage for instructions.