Guido van Rossum | 4b8c6ea | 2000-02-04 15:39:30 +0000 | [diff] [blame] | 1 | """Parser for command line options. |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | 6d06094 | 1998-11-17 04:16:37 +0000 | [diff] [blame] | 3 | This module helps scripts to parse the command line arguments in |
| 4 | sys.argv. It supports the same conventions as the Unix getopt() |
| 5 | function (including the special meanings of arguments of the form `-' |
| 6 | and `--'). Long options similar to those supported by GNU software |
| 7 | may be used as well via an optional third argument. This module |
Martin v. Löwis | 446a25f | 2002-06-06 10:58:36 +0000 | [diff] [blame] | 8 | provides two functions and an exception: |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | 6d06094 | 1998-11-17 04:16:37 +0000 | [diff] [blame] | 10 | getopt() -- Parse command line options |
Martin v. Löwis | 446a25f | 2002-06-06 10:58:36 +0000 | [diff] [blame] | 11 | gnu_getopt() -- Like getopt(), but allow option and non-option arguments |
Tim Peters | c411dba | 2002-07-16 21:35:23 +0000 | [diff] [blame] | 12 | to be intermixed. |
Guido van Rossum | 80c33e5 | 1999-12-21 22:38:40 +0000 | [diff] [blame] | 13 | GetoptError -- exception (class) raised with 'opt' attribute, which is the |
| 14 | option involved with the exception. |
Guido van Rossum | 6d06094 | 1998-11-17 04:16:37 +0000 | [diff] [blame] | 15 | """ |
Guido van Rossum | 1550ff7 | 1996-09-11 19:43:52 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | 6d06094 | 1998-11-17 04:16:37 +0000 | [diff] [blame] | 17 | # Long option support added by Lars Wirzenius <liw@iki.fi>. |
Martin v. Löwis | 446a25f | 2002-06-06 10:58:36 +0000 | [diff] [blame] | 18 | # |
Fred Drake | a395ced | 2000-02-25 16:14:08 +0000 | [diff] [blame] | 19 | # Gerrit Holl <gerrit@nl.linux.org> moved the string-based exceptions |
| 20 | # to class-based exceptions. |
Martin v. Löwis | 446a25f | 2002-06-06 10:58:36 +0000 | [diff] [blame] | 21 | # |
Éric Araujo | eda5583 | 2011-03-21 00:09:07 +0100 | [diff] [blame] | 22 | # Peter Åstrand <astrand@lysator.liu.se> added gnu_getopt(). |
Martin v. Löwis | 446a25f | 2002-06-06 10:58:36 +0000 | [diff] [blame] | 23 | # |
| 24 | # TODO for gnu_getopt(): |
| 25 | # |
| 26 | # - GNU getopt_long_only mechanism |
| 27 | # - allow the caller to specify ordering |
| 28 | # - RETURN_IN_ORDER option |
| 29 | # - GNU extension with '-' as first character of option string |
| 30 | # - optional arguments, specified by double colons |
| 31 | # - a option string with a W followed by semicolon should |
| 32 | # treat "-W foo" as "--foo" |
Fred Drake | a395ced | 2000-02-25 16:14:08 +0000 | [diff] [blame] | 33 | |
Skip Montanaro | 96803b2 | 2002-06-07 03:26:43 +0000 | [diff] [blame] | 34 | __all__ = ["GetoptError","error","getopt","gnu_getopt"] |
Skip Montanaro | eccd02a | 2001-01-20 23:34:12 +0000 | [diff] [blame] | 35 | |
Martin v. Löwis | 446a25f | 2002-06-06 10:58:36 +0000 | [diff] [blame] | 36 | import os |
Antoine Pitrou | 0fd59ac | 2011-03-21 16:04:06 +0100 | [diff] [blame] | 37 | try: |
| 38 | from gettext import gettext as _ |
| 39 | except ImportError: |
| 40 | # Bootstrapping Python: gettext's dependencies not built yet |
| 41 | def _(s): return s |
Martin v. Löwis | 446a25f | 2002-06-06 10:58:36 +0000 | [diff] [blame] | 42 | |
Guido van Rossum | 80c33e5 | 1999-12-21 22:38:40 +0000 | [diff] [blame] | 43 | class GetoptError(Exception): |
| 44 | opt = '' |
| 45 | msg = '' |
Andrew M. Kuchling | 0189266 | 2003-02-06 19:52:56 +0000 | [diff] [blame] | 46 | def __init__(self, msg, opt=''): |
Fred Drake | e1fd526 | 2001-01-08 15:39:32 +0000 | [diff] [blame] | 47 | self.msg = msg |
| 48 | self.opt = opt |
| 49 | Exception.__init__(self, msg, opt) |
Guido van Rossum | 80c33e5 | 1999-12-21 22:38:40 +0000 | [diff] [blame] | 50 | |
| 51 | def __str__(self): |
| 52 | return self.msg |
| 53 | |
| 54 | error = GetoptError # backward compatibility |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 55 | |
Guido van Rossum | 2c349bb | 1996-09-09 15:48:24 +0000 | [diff] [blame] | 56 | def getopt(args, shortopts, longopts = []): |
Guido van Rossum | 6d06094 | 1998-11-17 04:16:37 +0000 | [diff] [blame] | 57 | """getopt(args, options[, long_options]) -> opts, args |
| 58 | |
| 59 | Parses command line options and parameter list. args is the |
| 60 | argument list to be parsed, without the leading reference to the |
| 61 | running program. Typically, this means "sys.argv[1:]". shortopts |
| 62 | is the string of option letters that the script wants to |
| 63 | recognize, with options that require an argument followed by a |
| 64 | colon (i.e., the same format that Unix getopt() uses). If |
| 65 | specified, longopts is a list of strings with the names of the |
| 66 | long options which should be supported. The leading '--' |
| 67 | characters should not be included in the option name. Options |
| 68 | which require an argument should be followed by an equal sign |
| 69 | ('='). |
| 70 | |
| 71 | The return value consists of two elements: the first is a list of |
| 72 | (option, value) pairs; the second is the list of program arguments |
| 73 | left after the option list was stripped (this is a trailing slice |
| 74 | of the first argument). Each option-and-value pair returned has |
| 75 | the option as its first element, prefixed with a hyphen (e.g., |
| 76 | '-x'), and the option argument as its second element, or an empty |
| 77 | string if the option has no argument. The options occur in the |
| 78 | list in the same order in which they were found, thus allowing |
| 79 | multiple occurrences. Long and short options may be mixed. |
| 80 | |
| 81 | """ |
| 82 | |
| 83 | opts = [] |
| 84 | if type(longopts) == type(""): |
| 85 | longopts = [longopts] |
| 86 | else: |
| 87 | longopts = list(longopts) |
Tim Peters | dd699b6 | 2000-12-27 08:05:05 +0000 | [diff] [blame] | 88 | while args and args[0].startswith('-') and args[0] != '-': |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 89 | if args[0] == '--': |
| 90 | args = args[1:] |
| 91 | break |
Fred Drake | 1e7dfd3 | 2001-12-12 06:20:34 +0000 | [diff] [blame] | 92 | if args[0].startswith('--'): |
Guido van Rossum | 6d06094 | 1998-11-17 04:16:37 +0000 | [diff] [blame] | 93 | opts, args = do_longs(opts, args[0][2:], longopts, args[1:]) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 94 | else: |
Guido van Rossum | 6d06094 | 1998-11-17 04:16:37 +0000 | [diff] [blame] | 95 | opts, args = do_shorts(opts, args[0][1:], shortopts, args[1:]) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 96 | |
Guido van Rossum | 6d06094 | 1998-11-17 04:16:37 +0000 | [diff] [blame] | 97 | return opts, args |
Guido van Rossum | 2c349bb | 1996-09-09 15:48:24 +0000 | [diff] [blame] | 98 | |
Martin v. Löwis | 446a25f | 2002-06-06 10:58:36 +0000 | [diff] [blame] | 99 | def gnu_getopt(args, shortopts, longopts = []): |
| 100 | """getopt(args, options[, long_options]) -> opts, args |
| 101 | |
| 102 | This function works like getopt(), except that GNU style scanning |
| 103 | mode is used by default. This means that option and non-option |
| 104 | arguments may be intermixed. The getopt() function stops |
| 105 | processing options as soon as a non-option argument is |
| 106 | encountered. |
| 107 | |
| 108 | If the first character of the option string is `+', or if the |
| 109 | environment variable POSIXLY_CORRECT is set, then option |
| 110 | processing stops as soon as a non-option argument is encountered. |
Tim Peters | c411dba | 2002-07-16 21:35:23 +0000 | [diff] [blame] | 111 | |
Martin v. Löwis | 446a25f | 2002-06-06 10:58:36 +0000 | [diff] [blame] | 112 | """ |
| 113 | |
| 114 | opts = [] |
| 115 | prog_args = [] |
Martin v. Löwis | 33b77de | 2002-06-06 18:14:50 +0000 | [diff] [blame] | 116 | if isinstance(longopts, str): |
Martin v. Löwis | 446a25f | 2002-06-06 10:58:36 +0000 | [diff] [blame] | 117 | longopts = [longopts] |
| 118 | else: |
| 119 | longopts = list(longopts) |
| 120 | |
| 121 | # Allow options after non-option arguments? |
| 122 | if shortopts.startswith('+'): |
| 123 | shortopts = shortopts[1:] |
Martin v. Löwis | 33b77de | 2002-06-06 18:14:50 +0000 | [diff] [blame] | 124 | all_options_first = True |
Jack Jansen | f03c692 | 2002-07-26 11:34:49 +0000 | [diff] [blame] | 125 | elif os.environ.get("POSIXLY_CORRECT"): |
Martin v. Löwis | 33b77de | 2002-06-06 18:14:50 +0000 | [diff] [blame] | 126 | all_options_first = True |
Martin v. Löwis | 446a25f | 2002-06-06 10:58:36 +0000 | [diff] [blame] | 127 | else: |
Martin v. Löwis | 33b77de | 2002-06-06 18:14:50 +0000 | [diff] [blame] | 128 | all_options_first = False |
Martin v. Löwis | 446a25f | 2002-06-06 10:58:36 +0000 | [diff] [blame] | 129 | |
| 130 | while args: |
| 131 | if args[0] == '--': |
| 132 | prog_args += args[1:] |
| 133 | break |
| 134 | |
| 135 | if args[0][:2] == '--': |
| 136 | opts, args = do_longs(opts, args[0][2:], longopts, args[1:]) |
Georg Brandl | eee3116 | 2008-12-07 15:15:22 +0000 | [diff] [blame] | 137 | elif args[0][:1] == '-' and args[0] != '-': |
Martin v. Löwis | 446a25f | 2002-06-06 10:58:36 +0000 | [diff] [blame] | 138 | opts, args = do_shorts(opts, args[0][1:], shortopts, args[1:]) |
| 139 | else: |
| 140 | if all_options_first: |
| 141 | prog_args += args |
| 142 | break |
| 143 | else: |
| 144 | prog_args.append(args[0]) |
| 145 | args = args[1:] |
| 146 | |
| 147 | return opts, prog_args |
| 148 | |
Guido van Rossum | 6d06094 | 1998-11-17 04:16:37 +0000 | [diff] [blame] | 149 | def do_longs(opts, opt, longopts, args): |
Guido van Rossum | 1550ff7 | 1996-09-11 19:43:52 +0000 | [diff] [blame] | 150 | try: |
Fred Drake | a395ced | 2000-02-25 16:14:08 +0000 | [diff] [blame] | 151 | i = opt.index('=') |
Guido van Rossum | 1550ff7 | 1996-09-11 19:43:52 +0000 | [diff] [blame] | 152 | except ValueError: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 153 | optarg = None |
Tim Peters | dd699b6 | 2000-12-27 08:05:05 +0000 | [diff] [blame] | 154 | else: |
| 155 | opt, optarg = opt[:i], opt[i+1:] |
Guido van Rossum | 2c349bb | 1996-09-09 15:48:24 +0000 | [diff] [blame] | 156 | |
Guido van Rossum | 1550ff7 | 1996-09-11 19:43:52 +0000 | [diff] [blame] | 157 | has_arg, opt = long_has_args(opt, longopts) |
| 158 | if has_arg: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 159 | if optarg is None: |
| 160 | if not args: |
Éric Araujo | eda5583 | 2011-03-21 00:09:07 +0100 | [diff] [blame] | 161 | raise GetoptError(_('option --%s requires argument') % opt, opt) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 162 | optarg, args = args[0], args[1:] |
Victor Stinner | eccc5fa | 2010-07-24 00:49:20 +0000 | [diff] [blame] | 163 | elif optarg is not None: |
Éric Araujo | eda5583 | 2011-03-21 00:09:07 +0100 | [diff] [blame] | 164 | raise GetoptError(_('option --%s must not have an argument') % opt, opt) |
Guido van Rossum | 6d06094 | 1998-11-17 04:16:37 +0000 | [diff] [blame] | 165 | opts.append(('--' + opt, optarg or '')) |
| 166 | return opts, args |
Guido van Rossum | 2c349bb | 1996-09-09 15:48:24 +0000 | [diff] [blame] | 167 | |
| 168 | # Return: |
| 169 | # has_arg? |
| 170 | # full option name |
| 171 | def long_has_args(opt, longopts): |
Tim Peters | d31b632 | 2000-12-29 02:17:56 +0000 | [diff] [blame] | 172 | possibilities = [o for o in longopts if o.startswith(opt)] |
| 173 | if not possibilities: |
Éric Araujo | eda5583 | 2011-03-21 00:09:07 +0100 | [diff] [blame] | 174 | raise GetoptError(_('option --%s not recognized') % opt, opt) |
Tim Peters | dd699b6 | 2000-12-27 08:05:05 +0000 | [diff] [blame] | 175 | # Is there an exact match? |
| 176 | if opt in possibilities: |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 177 | return False, opt |
Tim Peters | dd699b6 | 2000-12-27 08:05:05 +0000 | [diff] [blame] | 178 | elif opt + '=' in possibilities: |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 179 | return True, opt |
Tim Peters | dd699b6 | 2000-12-27 08:05:05 +0000 | [diff] [blame] | 180 | # No exact match, so better be unique. |
| 181 | if len(possibilities) > 1: |
| 182 | # XXX since possibilities contains all valid continuations, might be |
| 183 | # nice to work them into the error msg |
Éric Araujo | eda5583 | 2011-03-21 00:09:07 +0100 | [diff] [blame] | 184 | raise GetoptError(_('option --%s not a unique prefix') % opt, opt) |
Tim Peters | dd699b6 | 2000-12-27 08:05:05 +0000 | [diff] [blame] | 185 | assert len(possibilities) == 1 |
| 186 | unique_match = possibilities[0] |
| 187 | has_arg = unique_match.endswith('=') |
| 188 | if has_arg: |
| 189 | unique_match = unique_match[:-1] |
| 190 | return has_arg, unique_match |
Guido van Rossum | 2c349bb | 1996-09-09 15:48:24 +0000 | [diff] [blame] | 191 | |
Guido van Rossum | 6d06094 | 1998-11-17 04:16:37 +0000 | [diff] [blame] | 192 | def do_shorts(opts, optstring, shortopts, args): |
Guido van Rossum | 1550ff7 | 1996-09-11 19:43:52 +0000 | [diff] [blame] | 193 | while optstring != '': |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 194 | opt, optstring = optstring[0], optstring[1:] |
| 195 | if short_has_arg(opt, shortopts): |
| 196 | if optstring == '': |
| 197 | if not args: |
Éric Araujo | eda5583 | 2011-03-21 00:09:07 +0100 | [diff] [blame] | 198 | raise GetoptError(_('option -%s requires argument') % opt, |
Fred Drake | 1e7dfd3 | 2001-12-12 06:20:34 +0000 | [diff] [blame] | 199 | opt) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 200 | optstring, args = args[0], args[1:] |
| 201 | optarg, optstring = optstring, '' |
| 202 | else: |
| 203 | optarg = '' |
Guido van Rossum | 6d06094 | 1998-11-17 04:16:37 +0000 | [diff] [blame] | 204 | opts.append(('-' + opt, optarg)) |
| 205 | return opts, args |
Guido van Rossum | 2c349bb | 1996-09-09 15:48:24 +0000 | [diff] [blame] | 206 | |
| 207 | def short_has_arg(opt, shortopts): |
Guido van Rossum | 1550ff7 | 1996-09-11 19:43:52 +0000 | [diff] [blame] | 208 | for i in range(len(shortopts)): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 209 | if opt == shortopts[i] != ':': |
Fred Drake | 1e7dfd3 | 2001-12-12 06:20:34 +0000 | [diff] [blame] | 210 | return shortopts.startswith(':', i+1) |
Éric Araujo | eda5583 | 2011-03-21 00:09:07 +0100 | [diff] [blame] | 211 | raise GetoptError(_('option -%s not recognized') % opt, opt) |
Guido van Rossum | 2c349bb | 1996-09-09 15:48:24 +0000 | [diff] [blame] | 212 | |
| 213 | if __name__ == '__main__': |
Guido van Rossum | 1550ff7 | 1996-09-11 19:43:52 +0000 | [diff] [blame] | 214 | import sys |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 215 | print(getopt(sys.argv[1:], "a:b", ["alpha=", "beta"])) |