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 |
Fred Drake | a8e484c | 2000-08-11 19:55:06 +0000 | [diff] [blame] | 24 | (\character{:}; i.e., the same format that \UNIX{} |
| 25 | \cfunction{getopt()} uses). |
| 26 | |
| 27 | \var{long_options}, if specified, must be a list of strings with the |
| 28 | names of the long options which should be supported. The leading |
Fred Drake | a44d740 | 1998-03-10 03:36:00 +0000 | [diff] [blame] | 29 | \code{'-}\code{-'} characters should not be included in the option |
Fred Drake | a8e484c | 2000-08-11 19:55:06 +0000 | [diff] [blame] | 30 | name. Long options which require an argument should be followed by an |
| 31 | equal sign (\character{=}). |
Fred Drake | a44d740 | 1998-03-10 03:36:00 +0000 | [diff] [blame] | 32 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 33 | 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] | 34 | \code{(\var{option}, \var{value})} pairs; the second is the list of |
| 35 | program arguments left after the option list was stripped (this is a |
Fred Drake | a8e484c | 2000-08-11 19:55:06 +0000 | [diff] [blame] | 36 | trailing slice of \var{args}). Each option-and-value pair returned |
| 37 | has the option as its first element, prefixed with a hyphen for short |
| 38 | options (e.g., \code{'-x'}) or two hyphens for long options (e.g., |
| 39 | \code{'-}\code{-long-option'}), and the option argument as its second |
| 40 | element, or an empty string if the option has no argument. The |
| 41 | options occur in the list in the same order in which they were found, |
| 42 | thus allowing multiple occurrences. Long and short options may be |
| 43 | mixed. |
Fred Drake | a44d740 | 1998-03-10 03:36:00 +0000 | [diff] [blame] | 44 | \end{funcdesc} |
| 45 | |
Fred Drake | 76e6da3 | 1999-12-21 22:50:05 +0000 | [diff] [blame] | 46 | \begin{excdesc}{GetoptError} |
Fred Drake | a44d740 | 1998-03-10 03:36:00 +0000 | [diff] [blame] | 47 | This is raised when an unrecognized option is found in the argument |
| 48 | list or when an option requiring an argument is given none. |
| 49 | The argument to the exception is a string indicating the cause of the |
| 50 | error. For long options, an argument given to an option which does |
Fred Drake | 76e6da3 | 1999-12-21 22:50:05 +0000 | [diff] [blame] | 51 | not require one will also cause this exception to be raised. The |
| 52 | attributes \member{msg} and \member{opt} give the error message and |
| 53 | related option; if there is no specific option to which the exception |
| 54 | relates, \member{opt} is an empty string. |
| 55 | \end{excdesc} |
| 56 | |
| 57 | \begin{excdesc}{error} |
| 58 | Alias for \exception{GetoptError}; for backward compatibility. |
Fred Drake | a44d740 | 1998-03-10 03:36:00 +0000 | [diff] [blame] | 59 | \end{excdesc} |
| 60 | |
Guido van Rossum | 2f66663 | 1996-09-11 21:26:29 +0000 | [diff] [blame] | 61 | |
| 62 | An example using only \UNIX{} style options: |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 63 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 64 | \begin{verbatim} |
Fred Drake | a8e484c | 2000-08-11 19:55:06 +0000 | [diff] [blame] | 65 | >>> import getopt |
| 66 | >>> args = '-a -b -cfoo -d bar a1 a2'.split() |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 67 | >>> args |
| 68 | ['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2'] |
| 69 | >>> optlist, args = getopt.getopt(args, 'abc:d:') |
| 70 | >>> optlist |
| 71 | [('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')] |
| 72 | >>> args |
| 73 | ['a1', 'a2'] |
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' |
Fred Drake | a8e484c | 2000-08-11 19:55:06 +0000 | [diff] [blame] | 80 | >>> args = s.split() |
Guido van Rossum | 2f66663 | 1996-09-11 21:26:29 +0000 | [diff] [blame] | 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'] |
Fred Drake | a8e484c | 2000-08-11 19:55:06 +0000 | [diff] [blame] | 90 | \end{verbatim} |
| 91 | |
| 92 | In a script, typical usage is something like this: |
| 93 | |
| 94 | \begin{verbatim} |
| 95 | import getopt, sys |
| 96 | |
| 97 | def main(): |
| 98 | try: |
| 99 | opts, args = getopt.getopt(sys.argv[1:], "ho:", ["help", "output="]) |
| 100 | except getopt.GetoptError: |
| 101 | # print help information and exit: |
| 102 | usage() |
| 103 | sys.exit(2) |
| 104 | output = None |
| 105 | for o, a in opts: |
| 106 | if o in ("-h", "--help"): |
| 107 | usage() |
| 108 | sys.exit() |
| 109 | if o in ("-o", "--output"): |
| 110 | output = a |
| 111 | # ... |
| 112 | |
| 113 | if __name__ == "__main__": |
| 114 | main() |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 115 | \end{verbatim} |