Differences between revisions 8 and 17 (spanning 9 versions)
Revision 8 as of 2005-11-28 23:13:28
Size: 1964
Editor: modemcable143
Comment:
Revision 17 as of 2019-09-12 18:41:55
Size: 1977
Editor: MatsWichmann
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
OptParse is a module introduced in Python2.3 that makes it easy to write command line tools. 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.
Line 32: Line 33:
 * [http://docs.python.org/lib/module-optparse.html Official Python optparse Documentation]  * [[http://docs.python.org/lib/module-optparse.html|Official Python optparse Documentation]]
Line 35: Line 36:
== Discussion == == Complaints ==
Line 37: Line 38:
* Impossible to verify the amount of options passed.  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.
Line 39: Line 44:
I have had to create a function to verify in certain circumstances if we have at least one option set:

(takes an OptParse Values instances returned by parser.parse_args() as argument)
{{{
#!python
def is_empty(options):
    """
    Returns True or False if an option is set or not.
    """
    values = options.__dict__.values()
    return (values == [None] * len(values))
}}}

Is my need for this is due to a bad design in my option parsing?
 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''

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 argparse module.

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("usage: %prog [options] arg1 arg2")
   5     parser.add_option("-H", "--host", dest="hostname",
   6                       default="127.0.0.1", type="string",
   7                       help="specify hostname to run on")
   8     parser.add_option("-p", "--port", dest="portnum", default=80,
   9                       type="int", help="port number to run on")
  10 
  11     (options, args) = parser.parse_args()
  12     if len(args) != 2:
  13         parser.error("incorrect number of arguments")
  14     hostname = options.hostname
  15     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

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 this thread in the retired getopt-sig mailing list. I also seem to remember a more protracted discussion about "required" arguments/options. -- David Boddie

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

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