Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 1 | # module getopt -- Standard command line processing. |
| 2 | |
| 3 | # Function getopt.getopt() has a different interface but provides the |
Guido van Rossum | 1550ff7 | 1996-09-11 19:43:52 +0000 | [diff] [blame] | 4 | # similar functionality to the Unix getopt() function, with the |
| 5 | # addition of long-option support. (Long option support added by Lars |
| 6 | # Wirzenius <liw@iki.fi>.) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 1550ff7 | 1996-09-11 19:43:52 +0000 | [diff] [blame] | 8 | # It has two required arguments: the first should be argv[1:] (it |
| 9 | # doesn't want the script name), the second the string of option |
| 10 | # letters as passed to Unix getopt() (i.e., a string of allowable |
| 11 | # option letters, with options requiring an argument followed by a |
| 12 | # colon). |
| 13 | |
| 14 | # The optional third argument, if present, getopt.getopt works similar |
| 15 | # to the GNU getopt_long function (but optional arguments are not |
| 16 | # supported). The third argument should be a list of strings that |
| 17 | # name the long options. If the name ends '=', the argument requires |
| 18 | # an argument. |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 19 | |
| 20 | # It raises the exception getopt.error with a string argument if it |
| 21 | # detects an error. |
| 22 | |
Guido van Rossum | 1550ff7 | 1996-09-11 19:43:52 +0000 | [diff] [blame] | 23 | # It returns two values: |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 24 | # (1) a list of pairs (option, option_argument) giving the options in |
| 25 | # the order in which they were specified. (I'd use a dictionary |
| 26 | # but applications may depend on option order or multiple |
| 27 | # occurrences.) Boolean options have '' as option_argument. |
| 28 | # (2) the list of remaining arguments (may be empty). |
| 29 | |
Guido van Rossum | 2c349bb | 1996-09-09 15:48:24 +0000 | [diff] [blame] | 30 | import string |
| 31 | |
Guido van Rossum | 1550ff7 | 1996-09-11 19:43:52 +0000 | [diff] [blame] | 32 | error = 'getopt.error' |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 33 | |
Guido van Rossum | 2c349bb | 1996-09-09 15:48:24 +0000 | [diff] [blame] | 34 | def getopt(args, shortopts, longopts = []): |
Guido van Rossum | 1550ff7 | 1996-09-11 19:43:52 +0000 | [diff] [blame] | 35 | list = [] |
| 36 | longopts = longopts[:] |
| 37 | longopts.sort() |
| 38 | while args and args[0][:1] == '-' and args[0] != '-': |
| 39 | if args[0] == '--': |
| 40 | args = args[1:] |
| 41 | break |
| 42 | if args[0][:2] == '--': |
| 43 | list, args = do_longs(list, args[0][2:], longopts, args[1:]) |
| 44 | else: |
| 45 | list, args = do_shorts(list, args[0][1:], shortopts, args[1:]) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 46 | |
Guido van Rossum | 1550ff7 | 1996-09-11 19:43:52 +0000 | [diff] [blame] | 47 | return list, args |
Guido van Rossum | 2c349bb | 1996-09-09 15:48:24 +0000 | [diff] [blame] | 48 | |
| 49 | def do_longs(list, opt, longopts, args): |
Guido van Rossum | 1550ff7 | 1996-09-11 19:43:52 +0000 | [diff] [blame] | 50 | try: |
| 51 | i = string.index(opt, '=') |
| 52 | opt, optarg = opt[:i], opt[i+1:] |
| 53 | except ValueError: |
| 54 | optarg = None |
Guido van Rossum | 2c349bb | 1996-09-09 15:48:24 +0000 | [diff] [blame] | 55 | |
Guido van Rossum | 1550ff7 | 1996-09-11 19:43:52 +0000 | [diff] [blame] | 56 | has_arg, opt = long_has_args(opt, longopts) |
| 57 | if has_arg: |
| 58 | if optarg is None: |
| 59 | if not args: |
| 60 | raise error, 'option --%s requires argument' % opt |
| 61 | optarg, args = args[0], args[1:] |
| 62 | elif optarg: |
| 63 | raise error, 'option --%s must not have an argument' % opt |
| 64 | list.append(('--' + opt, optarg or '')) |
| 65 | return list, args |
Guido van Rossum | 2c349bb | 1996-09-09 15:48:24 +0000 | [diff] [blame] | 66 | |
| 67 | # Return: |
| 68 | # has_arg? |
| 69 | # full option name |
| 70 | def long_has_args(opt, longopts): |
Guido van Rossum | 1550ff7 | 1996-09-11 19:43:52 +0000 | [diff] [blame] | 71 | optlen = len(opt) |
| 72 | for i in range(len(longopts)): |
| 73 | x, y = longopts[i][:optlen], longopts[i][optlen:] |
| 74 | if opt != x: |
| 75 | continue |
| 76 | if y != '' and y != '=' and i+1 < len(longopts): |
| 77 | if opt == longopts[i+1][:optlen]: |
| 78 | raise error, 'option --%s not a unique prefix' % opt |
| 79 | if longopts[i][-1:] in ('=', ): |
| 80 | return 1, longopts[i][:-1] |
| 81 | return 0, longopts[i] |
| 82 | raise error, 'option --' + opt + ' not recognized' |
Guido van Rossum | 2c349bb | 1996-09-09 15:48:24 +0000 | [diff] [blame] | 83 | |
| 84 | def do_shorts(list, optstring, shortopts, args): |
Guido van Rossum | 1550ff7 | 1996-09-11 19:43:52 +0000 | [diff] [blame] | 85 | while optstring != '': |
| 86 | opt, optstring = optstring[0], optstring[1:] |
| 87 | if short_has_arg(opt, shortopts): |
| 88 | if optstring == '': |
| 89 | if not args: |
| 90 | raise error, 'option -%s requires argument' % opt |
| 91 | optstring, args = args[0], args[1:] |
| 92 | optarg, optstring = optstring, '' |
| 93 | else: |
| 94 | optarg = '' |
| 95 | list.append(('-' + opt, optarg)) |
| 96 | return list, args |
Guido van Rossum | 2c349bb | 1996-09-09 15:48:24 +0000 | [diff] [blame] | 97 | |
| 98 | def short_has_arg(opt, shortopts): |
Guido van Rossum | 1550ff7 | 1996-09-11 19:43:52 +0000 | [diff] [blame] | 99 | for i in range(len(shortopts)): |
| 100 | if opt == shortopts[i] != ':': |
| 101 | return shortopts[i+1:i+2] == ':' |
| 102 | raise error, 'option -%s not recognized' % opt |
Guido van Rossum | 2c349bb | 1996-09-09 15:48:24 +0000 | [diff] [blame] | 103 | |
| 104 | if __name__ == '__main__': |
Guido van Rossum | 1550ff7 | 1996-09-11 19:43:52 +0000 | [diff] [blame] | 105 | import sys |
| 106 | print getopt(sys.argv[1:], "a:b", ["alpha=", "beta"]) |