home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / p / python / pyhtmldoc / g / getopt < prev    next >
Encoding:
Text File  |  1996-11-14  |  3.4 KB  |  80 lines

  1. <TITLE>getopt -- Python library reference</TITLE>
  2. Next: <A HREF="../t/tempfile" TYPE="Next">tempfile</A>  
  3. Prev: <A HREF="../t/time" TYPE="Prev">time</A>  
  4. Up: <A HREF="../g/generic_operating_system_services" TYPE="Up">Generic Operating System Services</A>  
  5. Top: <A HREF="../t/top" TYPE="Top">Top</A>  
  6. <H1>6.3. Standard Module <CODE>getopt</CODE></H1>
  7. This module helps scripts to parse the command line arguments in
  8. <CODE>sys.argv</CODE>.
  9. It supports the same conventions as the UNIX
  10. <CODE>getopt()</CODE>
  11. function (including the special meanings of arguments of the form
  12. `<SAMP>-</SAMP>' and `<SAMP>--</SAMP>').  Long options similar to those supported by
  13. GNU software may be used as well via an optional third argument.
  14. It defines the function
  15. <CODE>getopt.getopt(args, options [, long_options])</CODE>
  16. and the exception
  17. <CODE>getopt.error</CODE>.
  18. <P>
  19. The first argument to
  20. <CODE>getopt()</CODE>
  21. is the argument list passed to the script with its first element
  22. chopped off (i.e.,
  23. <CODE>sys.argv[1:]</CODE>).
  24. The second argument is the string of option letters that the
  25. script wants to recognize, with options that require an argument
  26. followed by a colon (i.e., the same format that UNIX
  27. <CODE>getopt()</CODE>
  28. uses).
  29. The third option, if specified, is a list of strings with the names of
  30. the long options which should be supported.  The leading <CODE>'--'</CODE>
  31. characters should not be included in the option name.  Options which
  32. require an argument should be followed by an equal sign (<CODE>'='</CODE>).
  33. The return value consists of two elements: the first is a list of
  34. option-and-value pairs; the second is the list of program arguments
  35. left after the option list was stripped (this is a trailing slice of the
  36. first argument).
  37. Each option-and-value pair returned has the option as its first element,
  38. prefixed with a hyphen (e.g.,
  39. <CODE>'-x'</CODE>),
  40. and the option argument as its second element, or an empty string if the
  41. option has no argument.
  42. The options occur in the list in the same order in which they were
  43. found, thus allowing multiple occurrences.  Long and short options may
  44. be mixed.
  45. <P>
  46. An example using only UNIX style options:
  47. <P>
  48. <UL COMPACT><CODE>>>> import getopt, string<P>
  49. >>> args = string.split('-a -b -cfoo -d bar a1 a2')<P>
  50. >>> args<P>
  51. ['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']<P>
  52. >>> optlist, args = getopt.getopt(args, 'abc:d:')<P>
  53. >>> optlist<P>
  54. [('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]<P>
  55. >>> args<P>
  56. ['a1', 'a2']<P>
  57. >>> <P>
  58. </CODE></UL>
  59. Using long option names is equally easy:
  60. <P>
  61. <UL COMPACT><CODE>>>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'<P>
  62. >>> args = string.split(s)<P>
  63. >>> args<P>
  64. ['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']<P>
  65. >>> optlist, args = getopt.getopt(args, 'x', [<P>
  66. ...     'condition=', 'output-file=', 'testing'])<P>
  67. >>> optlist<P>
  68. [('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', '')]<P>
  69. >>> args<P>
  70. ['a1', 'a2']<P>
  71. >>> <P>
  72. </CODE></UL>
  73. The exception
  74. <CODE>getopt.error = 'getopt.error'</CODE>
  75. is raised when an unrecognized option is found in the argument list or
  76. when an option requiring an argument is given none.
  77. The argument to the exception is a string indicating the cause of the
  78. error.  For long options, an argument given to an option which does
  79. not require one will also cause this exception to be raised.
  80.