Guido van Rossum | 6d06094 | 1998-11-17 04:16:37 +0000 | [diff] [blame] | 1 | """Module getopt -- 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 |
| 8 | provides a single function and an exception: |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | 80c33e5 | 1999-12-21 22:38:40 +0000 | [diff] [blame] | 10 | Gerrit Holl <gerrit@nl.linux.org> moved the string-based exceptions |
| 11 | to class-based exceptions. |
| 12 | |
Guido van Rossum | 6d06094 | 1998-11-17 04:16:37 +0000 | [diff] [blame] | 13 | getopt() -- Parse command line options |
Guido van Rossum | 80c33e5 | 1999-12-21 22:38:40 +0000 | [diff] [blame] | 14 | GetoptError -- exception (class) raised with 'opt' attribute, which is the |
| 15 | option involved with the exception. |
Guido van Rossum | 6d06094 | 1998-11-17 04:16:37 +0000 | [diff] [blame] | 16 | """ |
Guido van Rossum | 1550ff7 | 1996-09-11 19:43:52 +0000 | [diff] [blame] | 17 | |
Guido van Rossum | 6d06094 | 1998-11-17 04:16:37 +0000 | [diff] [blame] | 18 | # Long option support added by Lars Wirzenius <liw@iki.fi>. |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 19 | |
Guido van Rossum | 2c349bb | 1996-09-09 15:48:24 +0000 | [diff] [blame] | 20 | import string |
| 21 | |
Guido van Rossum | 80c33e5 | 1999-12-21 22:38:40 +0000 | [diff] [blame] | 22 | class GetoptError(Exception): |
| 23 | opt = '' |
| 24 | msg = '' |
| 25 | def __init__(self, *args): |
| 26 | self.args = args |
| 27 | if len(args) == 1: |
| 28 | self.msg = args[0] |
| 29 | elif len(args) == 2: |
| 30 | self.msg = args[0] |
| 31 | self.opt = args[1] |
| 32 | |
| 33 | def __str__(self): |
| 34 | return self.msg |
| 35 | |
| 36 | error = GetoptError # backward compatibility |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 37 | |
Guido van Rossum | 2c349bb | 1996-09-09 15:48:24 +0000 | [diff] [blame] | 38 | def getopt(args, shortopts, longopts = []): |
Guido van Rossum | 6d06094 | 1998-11-17 04:16:37 +0000 | [diff] [blame] | 39 | """getopt(args, options[, long_options]) -> opts, args |
| 40 | |
| 41 | Parses command line options and parameter list. args is the |
| 42 | argument list to be parsed, without the leading reference to the |
| 43 | running program. Typically, this means "sys.argv[1:]". shortopts |
| 44 | is the string of option letters that the script wants to |
| 45 | recognize, with options that require an argument followed by a |
| 46 | colon (i.e., the same format that Unix getopt() uses). If |
| 47 | specified, longopts is a list of strings with the names of the |
| 48 | long options which should be supported. The leading '--' |
| 49 | characters should not be included in the option name. Options |
| 50 | which require an argument should be followed by an equal sign |
| 51 | ('='). |
| 52 | |
| 53 | The return value consists of two elements: the first is a list of |
| 54 | (option, value) pairs; the second is the list of program arguments |
| 55 | left after the option list was stripped (this is a trailing slice |
| 56 | of the first argument). Each option-and-value pair returned has |
| 57 | the option as its first element, prefixed with a hyphen (e.g., |
| 58 | '-x'), and the option argument as its second element, or an empty |
| 59 | string if the option has no argument. The options occur in the |
| 60 | list in the same order in which they were found, thus allowing |
| 61 | multiple occurrences. Long and short options may be mixed. |
| 62 | |
| 63 | """ |
| 64 | |
| 65 | opts = [] |
| 66 | if type(longopts) == type(""): |
| 67 | longopts = [longopts] |
| 68 | else: |
| 69 | longopts = list(longopts) |
Guido van Rossum | 1550ff7 | 1996-09-11 19:43:52 +0000 | [diff] [blame] | 70 | longopts.sort() |
| 71 | while args and args[0][:1] == '-' and args[0] != '-': |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 72 | if args[0] == '--': |
| 73 | args = args[1:] |
| 74 | break |
| 75 | if args[0][:2] == '--': |
Guido van Rossum | 6d06094 | 1998-11-17 04:16:37 +0000 | [diff] [blame] | 76 | opts, args = do_longs(opts, args[0][2:], longopts, args[1:]) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 77 | else: |
Guido van Rossum | 6d06094 | 1998-11-17 04:16:37 +0000 | [diff] [blame] | 78 | opts, args = do_shorts(opts, args[0][1:], shortopts, args[1:]) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 79 | |
Guido van Rossum | 6d06094 | 1998-11-17 04:16:37 +0000 | [diff] [blame] | 80 | return opts, args |
Guido van Rossum | 2c349bb | 1996-09-09 15:48:24 +0000 | [diff] [blame] | 81 | |
Guido van Rossum | 6d06094 | 1998-11-17 04:16:37 +0000 | [diff] [blame] | 82 | def do_longs(opts, opt, longopts, args): |
Guido van Rossum | 1550ff7 | 1996-09-11 19:43:52 +0000 | [diff] [blame] | 83 | try: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 84 | i = string.index(opt, '=') |
| 85 | opt, optarg = opt[:i], opt[i+1:] |
Guido van Rossum | 1550ff7 | 1996-09-11 19:43:52 +0000 | [diff] [blame] | 86 | except ValueError: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 87 | optarg = None |
Guido van Rossum | 2c349bb | 1996-09-09 15:48:24 +0000 | [diff] [blame] | 88 | |
Guido van Rossum | 1550ff7 | 1996-09-11 19:43:52 +0000 | [diff] [blame] | 89 | has_arg, opt = long_has_args(opt, longopts) |
| 90 | if has_arg: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 91 | if optarg is None: |
| 92 | if not args: |
Guido van Rossum | 80c33e5 | 1999-12-21 22:38:40 +0000 | [diff] [blame] | 93 | raise GetoptError('option --%s requires argument' % opt, opt) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 94 | optarg, args = args[0], args[1:] |
Guido van Rossum | 1550ff7 | 1996-09-11 19:43:52 +0000 | [diff] [blame] | 95 | elif optarg: |
Guido van Rossum | 80c33e5 | 1999-12-21 22:38:40 +0000 | [diff] [blame] | 96 | raise GetoptError('option --%s must not have an argument' % opt, opt) |
Guido van Rossum | 6d06094 | 1998-11-17 04:16:37 +0000 | [diff] [blame] | 97 | opts.append(('--' + opt, optarg or '')) |
| 98 | return opts, args |
Guido van Rossum | 2c349bb | 1996-09-09 15:48:24 +0000 | [diff] [blame] | 99 | |
| 100 | # Return: |
| 101 | # has_arg? |
| 102 | # full option name |
| 103 | def long_has_args(opt, longopts): |
Guido van Rossum | 1550ff7 | 1996-09-11 19:43:52 +0000 | [diff] [blame] | 104 | optlen = len(opt) |
| 105 | for i in range(len(longopts)): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 106 | x, y = longopts[i][:optlen], longopts[i][optlen:] |
| 107 | if opt != x: |
| 108 | continue |
| 109 | if y != '' and y != '=' and i+1 < len(longopts): |
| 110 | if opt == longopts[i+1][:optlen]: |
Guido van Rossum | 80c33e5 | 1999-12-21 22:38:40 +0000 | [diff] [blame] | 111 | raise GetoptError('option --%s not a unique prefix' % opt, opt) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 112 | if longopts[i][-1:] in ('=', ): |
| 113 | return 1, longopts[i][:-1] |
| 114 | return 0, longopts[i] |
Guido van Rossum | 80c33e5 | 1999-12-21 22:38:40 +0000 | [diff] [blame] | 115 | raise GetoptError('option --%s not recognized' % opt, opt) |
Guido van Rossum | 2c349bb | 1996-09-09 15:48:24 +0000 | [diff] [blame] | 116 | |
Guido van Rossum | 6d06094 | 1998-11-17 04:16:37 +0000 | [diff] [blame] | 117 | def do_shorts(opts, optstring, shortopts, args): |
Guido van Rossum | 1550ff7 | 1996-09-11 19:43:52 +0000 | [diff] [blame] | 118 | while optstring != '': |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 119 | opt, optstring = optstring[0], optstring[1:] |
| 120 | if short_has_arg(opt, shortopts): |
| 121 | if optstring == '': |
| 122 | if not args: |
Guido van Rossum | 80c33e5 | 1999-12-21 22:38:40 +0000 | [diff] [blame] | 123 | raise GetoptError('option -%s requires argument' % opt, opt) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 124 | optstring, args = args[0], args[1:] |
| 125 | optarg, optstring = optstring, '' |
| 126 | else: |
| 127 | optarg = '' |
Guido van Rossum | 6d06094 | 1998-11-17 04:16:37 +0000 | [diff] [blame] | 128 | opts.append(('-' + opt, optarg)) |
| 129 | return opts, args |
Guido van Rossum | 2c349bb | 1996-09-09 15:48:24 +0000 | [diff] [blame] | 130 | |
| 131 | def short_has_arg(opt, shortopts): |
Guido van Rossum | 1550ff7 | 1996-09-11 19:43:52 +0000 | [diff] [blame] | 132 | for i in range(len(shortopts)): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 133 | if opt == shortopts[i] != ':': |
| 134 | return shortopts[i+1:i+2] == ':' |
Guido van Rossum | 80c33e5 | 1999-12-21 22:38:40 +0000 | [diff] [blame] | 135 | raise GetoptError('option -%s not recognized' % opt, opt) |
Guido van Rossum | 2c349bb | 1996-09-09 15:48:24 +0000 | [diff] [blame] | 136 | |
| 137 | if __name__ == '__main__': |
Guido van Rossum | 1550ff7 | 1996-09-11 19:43:52 +0000 | [diff] [blame] | 138 | import sys |
| 139 | print getopt(sys.argv[1:], "a:b", ["alpha=", "beta"]) |