Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{getopt} --- |
| 2 | Parser for command line options.} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | \declaremodule{standard}{getopt} |
| 4 | |
| 5 | \modulesynopsis{Parser for command line options.} |
| 6 | |
Fred Drake | a44d740 | 1998-03-10 03:36:00 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 8 | This module helps scripts to parse the command line arguments in |
| 9 | \code{sys.argv}. |
Fred Drake | a44d740 | 1998-03-10 03:36:00 +0000 | [diff] [blame] | 10 | It supports the same conventions as the \UNIX{} \cfunction{getopt()} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 11 | function (including the special meanings of arguments of the form |
Guido van Rossum | e8d94a8 | 1997-04-02 06:05:07 +0000 | [diff] [blame] | 12 | `\code{-}' and `\code{-}\code{-}'). |
| 13 | % That's to fool latex2html into leaving the two hyphens alone! |
| 14 | Long options similar to those supported by |
Guido van Rossum | 2f66663 | 1996-09-11 21:26:29 +0000 | [diff] [blame] | 15 | GNU software may be used as well via an optional third argument. |
Fred Drake | a44d740 | 1998-03-10 03:36:00 +0000 | [diff] [blame] | 16 | This module provides a single function and an exception: |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 17 | |
Fred Drake | a44d740 | 1998-03-10 03:36:00 +0000 | [diff] [blame] | 18 | \begin{funcdesc}{getopt}{args, options\optional{, long_options}} |
| 19 | Parses command line options and parameter list. \var{args} is the |
| 20 | argument list to be parsed, without the leading reference to the |
| 21 | running program. Typically, this means \samp{sys.argv[1:]}. |
| 22 | \var{options} is the string of option letters that the script wants to |
| 23 | recognize, with options that require an argument followed by a colon |
| 24 | (i.e., the same format that \UNIX{} \cfunction{getopt()} uses). If |
| 25 | specified, \var{long_options} is a list of strings with the names of |
| 26 | the long options which should be supported. The leading |
| 27 | \code{'-}\code{-'} characters should not be included in the option |
| 28 | name. Options which require an argument should be followed by an |
| 29 | equal sign (\code{'='}). |
| 30 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 31 | 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] | 32 | \code{(\var{option}, \var{value})} pairs; the second is the list of |
| 33 | program arguments left after the option list was stripped (this is a |
| 34 | trailing slice of the first argument). |
| 35 | Each option-and-value pair returned has the option as its first |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 36 | element, prefixed with a hyphen for short options (e.g., \code{'-x'}) |
| 37 | or two hyphens for long options (e.g., \code{'-}\code{-long-option'}), |
| 38 | and the option argument as its second element, or an empty string if |
| 39 | the option has no argument. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 40 | 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] | 41 | found, thus allowing multiple occurrences. Long and short options may |
| 42 | be mixed. |
Fred Drake | a44d740 | 1998-03-10 03:36:00 +0000 | [diff] [blame] | 43 | \end{funcdesc} |
| 44 | |
Fred Drake | 76e6da3 | 1999-12-21 22:50:05 +0000 | [diff] [blame] | 45 | \begin{excdesc}{GetoptError} |
Fred Drake | a44d740 | 1998-03-10 03:36:00 +0000 | [diff] [blame] | 46 | This is raised when an unrecognized option is found in the argument |
| 47 | list or when an option requiring an argument is given none. |
| 48 | The argument to the exception is a string indicating the cause of the |
| 49 | error. For long options, an argument given to an option which does |
Fred Drake | 76e6da3 | 1999-12-21 22:50:05 +0000 | [diff] [blame] | 50 | not require one will also cause this exception to be raised. The |
| 51 | attributes \member{msg} and \member{opt} give the error message and |
| 52 | related option; if there is no specific option to which the exception |
| 53 | relates, \member{opt} is an empty string. |
| 54 | \end{excdesc} |
| 55 | |
| 56 | \begin{excdesc}{error} |
| 57 | Alias for \exception{GetoptError}; for backward compatibility. |
Fred Drake | a44d740 | 1998-03-10 03:36:00 +0000 | [diff] [blame] | 58 | \end{excdesc} |
| 59 | |
Guido van Rossum | 2f66663 | 1996-09-11 21:26:29 +0000 | [diff] [blame] | 60 | |
| 61 | An example using only \UNIX{} style options: |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 62 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 63 | \begin{verbatim} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 64 | >>> import getopt, string |
| 65 | >>> args = string.split('-a -b -cfoo -d bar a1 a2') |
| 66 | >>> args |
| 67 | ['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2'] |
| 68 | >>> optlist, args = getopt.getopt(args, 'abc:d:') |
| 69 | >>> optlist |
| 70 | [('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')] |
| 71 | >>> args |
| 72 | ['a1', 'a2'] |
| 73 | >>> |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 74 | \end{verbatim} |
Fred Drake | a44d740 | 1998-03-10 03:36:00 +0000 | [diff] [blame] | 75 | |
Guido van Rossum | 2f66663 | 1996-09-11 21:26:29 +0000 | [diff] [blame] | 76 | Using long option names is equally easy: |
| 77 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 78 | \begin{verbatim} |
Guido van Rossum | 2f66663 | 1996-09-11 21:26:29 +0000 | [diff] [blame] | 79 | >>> s = '--condition=foo --testing --output-file abc.def -x a1 a2' |
| 80 | >>> args = string.split(s) |
| 81 | >>> args |
| 82 | ['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2'] |
| 83 | >>> optlist, args = getopt.getopt(args, 'x', [ |
| 84 | ... 'condition=', 'output-file=', 'testing']) |
| 85 | >>> optlist |
Fred Drake | a44d740 | 1998-03-10 03:36:00 +0000 | [diff] [blame] | 86 | [('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', |
| 87 | '')] |
Guido van Rossum | 2f66663 | 1996-09-11 21:26:29 +0000 | [diff] [blame] | 88 | >>> args |
| 89 | ['a1', 'a2'] |
| 90 | >>> |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 91 | \end{verbatim} |