|
5 | 5 | function (including the special meanings of arguments of the form `-' |
6 | 6 | and `--'). Long options similar to those supported by GNU software |
7 | 7 | may be used as well via an optional third argument. This module |
8 | | -provides a single function and an exception: |
| 8 | +provides two functions and an exception: |
9 | 9 |
|
10 | 10 | getopt() -- Parse command line options |
| 11 | +gnu_getopt() -- Like getopt(), but allow option and non-option arguments |
| 12 | +to be intermixed. |
11 | 13 | GetoptError -- exception (class) raised with 'opt' attribute, which is the |
12 | 14 | option involved with the exception. |
13 | 15 | """ |
14 | 16 |
|
15 | 17 | # Long option support added by Lars Wirzenius <[email protected]>. |
16 | | - |
| 18 | +# |
17 | 19 | # Gerrit Holl <[email protected]> moved the string-based exceptions |
18 | 20 | # to class-based exceptions. |
| 21 | +# |
| 22 | +# Peter Åstrand <[email protected]> added gnu_getopt(). |
| 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" |
19 | 33 |
|
20 | 34 | __all__ = ["GetoptError","error","getopt"] |
21 | 35 |
|
| 36 | +import os |
| 37 | + |
22 | 38 | class GetoptError(Exception): |
23 | 39 | opt = '' |
24 | 40 | msg = '' |
@@ -75,6 +91,56 @@ def getopt(args, shortopts, longopts = []): |
75 | 91 |
|
76 | 92 | return opts, args |
77 | 93 |
|
| 94 | +def gnu_getopt(args, shortopts, longopts = []): |
| 95 | + """getopt(args, options[, long_options]) -> opts, args |
| 96 | +
|
| 97 | + This function works like getopt(), except that GNU style scanning |
| 98 | + mode is used by default. This means that option and non-option |
| 99 | + arguments may be intermixed. The getopt() function stops |
| 100 | + processing options as soon as a non-option argument is |
| 101 | + encountered. |
| 102 | +
|
| 103 | + If the first character of the option string is `+', or if the |
| 104 | + environment variable POSIXLY_CORRECT is set, then option |
| 105 | + processing stops as soon as a non-option argument is encountered. |
| 106 | + |
| 107 | + """ |
| 108 | + |
| 109 | + opts = [] |
| 110 | + prog_args = [] |
| 111 | + if type(longopts) == type(""): |
| 112 | + longopts = [longopts] |
| 113 | + else: |
| 114 | + longopts = list(longopts) |
| 115 | + |
| 116 | + # Allow options after non-option arguments? |
| 117 | + if shortopts.startswith('+'): |
| 118 | + shortopts = shortopts[1:] |
| 119 | + all_options_first = 1 |
| 120 | + elif os.getenv("POSIXLY_CORRECT"): |
| 121 | + all_options_first = 1 |
| 122 | + else: |
| 123 | + all_options_first = 0 |
| 124 | + |
| 125 | + while args: |
| 126 | + if args[0] == '--': |
| 127 | + prog_args += args[1:] |
| 128 | + break |
| 129 | + |
| 130 | + if args[0][:2] == '--': |
| 131 | + opts, args = do_longs(opts, args[0][2:], longopts, args[1:]) |
| 132 | + elif args[0][:1] == '-': |
| 133 | + opts, args = do_shorts(opts, args[0][1:], shortopts, args[1:]) |
| 134 | + else: |
| 135 | + if all_options_first: |
| 136 | + prog_args += args |
| 137 | + break |
| 138 | + else: |
| 139 | + prog_args.append(args[0]) |
| 140 | + args = args[1:] |
| 141 | + |
| 142 | + return opts, prog_args |
| 143 | + |
78 | 144 | def do_longs(opts, opt, longopts, args): |
79 | 145 | try: |
80 | 146 | i = opt.index('=') |
|
0 commit comments