Mercurial > cpython-lru_cache-weakref
annotate Lib/getopt.py @ 87690:66c1c9f32567 default tip
Add use_weakref option to functools.lru_cache
The use_weakref option makes lru_cache create weak references to the function
arguments when creating the cache key, which prevents them from staying in the
memory forever even after all other references to them are gone.
This is a proof of concept for http://bugs.python.org/issue19859
| author | Radomir Dopieralski <devel@sheep.art.pl> |
|---|---|
| date | Tue, 03 Dec 2013 16:02:48 +0100 |
| parents | 7769c4d72806 |
| children |
| rev | line source |
|---|---|
|
11708
4a23cc49e2b0
Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents:
11615
diff
changeset
|
1 """Parser for command line options. |
|
4280
56909781a741
Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents:
4251
diff
changeset
|
2 |
|
9489
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
3 This module helps scripts to parse the command line arguments in |
|
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
4 sys.argv. It supports the same conventions as the Unix getopt() |
|
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
5 function (including the special meanings of arguments of the form `-' |
|
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
6 and `--'). Long options similar to those supported by GNU software |
|
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
7 may be used as well via an optional third argument. This module |
|
23699
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
8 provides two functions and an exception: |
| 5 | 9 |
|
9489
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
10 getopt() -- Parse command line options |
|
23699
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
11 gnu_getopt() -- Like getopt(), but allow option and non-option arguments |
|
24263
e6033254df21
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23726
diff
changeset
|
12 to be intermixed. |
|
11615
bb1f43f25ce0
Contribution from Gerrit Holl:
Guido van Rossum <guido@python.org>
parents:
9489
diff
changeset
|
13 GetoptError -- exception (class) raised with 'opt' attribute, which is the |
|
bb1f43f25ce0
Contribution from Gerrit Holl:
Guido van Rossum <guido@python.org>
parents:
9489
diff
changeset
|
14 option involved with the exception. |
|
9489
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
15 """ |
| 5 | 16 |
|
9489
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
17 # Long option support added by Lars Wirzenius <liw@iki.fi>. |
|
23699
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
18 # |
|
11852
89b52895310c
Gerrit Holl's patch to move attribution from the docstring to a
Fred Drake <fdrake@acm.org>
parents:
11708
diff
changeset
|
19 # Gerrit Holl <gerrit@nl.linux.org> moved the string-based exceptions |
|
89b52895310c
Gerrit Holl's patch to move attribution from the docstring to a
Fred Drake <fdrake@acm.org>
parents:
11708
diff
changeset
|
20 # to class-based exceptions. |
|
23699
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
21 # |
|
68789
d3e46930ffe9
Mark getopt error messages as localizable (fixes #11371).
Éric Araujo <merwok@netwok.org>
parents:
64316
diff
changeset
|
22 # Peter Åstrand <astrand@lysator.liu.se> added gnu_getopt(). |
|
23699
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
23 # |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
24 # TODO for gnu_getopt(): |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
25 # |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
26 # - GNU getopt_long_only mechanism |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
27 # - allow the caller to specify ordering |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
28 # - RETURN_IN_ORDER option |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
29 # - GNU extension with '-' as first character of option string |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
30 # - optional arguments, specified by double colons |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
31 # - a option string with a W followed by semicolon should |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
32 # treat "-W foo" as "--foo" |
|
11852
89b52895310c
Gerrit Holl's patch to move attribution from the docstring to a
Fred Drake <fdrake@acm.org>
parents:
11708
diff
changeset
|
33 |
|
23726
2cd6ed1c1b58
gnu_getopt should be exported in __all__
Skip Montanaro <skip@pobox.com>
parents:
23708
diff
changeset
|
34 __all__ = ["GetoptError","error","getopt","gnu_getopt"] |
| 16072 | 35 |
|
23699
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
36 import os |
|
68806
160823d258b5
Issue #11621: fix bootstrap issue with getopt/gettext (following d3e46930ffe9)
Antoine Pitrou <solipsis@pitrou.net>
parents:
68789
diff
changeset
|
37 try: |
|
160823d258b5
Issue #11621: fix bootstrap issue with getopt/gettext (following d3e46930ffe9)
Antoine Pitrou <solipsis@pitrou.net>
parents:
68789
diff
changeset
|
38 from gettext import gettext as _ |
|
84442
7769c4d72806
Issue #18200: Back out usage of ModuleNotFoundError (8d28d44f3a9a)
Brett Cannon <brett@python.org>
parents:
84114
diff
changeset
|
39 except ImportError: |
|
68806
160823d258b5
Issue #11621: fix bootstrap issue with getopt/gettext (following d3e46930ffe9)
Antoine Pitrou <solipsis@pitrou.net>
parents:
68789
diff
changeset
|
40 # Bootstrapping Python: gettext's dependencies not built yet |
|
160823d258b5
Issue #11621: fix bootstrap issue with getopt/gettext (following d3e46930ffe9)
Antoine Pitrou <solipsis@pitrou.net>
parents:
68789
diff
changeset
|
41 def _(s): return s |
|
23699
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
42 |
|
11615
bb1f43f25ce0
Contribution from Gerrit Holl:
Guido van Rossum <guido@python.org>
parents:
9489
diff
changeset
|
43 class GetoptError(Exception): |
|
bb1f43f25ce0
Contribution from Gerrit Holl:
Guido van Rossum <guido@python.org>
parents:
9489
diff
changeset
|
44 opt = '' |
|
bb1f43f25ce0
Contribution from Gerrit Holl:
Guido van Rossum <guido@python.org>
parents:
9489
diff
changeset
|
45 msg = '' |
|
27732
fa9f9e57e98f
A few naughty external scripts do 'raise getopt.error, "blah"', and
Andrew M. Kuchling <amk@amk.ca>
parents:
24537
diff
changeset
|
46 def __init__(self, msg, opt=''): |
|
15763
35e08dd37df1
GetoptError is always initialized with exactly two parameters, so simplify
Fred Drake <fdrake@acm.org>
parents:
15674
diff
changeset
|
47 self.msg = msg |
|
35e08dd37df1
GetoptError is always initialized with exactly two parameters, so simplify
Fred Drake <fdrake@acm.org>
parents:
15674
diff
changeset
|
48 self.opt = opt |
|
35e08dd37df1
GetoptError is always initialized with exactly two parameters, so simplify
Fred Drake <fdrake@acm.org>
parents:
15674
diff
changeset
|
49 Exception.__init__(self, msg, opt) |
|
11615
bb1f43f25ce0
Contribution from Gerrit Holl:
Guido van Rossum <guido@python.org>
parents:
9489
diff
changeset
|
50 |
|
bb1f43f25ce0
Contribution from Gerrit Holl:
Guido van Rossum <guido@python.org>
parents:
9489
diff
changeset
|
51 def __str__(self): |
|
bb1f43f25ce0
Contribution from Gerrit Holl:
Guido van Rossum <guido@python.org>
parents:
9489
diff
changeset
|
52 return self.msg |
|
bb1f43f25ce0
Contribution from Gerrit Holl:
Guido van Rossum <guido@python.org>
parents:
9489
diff
changeset
|
53 |
|
bb1f43f25ce0
Contribution from Gerrit Holl:
Guido van Rossum <guido@python.org>
parents:
9489
diff
changeset
|
54 error = GetoptError # backward compatibility |
| 5 | 55 |
|
4251
ac3fbb6771f5
Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents:
564
diff
changeset
|
56 def getopt(args, shortopts, longopts = []): |
|
9489
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
57 """getopt(args, options[, long_options]) -> opts, args |
|
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
58 |
|
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
59 Parses command line options and parameter list. args is the |
|
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
60 argument list to be parsed, without the leading reference to the |
|
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
61 running program. Typically, this means "sys.argv[1:]". shortopts |
|
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
62 is the string of option letters that the script wants to |
|
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
63 recognize, with options that require an argument followed by a |
|
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
64 colon (i.e., the same format that Unix getopt() uses). If |
|
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
65 specified, longopts is a list of strings with the names of the |
|
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
66 long options which should be supported. The leading '--' |
|
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
67 characters should not be included in the option name. Options |
|
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
68 which require an argument should be followed by an equal sign |
|
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
69 ('='). |
|
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
70 |
|
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
71 The return value consists of two elements: the first is a list of |
|
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
72 (option, value) pairs; the second is the list of program arguments |
|
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
73 left after the option list was stripped (this is a trailing slice |
|
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
74 of the first argument). Each option-and-value pair returned has |
|
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
75 the option as its first element, prefixed with a hyphen (e.g., |
|
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
76 '-x'), and the option argument as its second element, or an empty |
|
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
77 string if the option has no argument. The options occur in the |
|
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
78 list in the same order in which they were found, thus allowing |
|
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
79 multiple occurrences. Long and short options may be mixed. |
|
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
80 |
|
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
81 """ |
|
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
82 |
|
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
83 opts = [] |
|
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
84 if type(longopts) == type(""): |
|
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
85 longopts = [longopts] |
|
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
86 else: |
|
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
87 longopts = list(longopts) |
| 15667 | 88 while args and args[0].startswith('-') and args[0] != '-': |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4280
diff
changeset
|
89 if args[0] == '--': |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4280
diff
changeset
|
90 args = args[1:] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4280
diff
changeset
|
91 break |
| 21365 | 92 if args[0].startswith('--'): |
|
9489
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
93 opts, args = do_longs(opts, args[0][2:], longopts, args[1:]) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4280
diff
changeset
|
94 else: |
|
9489
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
95 opts, args = do_shorts(opts, args[0][1:], shortopts, args[1:]) |
|
4251
ac3fbb6771f5
Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents:
564
diff
changeset
|
96 |
|
9489
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
97 return opts, args |
|
4251
ac3fbb6771f5
Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents:
564
diff
changeset
|
98 |
|
23699
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
99 def gnu_getopt(args, shortopts, longopts = []): |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
100 """getopt(args, options[, long_options]) -> opts, args |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
101 |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
102 This function works like getopt(), except that GNU style scanning |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
103 mode is used by default. This means that option and non-option |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
104 arguments may be intermixed. The getopt() function stops |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
105 processing options as soon as a non-option argument is |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
106 encountered. |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
107 |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
108 If the first character of the option string is `+', or if the |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
109 environment variable POSIXLY_CORRECT is set, then option |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
110 processing stops as soon as a non-option argument is encountered. |
|
24263
e6033254df21
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23726
diff
changeset
|
111 |
|
23699
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
112 """ |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
113 |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
114 opts = [] |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
115 prog_args = [] |
|
23708
56a4d3a50b76
Use isinstance for the type check, use booleans.
Martin v. Löwis <martin@v.loewis.de>
parents:
23699
diff
changeset
|
116 if isinstance(longopts, str): |
|
23699
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
117 longopts = [longopts] |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
118 else: |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
119 longopts = list(longopts) |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
120 |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
121 # Allow options after non-option arguments? |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
122 if shortopts.startswith('+'): |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
123 shortopts = shortopts[1:] |
|
23708
56a4d3a50b76
Use isinstance for the type check, use booleans.
Martin v. Löwis <martin@v.loewis.de>
parents:
23699
diff
changeset
|
124 all_options_first = True |
|
24389
eb759da064aa
Use os.environ.get() in stead of os.getenv() (which is platform-dependent).
Jack Jansen <jack.jansen@cwi.nl>
parents:
24263
diff
changeset
|
125 elif os.environ.get("POSIXLY_CORRECT"): |
|
23708
56a4d3a50b76
Use isinstance for the type check, use booleans.
Martin v. Löwis <martin@v.loewis.de>
parents:
23699
diff
changeset
|
126 all_options_first = True |
|
23699
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
127 else: |
|
23708
56a4d3a50b76
Use isinstance for the type check, use booleans.
Martin v. Löwis <martin@v.loewis.de>
parents:
23699
diff
changeset
|
128 all_options_first = False |
|
23699
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
129 |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
130 while args: |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
131 if args[0] == '--': |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
132 prog_args += args[1:] |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
133 break |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
134 |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
135 if args[0][:2] == '--': |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
136 opts, args = do_longs(opts, args[0][2:], longopts, args[1:]) |
|
50079
a86cdf49b821
Manual merge of r67639 from py3k branch:
Georg Brandl <georg@python.org>
parents:
42293
diff
changeset
|
137 elif args[0][:1] == '-' and args[0] != '-': |
|
23699
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
138 opts, args = do_shorts(opts, args[0][1:], shortopts, args[1:]) |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
139 else: |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
140 if all_options_first: |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
141 prog_args += args |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
142 break |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
143 else: |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
144 prog_args.append(args[0]) |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
145 args = args[1:] |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
146 |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
147 return opts, prog_args |
|
18ddc02ce36f
Patch 473512: add GNU style scanning as gnu_getopt.
Martin v. Löwis <martin@v.loewis.de>
parents:
22830
diff
changeset
|
148 |
|
9489
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
149 def do_longs(opts, opt, longopts, args): |
|
4280
56909781a741
Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents:
4251
diff
changeset
|
150 try: |
|
11852
89b52895310c
Gerrit Holl's patch to move attribution from the docstring to a
Fred Drake <fdrake@acm.org>
parents:
11708
diff
changeset
|
151 i = opt.index('=') |
|
4280
56909781a741
Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents:
4251
diff
changeset
|
152 except ValueError: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4280
diff
changeset
|
153 optarg = None |
| 15667 | 154 else: |
| 155 opt, optarg = opt[:i], opt[i+1:] | |
|
4251
ac3fbb6771f5
Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents:
564
diff
changeset
|
156 |
|
4280
56909781a741
Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents:
4251
diff
changeset
|
157 has_arg, opt = long_has_args(opt, longopts) |
|
56909781a741
Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents:
4251
diff
changeset
|
158 if has_arg: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4280
diff
changeset
|
159 if optarg is None: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4280
diff
changeset
|
160 if not args: |
|
68789
d3e46930ffe9
Mark getopt error messages as localizable (fixes #11371).
Éric Araujo <merwok@netwok.org>
parents:
64316
diff
changeset
|
161 raise GetoptError(_('option --%s requires argument') % opt, opt) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4280
diff
changeset
|
162 optarg, args = args[0], args[1:] |
|
63188
8fa424aecd0d
Issue #4629: getopt raises an error if an argument ends with = whereas getopt
Victor Stinner <victor.stinner@haypocalc.com>
parents:
50079
diff
changeset
|
163 elif optarg is not None: |
|
68789
d3e46930ffe9
Mark getopt error messages as localizable (fixes #11371).
Éric Araujo <merwok@netwok.org>
parents:
64316
diff
changeset
|
164 raise GetoptError(_('option --%s must not have an argument') % opt, opt) |
|
9489
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
165 opts.append(('--' + opt, optarg or '')) |
|
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
166 return opts, args |
| 5 | 167 |
|
4251
ac3fbb6771f5
Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents:
564
diff
changeset
|
168 # Return: |
|
ac3fbb6771f5
Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents:
564
diff
changeset
|
169 # has_arg? |
|
ac3fbb6771f5
Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents:
564
diff
changeset
|
170 # full option name |
|
ac3fbb6771f5
Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents:
564
diff
changeset
|
171 def long_has_args(opt, longopts): |
|
15674
75c3a64847c4
getopt used to sort the long option names, in an attempt to simplify
Tim Peters <tim.peters@gmail.com>
parents:
15667
diff
changeset
|
172 possibilities = [o for o in longopts if o.startswith(opt)] |
|
75c3a64847c4
getopt used to sort the long option names, in an attempt to simplify
Tim Peters <tim.peters@gmail.com>
parents:
15667
diff
changeset
|
173 if not possibilities: |
|
68789
d3e46930ffe9
Mark getopt error messages as localizable (fixes #11371).
Éric Araujo <merwok@netwok.org>
parents:
64316
diff
changeset
|
174 raise GetoptError(_('option --%s not recognized') % opt, opt) |
| 15667 | 175 # Is there an exact match? |
| 176 if opt in possibilities: | |
|
22830
a9dd5e6d6805
Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents:
21365
diff
changeset
|
177 return False, opt |
| 15667 | 178 elif opt + '=' in possibilities: |
|
22830
a9dd5e6d6805
Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents:
21365
diff
changeset
|
179 return True, opt |
| 15667 | 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 | |
|
68789
d3e46930ffe9
Mark getopt error messages as localizable (fixes #11371).
Éric Araujo <merwok@netwok.org>
parents:
64316
diff
changeset
|
184 raise GetoptError(_('option --%s not a unique prefix') % opt, opt) |
| 15667 | 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 | |
|
4251
ac3fbb6771f5
Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents:
564
diff
changeset
|
191 |
|
9489
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
192 def do_shorts(opts, optstring, shortopts, args): |
|
4280
56909781a741
Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents:
4251
diff
changeset
|
193 while optstring != '': |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4280
diff
changeset
|
194 opt, optstring = optstring[0], optstring[1:] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4280
diff
changeset
|
195 if short_has_arg(opt, shortopts): |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4280
diff
changeset
|
196 if optstring == '': |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4280
diff
changeset
|
197 if not args: |
|
68789
d3e46930ffe9
Mark getopt error messages as localizable (fixes #11371).
Éric Araujo <merwok@netwok.org>
parents:
64316
diff
changeset
|
198 raise GetoptError(_('option -%s requires argument') % opt, |
| 21365 | 199 opt) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4280
diff
changeset
|
200 optstring, args = args[0], args[1:] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4280
diff
changeset
|
201 optarg, optstring = optstring, '' |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4280
diff
changeset
|
202 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4280
diff
changeset
|
203 optarg = '' |
|
9489
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
204 opts.append(('-' + opt, optarg)) |
|
db7cca09beaf
Accept a non-list sequence for the long options (request by Jack Jansen).
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
205 return opts, args |
|
4251
ac3fbb6771f5
Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents:
564
diff
changeset
|
206 |
|
ac3fbb6771f5
Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents:
564
diff
changeset
|
207 def short_has_arg(opt, shortopts): |
|
4280
56909781a741
Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents:
4251
diff
changeset
|
208 for i in range(len(shortopts)): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
4280
diff
changeset
|
209 if opt == shortopts[i] != ':': |
| 21365 | 210 return shortopts.startswith(':', i+1) |
|
68789
d3e46930ffe9
Mark getopt error messages as localizable (fixes #11371).
Éric Araujo <merwok@netwok.org>
parents:
64316
diff
changeset
|
211 raise GetoptError(_('option -%s not recognized') % opt, opt) |
|
4251
ac3fbb6771f5
Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents:
564
diff
changeset
|
212 |
|
ac3fbb6771f5
Rewritten by Lars Wizenius to add long options
Guido van Rossum <guido@python.org>
parents:
564
diff
changeset
|
213 if __name__ == '__main__': |
|
4280
56909781a741
Layout and cleanup by Fred
Guido van Rossum <guido@python.org>
parents:
4251
diff
changeset
|
214 import sys |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
27732
diff
changeset
|
215 print(getopt(sys.argv[1:], "a:b", ["alpha=", "beta"])) |
