Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 1 | \section{Standard Module \sectcode{getopt}} |
| 2 | |
| 3 | \stmodindex{getopt} |
| 4 | This module helps scripts to parse the command line arguments in |
| 5 | \code{sys.argv}. |
Guido van Rossum | 2f66663 | 1996-09-11 21:26:29 +0000 | [diff] [blame] | 6 | It supports the same conventions as the \UNIX{} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 7 | \code{getopt()} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 8 | function (including the special meanings of arguments of the form |
Guido van Rossum | e8d94a8 | 1997-04-02 06:05:07 +0000 | [diff] [blame] | 9 | `\code{-}' and `\code{-}\code{-}'). |
| 10 | % That's to fool latex2html into leaving the two hyphens alone! |
| 11 | Long options similar to those supported by |
Guido van Rossum | 2f66663 | 1996-09-11 21:26:29 +0000 | [diff] [blame] | 12 | GNU software may be used as well via an optional third argument. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 13 | It defines the function |
Guido van Rossum | 2f66663 | 1996-09-11 21:26:29 +0000 | [diff] [blame] | 14 | \code{getopt.getopt(args, options [, long_options])} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 15 | and the exception |
| 16 | \code{getopt.error}. |
| 17 | |
| 18 | The first argument to |
| 19 | \code{getopt()} |
| 20 | is the argument list passed to the script with its first element |
| 21 | chopped off (i.e., |
| 22 | \code{sys.argv[1:]}). |
| 23 | The second argument is the string of option letters that the |
| 24 | script wants to recognize, with options that require an argument |
| 25 | followed by a colon (i.e., the same format that \UNIX{} |
| 26 | \code{getopt()} |
| 27 | uses). |
Guido van Rossum | 2f66663 | 1996-09-11 21:26:29 +0000 | [diff] [blame] | 28 | The third option, if specified, is a list of strings with the names of |
Guido van Rossum | e8d94a8 | 1997-04-02 06:05:07 +0000 | [diff] [blame] | 29 | the long options which should be supported. The leading \code{'-}\code{-'} |
Guido van Rossum | 2f66663 | 1996-09-11 21:26:29 +0000 | [diff] [blame] | 30 | characters should not be included in the option name. Options which |
| 31 | require an argument should be followed by an equal sign (\code{'='}). |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 32 | The return value consists of two elements: the first is a list of |
| 33 | option-and-value pairs; the second is the list of program arguments |
| 34 | left after the option list was stripped (this is a trailing slice of the |
| 35 | first argument). |
| 36 | Each option-and-value pair returned has the option as its first element, |
| 37 | prefixed with a hyphen (e.g., |
| 38 | \code{'-x'}), |
| 39 | and the option argument as its second element, or an empty string if the |
| 40 | option has no argument. |
| 41 | The options occur in the list in the same order in which they were |
Guido van Rossum | 2f66663 | 1996-09-11 21:26:29 +0000 | [diff] [blame] | 42 | found, thus allowing multiple occurrences. Long and short options may |
| 43 | be mixed. |
| 44 | |
| 45 | An example using only \UNIX{} style options: |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 46 | |
| 47 | \bcode\begin{verbatim} |
| 48 | >>> import getopt, string |
| 49 | >>> args = string.split('-a -b -cfoo -d bar a1 a2') |
| 50 | >>> args |
| 51 | ['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2'] |
| 52 | >>> optlist, args = getopt.getopt(args, 'abc:d:') |
| 53 | >>> optlist |
| 54 | [('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')] |
| 55 | >>> args |
| 56 | ['a1', 'a2'] |
| 57 | >>> |
| 58 | \end{verbatim}\ecode |
| 59 | |
Guido van Rossum | 2f66663 | 1996-09-11 21:26:29 +0000 | [diff] [blame] | 60 | Using long option names is equally easy: |
| 61 | |
| 62 | \bcode\begin{verbatim} |
| 63 | >>> s = '--condition=foo --testing --output-file abc.def -x a1 a2' |
| 64 | >>> args = string.split(s) |
| 65 | >>> args |
| 66 | ['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2'] |
| 67 | >>> optlist, args = getopt.getopt(args, 'x', [ |
| 68 | ... 'condition=', 'output-file=', 'testing']) |
| 69 | >>> optlist |
| 70 | [('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', '')] |
| 71 | >>> args |
| 72 | ['a1', 'a2'] |
| 73 | >>> |
| 74 | \end{verbatim}\ecode |
| 75 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 76 | The exception |
Guido van Rossum | 2f66663 | 1996-09-11 21:26:29 +0000 | [diff] [blame] | 77 | \code{getopt.error = 'getopt.error'} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 78 | is raised when an unrecognized option is found in the argument list or |
| 79 | when an option requiring an argument is given none. |
| 80 | The argument to the exception is a string indicating the cause of the |
Guido van Rossum | 2f66663 | 1996-09-11 21:26:29 +0000 | [diff] [blame] | 81 | error. For long options, an argument given to an option which does |
| 82 | not require one will also cause this exception to be raised. |