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 | |
Fred Drake | 0aa811c | 2001-10-20 04:24:09 +0000 | [diff] [blame] | 28 | \note{Unlike GNU \cfunction{getopt()}, after a non-option |
Moshe Zadka | 2bd0d88 | 2001-04-11 07:33:08 +0000 | [diff] [blame] | 29 | argument, all further arguments are considered also non-options. |
Fred Drake | 0aa811c | 2001-10-20 04:24:09 +0000 | [diff] [blame] | 30 | This is similar to the way non-GNU \UNIX{} systems work.} |
Moshe Zadka | 2bd0d88 | 2001-04-11 07:33:08 +0000 | [diff] [blame] | 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, |
Raymond Hettinger | 565ea5a | 2004-10-02 11:02:59 +0000 | [diff] [blame] | 40 | if \var{long_options} is \code{['foo', 'frob']}, the option |
Fred Drake | 45b1d6a | 2001-01-08 16:05:51 +0000 | [diff] [blame] | 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 | |
Martin v. Löwis | 446a25f | 2002-06-06 10:58:36 +0000 | [diff] [blame] | 58 | \begin{funcdesc}{gnu_getopt}{args, options\optional{, long_options}} |
| 59 | This function works like \function{getopt()}, except that GNU style |
| 60 | scanning mode is used by default. This means that option and |
| 61 | non-option arguments may be intermixed. The \function{getopt()} |
| 62 | function stops processing options as soon as a non-option argument is |
| 63 | encountered. |
| 64 | |
| 65 | If the first character of the option string is `+', or if the |
| 66 | environment variable POSIXLY_CORRECT is set, then option processing |
| 67 | stops as soon as a non-option argument is encountered. |
Fred Drake | 40558fe | 2006-01-20 03:30:36 +0000 | [diff] [blame] | 68 | |
| 69 | \versionadded{2.3} |
Martin v. Löwis | 446a25f | 2002-06-06 10:58:36 +0000 | [diff] [blame] | 70 | \end{funcdesc} |
| 71 | |
Fred Drake | 76e6da3 | 1999-12-21 22:50:05 +0000 | [diff] [blame] | 72 | \begin{excdesc}{GetoptError} |
Fred Drake | a44d740 | 1998-03-10 03:36:00 +0000 | [diff] [blame] | 73 | This is raised when an unrecognized option is found in the argument |
| 74 | list or when an option requiring an argument is given none. |
| 75 | The argument to the exception is a string indicating the cause of the |
| 76 | error. For long options, an argument given to an option which does |
Fred Drake | 76e6da3 | 1999-12-21 22:50:05 +0000 | [diff] [blame] | 77 | not require one will also cause this exception to be raised. The |
| 78 | attributes \member{msg} and \member{opt} give the error message and |
| 79 | related option; if there is no specific option to which the exception |
| 80 | relates, \member{opt} is an empty string. |
Fred Drake | 293f77a | 2001-04-18 03:18:57 +0000 | [diff] [blame] | 81 | |
| 82 | \versionchanged[Introduced \exception{GetoptError} as a synonym for |
| 83 | \exception{error}]{1.6} |
Fred Drake | 76e6da3 | 1999-12-21 22:50:05 +0000 | [diff] [blame] | 84 | \end{excdesc} |
| 85 | |
| 86 | \begin{excdesc}{error} |
| 87 | Alias for \exception{GetoptError}; for backward compatibility. |
Fred Drake | a44d740 | 1998-03-10 03:36:00 +0000 | [diff] [blame] | 88 | \end{excdesc} |
| 89 | |
Guido van Rossum | 2f66663 | 1996-09-11 21:26:29 +0000 | [diff] [blame] | 90 | |
| 91 | An example using only \UNIX{} style options: |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 92 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 93 | \begin{verbatim} |
Fred Drake | a8e484c | 2000-08-11 19:55:06 +0000 | [diff] [blame] | 94 | >>> import getopt |
| 95 | >>> args = '-a -b -cfoo -d bar a1 a2'.split() |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 96 | >>> args |
| 97 | ['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2'] |
| 98 | >>> optlist, args = getopt.getopt(args, 'abc:d:') |
| 99 | >>> optlist |
| 100 | [('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')] |
| 101 | >>> args |
| 102 | ['a1', 'a2'] |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 103 | \end{verbatim} |
Fred Drake | a44d740 | 1998-03-10 03:36:00 +0000 | [diff] [blame] | 104 | |
Guido van Rossum | 2f66663 | 1996-09-11 21:26:29 +0000 | [diff] [blame] | 105 | Using long option names is equally easy: |
| 106 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 107 | \begin{verbatim} |
Guido van Rossum | 2f66663 | 1996-09-11 21:26:29 +0000 | [diff] [blame] | 108 | >>> s = '--condition=foo --testing --output-file abc.def -x a1 a2' |
Fred Drake | a8e484c | 2000-08-11 19:55:06 +0000 | [diff] [blame] | 109 | >>> args = s.split() |
Guido van Rossum | 2f66663 | 1996-09-11 21:26:29 +0000 | [diff] [blame] | 110 | >>> args |
| 111 | ['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2'] |
| 112 | >>> optlist, args = getopt.getopt(args, 'x', [ |
| 113 | ... 'condition=', 'output-file=', 'testing']) |
| 114 | >>> optlist |
Fred Drake | a44d740 | 1998-03-10 03:36:00 +0000 | [diff] [blame] | 115 | [('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', |
| 116 | '')] |
Guido van Rossum | 2f66663 | 1996-09-11 21:26:29 +0000 | [diff] [blame] | 117 | >>> args |
| 118 | ['a1', 'a2'] |
Fred Drake | a8e484c | 2000-08-11 19:55:06 +0000 | [diff] [blame] | 119 | \end{verbatim} |
| 120 | |
| 121 | In a script, typical usage is something like this: |
| 122 | |
| 123 | \begin{verbatim} |
| 124 | import getopt, sys |
| 125 | |
| 126 | def main(): |
| 127 | try: |
Raymond Hettinger | 6e887bb | 2003-04-29 04:35:36 +0000 | [diff] [blame] | 128 | opts, args = getopt.getopt(sys.argv[1:], "ho:v", ["help", "output="]) |
Georg Brandl | 3f96902 | 2006-11-23 09:55:07 +0000 | [diff] [blame] | 129 | except getopt.GetoptError, err: |
Fred Drake | a8e484c | 2000-08-11 19:55:06 +0000 | [diff] [blame] | 130 | # print help information and exit: |
Georg Brandl | 3f96902 | 2006-11-23 09:55:07 +0000 | [diff] [blame] | 131 | print str(err) # will print something like "option -a not recognized" |
Fred Drake | a8e484c | 2000-08-11 19:55:06 +0000 | [diff] [blame] | 132 | usage() |
| 133 | sys.exit(2) |
| 134 | output = None |
Raymond Hettinger | 6e887bb | 2003-04-29 04:35:36 +0000 | [diff] [blame] | 135 | verbose = False |
Fred Drake | a8e484c | 2000-08-11 19:55:06 +0000 | [diff] [blame] | 136 | for o, a in opts: |
Raymond Hettinger | 6e887bb | 2003-04-29 04:35:36 +0000 | [diff] [blame] | 137 | if o == "-v": |
| 138 | verbose = True |
Georg Brandl | 3f96902 | 2006-11-23 09:55:07 +0000 | [diff] [blame] | 139 | elif o in ("-h", "--help"): |
Fred Drake | a8e484c | 2000-08-11 19:55:06 +0000 | [diff] [blame] | 140 | usage() |
| 141 | sys.exit() |
Georg Brandl | 3f96902 | 2006-11-23 09:55:07 +0000 | [diff] [blame] | 142 | elif o in ("-o", "--output"): |
Fred Drake | a8e484c | 2000-08-11 19:55:06 +0000 | [diff] [blame] | 143 | output = a |
Georg Brandl | 3f96902 | 2006-11-23 09:55:07 +0000 | [diff] [blame] | 144 | else: |
| 145 | assert False, "unhandled option" |
Fred Drake | a8e484c | 2000-08-11 19:55:06 +0000 | [diff] [blame] | 146 | # ... |
| 147 | |
| 148 | if __name__ == "__main__": |
| 149 | main() |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 150 | \end{verbatim} |
Skip Montanaro | db8d1c2 | 2004-01-26 19:30:21 +0000 | [diff] [blame] | 151 | |
| 152 | \begin{seealso} |
Fred Drake | ee3c607 | 2004-01-26 19:40:18 +0000 | [diff] [blame] | 153 | \seemodule{optparse}{More object-oriented command line option parsing.} |
Skip Montanaro | db8d1c2 | 2004-01-26 19:30:21 +0000 | [diff] [blame] | 154 | \end{seealso} |