Fred Drake | 3a0351c | 1998-04-04 07:23:21 +0000 | [diff] [blame^] | 1 | \section{Standard Module \module{getopt}} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 2 | \label{module-getopt} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 3 | \stmodindex{getopt} |
Fred Drake | a44d740 | 1998-03-10 03:36:00 +0000 | [diff] [blame] | 4 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 5 | This module helps scripts to parse the command line arguments in |
| 6 | \code{sys.argv}. |
Fred Drake | a44d740 | 1998-03-10 03:36:00 +0000 | [diff] [blame] | 7 | It supports the same conventions as the \UNIX{} \cfunction{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. |
Fred Drake | a44d740 | 1998-03-10 03:36:00 +0000 | [diff] [blame] | 13 | This module provides a single function and an exception: |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 14 | |
Fred Drake | a44d740 | 1998-03-10 03:36:00 +0000 | [diff] [blame] | 15 | \begin{funcdesc}{getopt}{args, options\optional{, long_options}} |
| 16 | Parses command line options and parameter list. \var{args} is the |
| 17 | argument list to be parsed, without the leading reference to the |
| 18 | running program. Typically, this means \samp{sys.argv[1:]}. |
| 19 | \var{options} is the string of option letters that the script wants to |
| 20 | recognize, with options that require an argument followed by a colon |
| 21 | (i.e., the same format that \UNIX{} \cfunction{getopt()} uses). If |
| 22 | specified, \var{long_options} is a list of strings with the names of |
| 23 | the long options which should be supported. The leading |
| 24 | \code{'-}\code{-'} characters should not be included in the option |
| 25 | name. Options which require an argument should be followed by an |
| 26 | equal sign (\code{'='}). |
| 27 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 28 | The return value consists of two elements: the first is a list of |
Fred Drake | a44d740 | 1998-03-10 03:36:00 +0000 | [diff] [blame] | 29 | \code{(\var{option}, \var{value})} pairs; the second is the list of |
| 30 | program arguments left after the option list was stripped (this is a |
| 31 | trailing slice of the first argument). |
| 32 | Each option-and-value pair returned has the option as its first |
| 33 | element, prefixed with a hyphen (e.g., \code{'-x'}), and the option |
| 34 | argument as its second element, or an empty string if the option has |
| 35 | no argument. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 36 | 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] | 37 | found, thus allowing multiple occurrences. Long and short options may |
| 38 | be mixed. |
Fred Drake | a44d740 | 1998-03-10 03:36:00 +0000 | [diff] [blame] | 39 | \end{funcdesc} |
| 40 | |
| 41 | \begin{excdesc}{error} |
| 42 | This is raised when an unrecognized option is found in the argument |
| 43 | list or when an option requiring an argument is given none. |
| 44 | The argument to the exception is a string indicating the cause of the |
| 45 | error. For long options, an argument given to an option which does |
| 46 | not require one will also cause this exception to be raised. |
| 47 | \end{excdesc} |
| 48 | |
Guido van Rossum | 2f66663 | 1996-09-11 21:26:29 +0000 | [diff] [blame] | 49 | |
| 50 | An example using only \UNIX{} style options: |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 51 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 52 | \begin{verbatim} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 53 | >>> import getopt, string |
| 54 | >>> args = string.split('-a -b -cfoo -d bar a1 a2') |
| 55 | >>> args |
| 56 | ['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2'] |
| 57 | >>> optlist, args = getopt.getopt(args, 'abc:d:') |
| 58 | >>> optlist |
| 59 | [('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')] |
| 60 | >>> args |
| 61 | ['a1', 'a2'] |
| 62 | >>> |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 63 | \end{verbatim} |
Fred Drake | a44d740 | 1998-03-10 03:36:00 +0000 | [diff] [blame] | 64 | |
Guido van Rossum | 2f66663 | 1996-09-11 21:26:29 +0000 | [diff] [blame] | 65 | Using long option names is equally easy: |
| 66 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 67 | \begin{verbatim} |
Guido van Rossum | 2f66663 | 1996-09-11 21:26:29 +0000 | [diff] [blame] | 68 | >>> s = '--condition=foo --testing --output-file abc.def -x a1 a2' |
| 69 | >>> args = string.split(s) |
| 70 | >>> args |
| 71 | ['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2'] |
| 72 | >>> optlist, args = getopt.getopt(args, 'x', [ |
| 73 | ... 'condition=', 'output-file=', 'testing']) |
| 74 | >>> optlist |
Fred Drake | a44d740 | 1998-03-10 03:36:00 +0000 | [diff] [blame] | 75 | [('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', |
| 76 | '')] |
Guido van Rossum | 2f66663 | 1996-09-11 21:26:29 +0000 | [diff] [blame] | 77 | >>> args |
| 78 | ['a1', 'a2'] |
| 79 | >>> |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 80 | \end{verbatim} |