Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{getopt} --- |
Fred Drake | f8ca7d8 | 2000-10-10 17:03:45 +0000 | [diff] [blame] | 2 | Parser for command line options} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | |
Fred Drake | f8ca7d8 | 2000-10-10 17:03:45 +0000 | [diff] [blame] | 4 | \declaremodule{standard}{getopt} |
| 5 | \modulesynopsis{Portable parser for command line options; support both |
| 6 | short and long option names.} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 7 | |
Fred Drake | a44d740 | 1998-03-10 03:36:00 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 9 | This module helps scripts to parse the command line arguments in |
| 10 | \code{sys.argv}. |
Fred Drake | a44d740 | 1998-03-10 03:36:00 +0000 | [diff] [blame] | 11 | It supports the same conventions as the \UNIX{} \cfunction{getopt()} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 12 | function (including the special meanings of arguments of the form |
Guido van Rossum | e8d94a8 | 1997-04-02 06:05:07 +0000 | [diff] [blame] | 13 | `\code{-}' and `\code{-}\code{-}'). |
| 14 | % That's to fool latex2html into leaving the two hyphens alone! |
| 15 | Long options similar to those supported by |
Guido van Rossum | 2f66663 | 1996-09-11 21:26:29 +0000 | [diff] [blame] | 16 | GNU software may be used as well via an optional third argument. |
Fred Drake | a44d740 | 1998-03-10 03:36:00 +0000 | [diff] [blame] | 17 | This module provides a single function and an exception: |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 18 | |
Fred Drake | a44d740 | 1998-03-10 03:36:00 +0000 | [diff] [blame] | 19 | \begin{funcdesc}{getopt}{args, options\optional{, long_options}} |
| 20 | Parses command line options and parameter list. \var{args} is the |
| 21 | argument list to be parsed, without the leading reference to the |
| 22 | running program. Typically, this means \samp{sys.argv[1:]}. |
| 23 | \var{options} is the string of option letters that the script wants to |
| 24 | recognize, with options that require an argument followed by a colon |
Fred Drake | a8e484c | 2000-08-11 19:55:06 +0000 | [diff] [blame] | 25 | (\character{:}; i.e., the same format that \UNIX{} |
| 26 | \cfunction{getopt()} uses). |
| 27 | |
Moshe Zadka | 2bd0d88 | 2001-04-11 07:33:08 +0000 | [diff] [blame] | 28 | \strong{Note:} Unlike GNU \cfunction{getopt()}, after a non-option |
| 29 | argument, all further arguments are considered also non-options. |
| 30 | This is similar to the way non-GNU \UNIX{} systems work. |
| 31 | |
Fred Drake | a8e484c | 2000-08-11 19:55:06 +0000 | [diff] [blame] | 32 | \var{long_options}, if specified, must be a list of strings with the |
| 33 | names of the long options which should be supported. The leading |
Fred Drake | a44d740 | 1998-03-10 03:36:00 +0000 | [diff] [blame] | 34 | \code{'-}\code{-'} characters should not be included in the option |
Fred Drake | a8e484c | 2000-08-11 19:55:06 +0000 | [diff] [blame] | 35 | name. Long options which require an argument should be followed by an |
Fred Drake | 45b1d6a | 2001-01-08 16:05:51 +0000 | [diff] [blame] | 36 | equal sign (\character{=}). To accept only long options, |
| 37 | \var{options} should be an empty string. Long options on the command |
| 38 | line can be recognized so long as they provide a prefix of the option |
| 39 | name that matches exactly one of the accepted options. For example, |
| 40 | it \var{long_options} is \code{['foo', 'frob']}, the option |
| 41 | \longprogramopt{fo} will match as \longprogramopt{foo}, but |
| 42 | \longprogramopt{f} will not match uniquely, so \exception{GetoptError} |
| 43 | will be raised. |
Fred Drake | a44d740 | 1998-03-10 03:36:00 +0000 | [diff] [blame] | 44 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 45 | 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] | 46 | \code{(\var{option}, \var{value})} pairs; the second is the list of |
| 47 | program arguments left after the option list was stripped (this is a |
Fred Drake | a8e484c | 2000-08-11 19:55:06 +0000 | [diff] [blame] | 48 | trailing slice of \var{args}). Each option-and-value pair returned |
| 49 | has the option as its first element, prefixed with a hyphen for short |
| 50 | options (e.g., \code{'-x'}) or two hyphens for long options (e.g., |
| 51 | \code{'-}\code{-long-option'}), and the option argument as its second |
| 52 | element, or an empty string if the option has no argument. The |
| 53 | options occur in the list in the same order in which they were found, |
| 54 | thus allowing multiple occurrences. Long and short options may be |
| 55 | mixed. |
Fred Drake | a44d740 | 1998-03-10 03:36:00 +0000 | [diff] [blame] | 56 | \end{funcdesc} |
| 57 | |
Fred Drake | 76e6da3 | 1999-12-21 22:50:05 +0000 | [diff] [blame] | 58 | \begin{excdesc}{GetoptError} |
Fred Drake | a44d740 | 1998-03-10 03:36:00 +0000 | [diff] [blame] | 59 | This is raised when an unrecognized option is found in the argument |
| 60 | list or when an option requiring an argument is given none. |
| 61 | The argument to the exception is a string indicating the cause of the |
| 62 | error. For long options, an argument given to an option which does |
Fred Drake | 76e6da3 | 1999-12-21 22:50:05 +0000 | [diff] [blame] | 63 | not require one will also cause this exception to be raised. The |
| 64 | attributes \member{msg} and \member{opt} give the error message and |
| 65 | related option; if there is no specific option to which the exception |
| 66 | relates, \member{opt} is an empty string. |
Fred Drake | 293f77a | 2001-04-18 03:18:57 +0000 | [diff] [blame] | 67 | |
| 68 | \versionchanged[Introduced \exception{GetoptError} as a synonym for |
| 69 | \exception{error}]{1.6} |
Fred Drake | 76e6da3 | 1999-12-21 22:50:05 +0000 | [diff] [blame] | 70 | \end{excdesc} |
| 71 | |
| 72 | \begin{excdesc}{error} |
| 73 | Alias for \exception{GetoptError}; for backward compatibility. |
Fred Drake | a44d740 | 1998-03-10 03:36:00 +0000 | [diff] [blame] | 74 | \end{excdesc} |
| 75 | |
Guido van Rossum | 2f66663 | 1996-09-11 21:26:29 +0000 | [diff] [blame] | 76 | |
| 77 | An example using only \UNIX{} style options: |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 78 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 79 | \begin{verbatim} |
Fred Drake | a8e484c | 2000-08-11 19:55:06 +0000 | [diff] [blame] | 80 | >>> import getopt |
| 81 | >>> args = '-a -b -cfoo -d bar a1 a2'.split() |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 82 | >>> args |
| 83 | ['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2'] |
| 84 | >>> optlist, args = getopt.getopt(args, 'abc:d:') |
| 85 | >>> optlist |
| 86 | [('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')] |
| 87 | >>> args |
| 88 | ['a1', 'a2'] |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 89 | \end{verbatim} |
Fred Drake | a44d740 | 1998-03-10 03:36:00 +0000 | [diff] [blame] | 90 | |
Guido van Rossum | 2f66663 | 1996-09-11 21:26:29 +0000 | [diff] [blame] | 91 | Using long option names is equally easy: |
| 92 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 93 | \begin{verbatim} |
Guido van Rossum | 2f66663 | 1996-09-11 21:26:29 +0000 | [diff] [blame] | 94 | >>> s = '--condition=foo --testing --output-file abc.def -x a1 a2' |
Fred Drake | a8e484c | 2000-08-11 19:55:06 +0000 | [diff] [blame] | 95 | >>> args = s.split() |
Guido van Rossum | 2f66663 | 1996-09-11 21:26:29 +0000 | [diff] [blame] | 96 | >>> args |
| 97 | ['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2'] |
| 98 | >>> optlist, args = getopt.getopt(args, 'x', [ |
| 99 | ... 'condition=', 'output-file=', 'testing']) |
| 100 | >>> optlist |
Fred Drake | a44d740 | 1998-03-10 03:36:00 +0000 | [diff] [blame] | 101 | [('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', |
| 102 | '')] |
Guido van Rossum | 2f66663 | 1996-09-11 21:26:29 +0000 | [diff] [blame] | 103 | >>> args |
| 104 | ['a1', 'a2'] |
Fred Drake | a8e484c | 2000-08-11 19:55:06 +0000 | [diff] [blame] | 105 | \end{verbatim} |
| 106 | |
| 107 | In a script, typical usage is something like this: |
| 108 | |
| 109 | \begin{verbatim} |
| 110 | import getopt, sys |
| 111 | |
| 112 | def main(): |
| 113 | try: |
| 114 | opts, args = getopt.getopt(sys.argv[1:], "ho:", ["help", "output="]) |
| 115 | except getopt.GetoptError: |
| 116 | # print help information and exit: |
| 117 | usage() |
| 118 | sys.exit(2) |
| 119 | output = None |
| 120 | for o, a in opts: |
| 121 | if o in ("-h", "--help"): |
| 122 | usage() |
| 123 | sys.exit() |
| 124 | if o in ("-o", "--output"): |
| 125 | output = a |
| 126 | # ... |
| 127 | |
| 128 | if __name__ == "__main__": |
| 129 | main() |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 130 | \end{verbatim} |