Merged revisions 66801,66803-66804,66813,66854-66856,66866,66870-66872,66874,66887,66903,66905,66911,66913,66927,66932,66938,66942,66962,66964,66973-66974,66977,66992,66998-66999,67002,67005,67007,67028,67040-67041,67044,67070,67089,67091,67101,67117-67119,67123-67124 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
................
r66801 | andrew.kuchling | 2008-10-04 23:51:59 +0200 (Sat, 04 Oct 2008) | 1 line
Punctuation fix; expand dict.update docstring to be clearer
................
r66803 | benjamin.peterson | 2008-10-05 00:15:31 +0200 (Sun, 05 Oct 2008) | 1 line
fix typo
................
r66804 | andrew.kuchling | 2008-10-05 02:11:56 +0200 (Sun, 05 Oct 2008) | 1 line
#1415508 from Rocky Bernstein: add docstrings for enable_interspersed_args(), disable_interspersed_args()
................
r66813 | andrew.kuchling | 2008-10-06 14:07:04 +0200 (Mon, 06 Oct 2008) | 3 lines
Per Greg Ward, optparse is no longer being externally maintained.
I'll look at the bugs in the Optik bug tracker and copy them to the Python bug
tracker if they're still relevant.
................
r66854 | georg.brandl | 2008-10-08 19:20:20 +0200 (Wed, 08 Oct 2008) | 2 lines
#4059: patch up some sqlite docs.
................
r66855 | georg.brandl | 2008-10-08 19:30:55 +0200 (Wed, 08 Oct 2008) | 2 lines
#4058: fix some whatsnew markup.
................
r66856 | georg.brandl | 2008-10-08 20:47:17 +0200 (Wed, 08 Oct 2008) | 3 lines
#3935: properly support list subclasses in the C impl. of bisect.
Patch reviewed by Raymond.
................
r66866 | benjamin.peterson | 2008-10-09 22:54:43 +0200 (Thu, 09 Oct 2008) | 1 line
update paragraph about __future__ for 2.6
................
r66870 | armin.rigo | 2008-10-10 10:40:44 +0200 (Fri, 10 Oct 2008) | 2 lines
Typo: "ThreadError" is the name in the C source.
................
r66871 | benjamin.peterson | 2008-10-10 22:38:49 +0200 (Fri, 10 Oct 2008) | 1 line
fix a small typo
................
r66872 | benjamin.peterson | 2008-10-10 22:51:37 +0200 (Fri, 10 Oct 2008) | 1 line
talk about how you can unzip with zip
................
r66874 | benjamin.peterson | 2008-10-11 00:23:41 +0200 (Sat, 11 Oct 2008) | 1 line
PyGILState_Acquire -> PyGILState_Ensure
................
r66887 | benjamin.peterson | 2008-10-13 23:51:40 +0200 (Mon, 13 Oct 2008) | 1 line
document how to disable fixers
................
r66903 | benjamin.peterson | 2008-10-15 22:34:09 +0200 (Wed, 15 Oct 2008) | 1 line
don't recurse into directories that start with '.'
................
r66905 | benjamin.peterson | 2008-10-15 23:05:55 +0200 (Wed, 15 Oct 2008) | 1 line
support the optional line argument for idle
................
r66911 | benjamin.peterson | 2008-10-16 01:10:28 +0200 (Thu, 16 Oct 2008) | 41 lines
Merged revisions 66805,66841,66860,66884-66886,66893,66907,66910 via svnmerge from
svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3
........
r66805 | benjamin.peterson | 2008-10-04 20:11:02 -0500 (Sat, 04 Oct 2008) | 1 line
mention what the fixes directory is for
........
r66841 | benjamin.peterson | 2008-10-07 17:48:12 -0500 (Tue, 07 Oct 2008) | 1 line
use assertFalse and assertTrue
........
r66860 | benjamin.peterson | 2008-10-08 16:05:07 -0500 (Wed, 08 Oct 2008) | 1 line
instead of abusing the pattern matcher, use start_tree to find a next binding
........
r66884 | benjamin.peterson | 2008-10-13 15:50:30 -0500 (Mon, 13 Oct 2008) | 1 line
don't print tokens to stdout when -v is given
........
r66885 | benjamin.peterson | 2008-10-13 16:28:57 -0500 (Mon, 13 Oct 2008) | 1 line
add the -x option to disable fixers
........
r66886 | benjamin.peterson | 2008-10-13 16:33:53 -0500 (Mon, 13 Oct 2008) | 1 line
cut down on some crud
........
r66893 | benjamin.peterson | 2008-10-14 17:16:54 -0500 (Tue, 14 Oct 2008) | 1 line
add an optional set literal fixer
........
r66907 | benjamin.peterson | 2008-10-15 16:59:41 -0500 (Wed, 15 Oct 2008) | 1 line
don't write backup files by default
........
r66910 | benjamin.peterson | 2008-10-15 17:43:10 -0500 (Wed, 15 Oct 2008) | 1 line
add the -n option; it stops backupfiles from being written
........
................
r66913 | benjamin.peterson | 2008-10-16 20:52:14 +0200 (Thu, 16 Oct 2008) | 1 line
document that deque indexing is O(n) #4123
................
r66927 | andrew.kuchling | 2008-10-16 22:15:47 +0200 (Thu, 16 Oct 2008) | 1 line
Fix wording (2.6.1 backport candidate)
................
r66932 | benjamin.peterson | 2008-10-16 23:09:28 +0200 (Thu, 16 Oct 2008) | 1 line
check for error conditions in _json #3623
................
r66938 | benjamin.peterson | 2008-10-16 23:27:54 +0200 (Thu, 16 Oct 2008) | 1 line
fix possible ref leak
................
r66942 | benjamin.peterson | 2008-10-16 23:48:06 +0200 (Thu, 16 Oct 2008) | 1 line
fix more possible ref leaks in _json and use Py_CLEAR
................
r66962 | benjamin.peterson | 2008-10-17 22:01:01 +0200 (Fri, 17 Oct 2008) | 1 line
clarify CALL_FUNCTION #4141
................
r66964 | georg.brandl | 2008-10-17 23:41:49 +0200 (Fri, 17 Oct 2008) | 2 lines
Fix duplicate word.
................
r66973 | armin.ronacher | 2008-10-19 10:27:43 +0200 (Sun, 19 Oct 2008) | 3 lines
Fixed #4067 by implementing _attributes and _fields for the AST root node.
................
r66974 | benjamin.peterson | 2008-10-19 15:59:01 +0200 (Sun, 19 Oct 2008) | 1 line
fix compiler warning
................
r66977 | benjamin.peterson | 2008-10-19 21:39:16 +0200 (Sun, 19 Oct 2008) | 1 line
mention -n
................
r66992 | benjamin.peterson | 2008-10-21 22:51:13 +0200 (Tue, 21 Oct 2008) | 1 line
make sure to call iteritems()
................
r66998 | benjamin.peterson | 2008-10-22 22:57:43 +0200 (Wed, 22 Oct 2008) | 1 line
fix a few typos
................
r66999 | benjamin.peterson | 2008-10-22 23:05:30 +0200 (Wed, 22 Oct 2008) | 1 line
and another typo...
................
r67002 | hirokazu.yamamoto | 2008-10-23 02:37:33 +0200 (Thu, 23 Oct 2008) | 1 line
Issue #4183: Some tests didn't run with pickle.HIGHEST_PROTOCOL.
................
r67005 | walter.doerwald | 2008-10-23 15:11:39 +0200 (Thu, 23 Oct 2008) | 2 lines
Use the correct names of the stateless codec functions (Fixes issue 4178).
................
r67007 | benjamin.peterson | 2008-10-23 23:43:48 +0200 (Thu, 23 Oct 2008) | 1 line
only nonempty __slots__ don't work
................
r67028 | benjamin.peterson | 2008-10-26 01:27:07 +0200 (Sun, 26 Oct 2008) | 1 line
don't use a catch-all
................
r67040 | armin.rigo | 2008-10-28 18:01:21 +0100 (Tue, 28 Oct 2008) | 5 lines
Fix one of the tests: it relied on being present in an "output test" in
order to actually test what it was supposed to test, i.e. that the code
in the __del__ method did not crash. Use instead the new helper
test_support.captured_output().
................
r67041 | benjamin.peterson | 2008-10-29 21:33:00 +0100 (Wed, 29 Oct 2008) | 1 line
mention the version gettempdir() was added
................
r67044 | amaury.forgeotdarc | 2008-10-30 00:15:57 +0100 (Thu, 30 Oct 2008) | 3 lines
Correct error message in io.open():
closefd=True is the only accepted value with a file name.
................
r67070 | benjamin.peterson | 2008-10-31 21:41:44 +0100 (Fri, 31 Oct 2008) | 1 line
rephrase has_key doc
................
r67089 | benjamin.peterson | 2008-11-03 21:43:20 +0100 (Mon, 03 Nov 2008) | 1 line
clarify by splitting into multiple paragraphs
................
r67091 | benjamin.peterson | 2008-11-03 23:34:57 +0100 (Mon, 03 Nov 2008) | 1 line
move a FileIO test to test_fileio
................
r67101 | georg.brandl | 2008-11-04 21:49:35 +0100 (Tue, 04 Nov 2008) | 2 lines
#4167: fix markup glitches.
................
r67117 | georg.brandl | 2008-11-06 11:17:58 +0100 (Thu, 06 Nov 2008) | 2 lines
#4268: Use correct module for two toplevel functions.
................
r67118 | georg.brandl | 2008-11-06 11:19:11 +0100 (Thu, 06 Nov 2008) | 2 lines
#4267: small fixes in sqlite3 docs.
................
r67119 | georg.brandl | 2008-11-06 11:20:49 +0100 (Thu, 06 Nov 2008) | 2 lines
#4245: move Thread section to the top.
................
r67123 | georg.brandl | 2008-11-06 19:49:15 +0100 (Thu, 06 Nov 2008) | 2 lines
#4247: add "pass" examples to tutorial.
................
r67124 | andrew.kuchling | 2008-11-06 20:23:02 +0100 (Thu, 06 Nov 2008) | 1 line
Fix grammar error; reword two paragraphs
................
2008-11-07 04:56:27 -04:00
|
|
|
"""A powerful, extensible, and easy-to-use option parser.
|
2002-11-14 18:00:19 -04:00
|
|
|
|
|
|
|
By Greg Ward <gward@python.net>
|
|
|
|
|
Merged revisions 66801,66803-66804,66813,66854-66856,66866,66870-66872,66874,66887,66903,66905,66911,66913,66927,66932,66938,66942,66962,66964,66973-66974,66977,66992,66998-66999,67002,67005,67007,67028,67040-67041,67044,67070,67089,67091,67101,67117-67119,67123-67124 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
................
r66801 | andrew.kuchling | 2008-10-04 23:51:59 +0200 (Sat, 04 Oct 2008) | 1 line
Punctuation fix; expand dict.update docstring to be clearer
................
r66803 | benjamin.peterson | 2008-10-05 00:15:31 +0200 (Sun, 05 Oct 2008) | 1 line
fix typo
................
r66804 | andrew.kuchling | 2008-10-05 02:11:56 +0200 (Sun, 05 Oct 2008) | 1 line
#1415508 from Rocky Bernstein: add docstrings for enable_interspersed_args(), disable_interspersed_args()
................
r66813 | andrew.kuchling | 2008-10-06 14:07:04 +0200 (Mon, 06 Oct 2008) | 3 lines
Per Greg Ward, optparse is no longer being externally maintained.
I'll look at the bugs in the Optik bug tracker and copy them to the Python bug
tracker if they're still relevant.
................
r66854 | georg.brandl | 2008-10-08 19:20:20 +0200 (Wed, 08 Oct 2008) | 2 lines
#4059: patch up some sqlite docs.
................
r66855 | georg.brandl | 2008-10-08 19:30:55 +0200 (Wed, 08 Oct 2008) | 2 lines
#4058: fix some whatsnew markup.
................
r66856 | georg.brandl | 2008-10-08 20:47:17 +0200 (Wed, 08 Oct 2008) | 3 lines
#3935: properly support list subclasses in the C impl. of bisect.
Patch reviewed by Raymond.
................
r66866 | benjamin.peterson | 2008-10-09 22:54:43 +0200 (Thu, 09 Oct 2008) | 1 line
update paragraph about __future__ for 2.6
................
r66870 | armin.rigo | 2008-10-10 10:40:44 +0200 (Fri, 10 Oct 2008) | 2 lines
Typo: "ThreadError" is the name in the C source.
................
r66871 | benjamin.peterson | 2008-10-10 22:38:49 +0200 (Fri, 10 Oct 2008) | 1 line
fix a small typo
................
r66872 | benjamin.peterson | 2008-10-10 22:51:37 +0200 (Fri, 10 Oct 2008) | 1 line
talk about how you can unzip with zip
................
r66874 | benjamin.peterson | 2008-10-11 00:23:41 +0200 (Sat, 11 Oct 2008) | 1 line
PyGILState_Acquire -> PyGILState_Ensure
................
r66887 | benjamin.peterson | 2008-10-13 23:51:40 +0200 (Mon, 13 Oct 2008) | 1 line
document how to disable fixers
................
r66903 | benjamin.peterson | 2008-10-15 22:34:09 +0200 (Wed, 15 Oct 2008) | 1 line
don't recurse into directories that start with '.'
................
r66905 | benjamin.peterson | 2008-10-15 23:05:55 +0200 (Wed, 15 Oct 2008) | 1 line
support the optional line argument for idle
................
r66911 | benjamin.peterson | 2008-10-16 01:10:28 +0200 (Thu, 16 Oct 2008) | 41 lines
Merged revisions 66805,66841,66860,66884-66886,66893,66907,66910 via svnmerge from
svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3
........
r66805 | benjamin.peterson | 2008-10-04 20:11:02 -0500 (Sat, 04 Oct 2008) | 1 line
mention what the fixes directory is for
........
r66841 | benjamin.peterson | 2008-10-07 17:48:12 -0500 (Tue, 07 Oct 2008) | 1 line
use assertFalse and assertTrue
........
r66860 | benjamin.peterson | 2008-10-08 16:05:07 -0500 (Wed, 08 Oct 2008) | 1 line
instead of abusing the pattern matcher, use start_tree to find a next binding
........
r66884 | benjamin.peterson | 2008-10-13 15:50:30 -0500 (Mon, 13 Oct 2008) | 1 line
don't print tokens to stdout when -v is given
........
r66885 | benjamin.peterson | 2008-10-13 16:28:57 -0500 (Mon, 13 Oct 2008) | 1 line
add the -x option to disable fixers
........
r66886 | benjamin.peterson | 2008-10-13 16:33:53 -0500 (Mon, 13 Oct 2008) | 1 line
cut down on some crud
........
r66893 | benjamin.peterson | 2008-10-14 17:16:54 -0500 (Tue, 14 Oct 2008) | 1 line
add an optional set literal fixer
........
r66907 | benjamin.peterson | 2008-10-15 16:59:41 -0500 (Wed, 15 Oct 2008) | 1 line
don't write backup files by default
........
r66910 | benjamin.peterson | 2008-10-15 17:43:10 -0500 (Wed, 15 Oct 2008) | 1 line
add the -n option; it stops backupfiles from being written
........
................
r66913 | benjamin.peterson | 2008-10-16 20:52:14 +0200 (Thu, 16 Oct 2008) | 1 line
document that deque indexing is O(n) #4123
................
r66927 | andrew.kuchling | 2008-10-16 22:15:47 +0200 (Thu, 16 Oct 2008) | 1 line
Fix wording (2.6.1 backport candidate)
................
r66932 | benjamin.peterson | 2008-10-16 23:09:28 +0200 (Thu, 16 Oct 2008) | 1 line
check for error conditions in _json #3623
................
r66938 | benjamin.peterson | 2008-10-16 23:27:54 +0200 (Thu, 16 Oct 2008) | 1 line
fix possible ref leak
................
r66942 | benjamin.peterson | 2008-10-16 23:48:06 +0200 (Thu, 16 Oct 2008) | 1 line
fix more possible ref leaks in _json and use Py_CLEAR
................
r66962 | benjamin.peterson | 2008-10-17 22:01:01 +0200 (Fri, 17 Oct 2008) | 1 line
clarify CALL_FUNCTION #4141
................
r66964 | georg.brandl | 2008-10-17 23:41:49 +0200 (Fri, 17 Oct 2008) | 2 lines
Fix duplicate word.
................
r66973 | armin.ronacher | 2008-10-19 10:27:43 +0200 (Sun, 19 Oct 2008) | 3 lines
Fixed #4067 by implementing _attributes and _fields for the AST root node.
................
r66974 | benjamin.peterson | 2008-10-19 15:59:01 +0200 (Sun, 19 Oct 2008) | 1 line
fix compiler warning
................
r66977 | benjamin.peterson | 2008-10-19 21:39:16 +0200 (Sun, 19 Oct 2008) | 1 line
mention -n
................
r66992 | benjamin.peterson | 2008-10-21 22:51:13 +0200 (Tue, 21 Oct 2008) | 1 line
make sure to call iteritems()
................
r66998 | benjamin.peterson | 2008-10-22 22:57:43 +0200 (Wed, 22 Oct 2008) | 1 line
fix a few typos
................
r66999 | benjamin.peterson | 2008-10-22 23:05:30 +0200 (Wed, 22 Oct 2008) | 1 line
and another typo...
................
r67002 | hirokazu.yamamoto | 2008-10-23 02:37:33 +0200 (Thu, 23 Oct 2008) | 1 line
Issue #4183: Some tests didn't run with pickle.HIGHEST_PROTOCOL.
................
r67005 | walter.doerwald | 2008-10-23 15:11:39 +0200 (Thu, 23 Oct 2008) | 2 lines
Use the correct names of the stateless codec functions (Fixes issue 4178).
................
r67007 | benjamin.peterson | 2008-10-23 23:43:48 +0200 (Thu, 23 Oct 2008) | 1 line
only nonempty __slots__ don't work
................
r67028 | benjamin.peterson | 2008-10-26 01:27:07 +0200 (Sun, 26 Oct 2008) | 1 line
don't use a catch-all
................
r67040 | armin.rigo | 2008-10-28 18:01:21 +0100 (Tue, 28 Oct 2008) | 5 lines
Fix one of the tests: it relied on being present in an "output test" in
order to actually test what it was supposed to test, i.e. that the code
in the __del__ method did not crash. Use instead the new helper
test_support.captured_output().
................
r67041 | benjamin.peterson | 2008-10-29 21:33:00 +0100 (Wed, 29 Oct 2008) | 1 line
mention the version gettempdir() was added
................
r67044 | amaury.forgeotdarc | 2008-10-30 00:15:57 +0100 (Thu, 30 Oct 2008) | 3 lines
Correct error message in io.open():
closefd=True is the only accepted value with a file name.
................
r67070 | benjamin.peterson | 2008-10-31 21:41:44 +0100 (Fri, 31 Oct 2008) | 1 line
rephrase has_key doc
................
r67089 | benjamin.peterson | 2008-11-03 21:43:20 +0100 (Mon, 03 Nov 2008) | 1 line
clarify by splitting into multiple paragraphs
................
r67091 | benjamin.peterson | 2008-11-03 23:34:57 +0100 (Mon, 03 Nov 2008) | 1 line
move a FileIO test to test_fileio
................
r67101 | georg.brandl | 2008-11-04 21:49:35 +0100 (Tue, 04 Nov 2008) | 2 lines
#4167: fix markup glitches.
................
r67117 | georg.brandl | 2008-11-06 11:17:58 +0100 (Thu, 06 Nov 2008) | 2 lines
#4268: Use correct module for two toplevel functions.
................
r67118 | georg.brandl | 2008-11-06 11:19:11 +0100 (Thu, 06 Nov 2008) | 2 lines
#4267: small fixes in sqlite3 docs.
................
r67119 | georg.brandl | 2008-11-06 11:20:49 +0100 (Thu, 06 Nov 2008) | 2 lines
#4245: move Thread section to the top.
................
r67123 | georg.brandl | 2008-11-06 19:49:15 +0100 (Thu, 06 Nov 2008) | 2 lines
#4247: add "pass" examples to tutorial.
................
r67124 | andrew.kuchling | 2008-11-06 20:23:02 +0100 (Thu, 06 Nov 2008) | 1 line
Fix grammar error; reword two paragraphs
................
2008-11-07 04:56:27 -04:00
|
|
|
Originally distributed as Optik.
|
2003-04-20 23:40:34 -03:00
|
|
|
|
|
|
|
For support, use the optik-users@lists.sourceforge.net mailing list
|
|
|
|
(http://lists.sourceforge.net/lists/listinfo/optik-users).
|
2002-11-14 18:00:19 -04:00
|
|
|
"""
|
|
|
|
|
2006-07-23 13:05:51 -03:00
|
|
|
__version__ = "1.5.3"
|
2003-05-07 22:38:52 -03:00
|
|
|
|
|
|
|
__all__ = ['Option',
|
Merged revisions 70866-70868,70870-70871,70893,70896,70902,70905,70907,70912,70915,70927,70933,70940,70944,70954,70963,70998,71056 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r70866 | georg.brandl | 2009-03-31 21:06:57 +0200 (Di, 31 Mär 2009) | 1 line
#4882: document named group behavior a bit better.
........
r70867 | georg.brandl | 2009-03-31 21:10:35 +0200 (Di, 31 Mär 2009) | 1 line
#1096310: document usage of sys.__std*__ a bit better.
........
r70868 | georg.brandl | 2009-03-31 21:12:17 +0200 (Di, 31 Mär 2009) | 1 line
#5190: export make_option in __all__.
........
r70870 | georg.brandl | 2009-03-31 21:26:24 +0200 (Di, 31 Mär 2009) | 1 line
#4411: document mro() and __mro__. (I hope I got it right.)
........
r70871 | georg.brandl | 2009-03-31 21:30:56 +0200 (Di, 31 Mär 2009) | 1 line
#5618: fix typo.
........
r70893 | georg.brandl | 2009-03-31 22:56:32 +0200 (Di, 31 Mär 2009) | 1 line
#1530012: move TQS section before raw strings.
........
r70896 | georg.brandl | 2009-03-31 23:15:33 +0200 (Di, 31 Mär 2009) | 1 line
#5598: document DocFileSuite *args argument.
........
r70902 | georg.brandl | 2009-03-31 23:43:03 +0200 (Di, 31 Mär 2009) | 1 line
#1675026: add a note about a strange Windows problem, and remove notes about AtheOS.
........
r70905 | georg.brandl | 2009-04-01 00:03:40 +0200 (Mi, 01 Apr 2009) | 1 line
#5563: more documentation for bdist_msi.
........
r70907 | georg.brandl | 2009-04-01 00:18:19 +0200 (Mi, 01 Apr 2009) | 1 line
#3427: document correct return type for urlopen().info().
........
r70912 | georg.brandl | 2009-04-01 00:35:46 +0200 (Mi, 01 Apr 2009) | 1 line
#5617: add a handy function to print a unicode string to gdbinit.
........
r70915 | georg.brandl | 2009-04-01 00:40:16 +0200 (Mi, 01 Apr 2009) | 1 line
#5018: remove confusing paragraph.
........
r70927 | georg.brandl | 2009-04-01 01:01:27 +0200 (Mi, 01 Apr 2009) | 1 line
Dont shout to users.
........
r70933 | georg.brandl | 2009-04-01 02:04:33 +0200 (Mi, 01 Apr 2009) | 2 lines
Issue #5635: Fix running test_sys with tracing enabled.
........
r70940 | georg.brandl | 2009-04-01 06:21:14 +0200 (Mi, 01 Apr 2009) | 2 lines
The SimpleXMLRPCServer's CGI handler now runs like a pony.
........
r70944 | georg.brandl | 2009-04-01 06:32:39 +0200 (Mi, 01 Apr 2009) | 1 line
#5631: add upload to list of possible commands, which is presented in --help-commands.
........
r70954 | georg.brandl | 2009-04-01 17:23:43 +0200 (Mi, 01 Apr 2009) | 1 line
Fix test_xmlrpc and make the CGI handler work with no CONTENT_LENGTH.
........
r70963 | georg.brandl | 2009-04-01 19:46:01 +0200 (Mi, 01 Apr 2009) | 1 line
#5655: fix docstring oversight.
........
r70998 | georg.brandl | 2009-04-01 23:54:21 +0200 (Mi, 01 Apr 2009) | 1 line
In Pdb, stop assigning values to __builtin__._ which interferes with the one commonly installed by gettext.
........
r71056 | georg.brandl | 2009-04-02 19:43:07 +0200 (Do, 02 Apr 2009) | 2 lines
Actually the displayhook should print the repr.
........
2009-04-05 18:21:05 -03:00
|
|
|
'make_option',
|
2003-05-07 22:38:52 -03:00
|
|
|
'SUPPRESS_HELP',
|
|
|
|
'SUPPRESS_USAGE',
|
|
|
|
'Values',
|
|
|
|
'OptionContainer',
|
|
|
|
'OptionGroup',
|
|
|
|
'OptionParser',
|
|
|
|
'HelpFormatter',
|
|
|
|
'IndentedHelpFormatter',
|
|
|
|
'TitledHelpFormatter',
|
|
|
|
'OptParseError',
|
|
|
|
'OptionError',
|
|
|
|
'OptionConflictError',
|
|
|
|
'OptionValueError',
|
|
|
|
'BadOptionError']
|
2003-04-20 23:40:34 -03:00
|
|
|
|
2002-11-14 18:00:19 -04:00
|
|
|
__copyright__ = """
|
2006-04-23 00:47:58 -03:00
|
|
|
Copyright (c) 2001-2006 Gregory P. Ward. All rights reserved.
|
|
|
|
Copyright (c) 2002-2006 Python Software Foundation. All rights reserved.
|
2002-11-14 18:00:19 -04:00
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are
|
|
|
|
met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived from
|
|
|
|
this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
|
|
|
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
|
|
|
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
|
|
|
|
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
|
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
|
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
|
|
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import sys, os
|
2006-04-23 00:47:58 -03:00
|
|
|
import types
|
2002-11-14 18:00:19 -04:00
|
|
|
import textwrap
|
2004-07-31 13:15:44 -03:00
|
|
|
|
|
|
|
def _repr(self):
|
|
|
|
return "<%s at 0x%x: %s>" % (self.__class__.__name__, id(self), self)
|
|
|
|
|
|
|
|
|
|
|
|
# This file was generated from:
|
2006-07-23 13:05:51 -03:00
|
|
|
# Id: option_parser.py 527 2006-07-23 15:21:30Z greg
|
2006-06-11 13:24:11 -03:00
|
|
|
# Id: option.py 522 2006-06-11 16:22:03Z gward
|
2006-07-23 13:05:51 -03:00
|
|
|
# Id: help.py 527 2006-07-23 15:21:30Z greg
|
2006-04-23 00:47:58 -03:00
|
|
|
# Id: errors.py 509 2006-04-20 00:58:24Z gward
|
|
|
|
|
|
|
|
try:
|
|
|
|
from gettext import gettext
|
|
|
|
except ImportError:
|
|
|
|
def gettext(message):
|
|
|
|
return message
|
|
|
|
_ = gettext
|
|
|
|
|
2002-11-14 18:00:19 -04:00
|
|
|
|
|
|
|
class OptParseError (Exception):
|
2004-07-31 13:15:44 -03:00
|
|
|
def __init__(self, msg):
|
2002-11-14 18:00:19 -04:00
|
|
|
self.msg = msg
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def __str__(self):
|
2002-11-14 18:00:19 -04:00
|
|
|
return self.msg
|
|
|
|
|
2003-04-20 23:40:34 -03:00
|
|
|
|
2002-11-14 18:00:19 -04:00
|
|
|
class OptionError (OptParseError):
|
|
|
|
"""
|
|
|
|
Raised if an Option instance is created with invalid or
|
|
|
|
inconsistent arguments.
|
|
|
|
"""
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def __init__(self, msg, option):
|
2002-11-14 18:00:19 -04:00
|
|
|
self.msg = msg
|
|
|
|
self.option_id = str(option)
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def __str__(self):
|
2002-11-14 18:00:19 -04:00
|
|
|
if self.option_id:
|
|
|
|
return "option %s: %s" % (self.option_id, self.msg)
|
|
|
|
else:
|
|
|
|
return self.msg
|
|
|
|
|
|
|
|
class OptionConflictError (OptionError):
|
|
|
|
"""
|
|
|
|
Raised if conflicting options are added to an OptionParser.
|
|
|
|
"""
|
|
|
|
|
|
|
|
class OptionValueError (OptParseError):
|
|
|
|
"""
|
|
|
|
Raised if an invalid option value is encountered on the command
|
|
|
|
line.
|
|
|
|
"""
|
|
|
|
|
|
|
|
class BadOptionError (OptParseError):
|
|
|
|
"""
|
2006-04-23 00:47:58 -03:00
|
|
|
Raised if an invalid option is seen on the command line.
|
|
|
|
"""
|
|
|
|
def __init__(self, opt_str):
|
|
|
|
self.opt_str = opt_str
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return _("no such option: %s") % self.opt_str
|
|
|
|
|
|
|
|
class AmbiguousOptionError (BadOptionError):
|
|
|
|
"""
|
|
|
|
Raised if an ambiguous option is seen on the command line.
|
2002-11-14 18:00:19 -04:00
|
|
|
"""
|
2006-04-23 00:47:58 -03:00
|
|
|
def __init__(self, opt_str, possibilities):
|
|
|
|
BadOptionError.__init__(self, opt_str)
|
|
|
|
self.possibilities = possibilities
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return (_("ambiguous option: %s (%s?)")
|
|
|
|
% (self.opt_str, ", ".join(self.possibilities)))
|
2003-04-20 23:40:34 -03:00
|
|
|
|
|
|
|
|
2002-11-14 18:00:19 -04:00
|
|
|
class HelpFormatter:
|
|
|
|
|
|
|
|
"""
|
|
|
|
Abstract base class for formatting option help. OptionParser
|
|
|
|
instances should use one of the HelpFormatter subclasses for
|
|
|
|
formatting help; by default IndentedHelpFormatter is used.
|
|
|
|
|
|
|
|
Instance attributes:
|
2004-07-31 13:15:44 -03:00
|
|
|
parser : OptionParser
|
|
|
|
the controlling OptionParser instance
|
2002-11-14 18:00:19 -04:00
|
|
|
indent_increment : int
|
|
|
|
the number of columns to indent per nesting level
|
|
|
|
max_help_position : int
|
|
|
|
the maximum starting column for option help text
|
|
|
|
help_position : int
|
|
|
|
the calculated starting column for option help text;
|
|
|
|
initially the same as the maximum
|
|
|
|
width : int
|
2004-07-31 13:15:44 -03:00
|
|
|
total number of columns for output (pass None to constructor for
|
|
|
|
this value to be taken from the $COLUMNS environment variable)
|
2002-11-14 18:00:19 -04:00
|
|
|
level : int
|
|
|
|
current indentation level
|
|
|
|
current_indent : int
|
|
|
|
current indentation level (in columns)
|
|
|
|
help_width : int
|
|
|
|
number of columns available for option help text (calculated)
|
2004-07-31 13:15:44 -03:00
|
|
|
default_tag : str
|
|
|
|
text to replace with each option's default value, "%default"
|
|
|
|
by default. Set to false value to disable default value expansion.
|
|
|
|
option_strings : { Option : str }
|
|
|
|
maps Option instances to the snippet of help text explaining
|
|
|
|
the syntax of that option, e.g. "-h, --help" or
|
|
|
|
"-fFILE, --file=FILE"
|
|
|
|
_short_opt_fmt : str
|
|
|
|
format string controlling how short options with values are
|
|
|
|
printed in help text. Must be either "%s%s" ("-fFILE") or
|
|
|
|
"%s %s" ("-f FILE"), because those are the two syntaxes that
|
|
|
|
Optik supports.
|
|
|
|
_long_opt_fmt : str
|
|
|
|
similar but for long options; must be either "%s %s" ("--file FILE")
|
|
|
|
or "%s=%s" ("--file=FILE").
|
2002-11-14 18:00:19 -04:00
|
|
|
"""
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
NO_DEFAULT_VALUE = "none"
|
|
|
|
|
|
|
|
def __init__(self,
|
|
|
|
indent_increment,
|
|
|
|
max_help_position,
|
|
|
|
width,
|
|
|
|
short_first):
|
|
|
|
self.parser = None
|
2002-11-14 18:00:19 -04:00
|
|
|
self.indent_increment = indent_increment
|
|
|
|
self.help_position = self.max_help_position = max_help_position
|
2004-07-31 13:15:44 -03:00
|
|
|
if width is None:
|
|
|
|
try:
|
|
|
|
width = int(os.environ['COLUMNS'])
|
|
|
|
except (KeyError, ValueError):
|
|
|
|
width = 80
|
|
|
|
width -= 2
|
2002-11-14 18:00:19 -04:00
|
|
|
self.width = width
|
|
|
|
self.current_indent = 0
|
|
|
|
self.level = 0
|
2004-07-31 13:15:44 -03:00
|
|
|
self.help_width = None # computed later
|
2003-04-20 23:40:34 -03:00
|
|
|
self.short_first = short_first
|
2004-07-31 13:15:44 -03:00
|
|
|
self.default_tag = "%default"
|
|
|
|
self.option_strings = {}
|
|
|
|
self._short_opt_fmt = "%s %s"
|
|
|
|
self._long_opt_fmt = "%s=%s"
|
|
|
|
|
|
|
|
def set_parser(self, parser):
|
|
|
|
self.parser = parser
|
|
|
|
|
|
|
|
def set_short_opt_delimiter(self, delim):
|
|
|
|
if delim not in ("", " "):
|
|
|
|
raise ValueError(
|
|
|
|
"invalid metavar delimiter for short options: %r" % delim)
|
|
|
|
self._short_opt_fmt = "%s" + delim + "%s"
|
2002-11-14 18:00:19 -04:00
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def set_long_opt_delimiter(self, delim):
|
|
|
|
if delim not in ("=", " "):
|
|
|
|
raise ValueError(
|
|
|
|
"invalid metavar delimiter for long options: %r" % delim)
|
|
|
|
self._long_opt_fmt = "%s" + delim + "%s"
|
|
|
|
|
|
|
|
def indent(self):
|
2002-11-14 18:00:19 -04:00
|
|
|
self.current_indent += self.indent_increment
|
|
|
|
self.level += 1
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def dedent(self):
|
2002-11-14 18:00:19 -04:00
|
|
|
self.current_indent -= self.indent_increment
|
|
|
|
assert self.current_indent >= 0, "Indent decreased below 0."
|
|
|
|
self.level -= 1
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def format_usage(self, usage):
|
2002-11-14 18:00:19 -04:00
|
|
|
raise NotImplementedError, "subclasses must implement"
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def format_heading(self, heading):
|
2002-11-14 18:00:19 -04:00
|
|
|
raise NotImplementedError, "subclasses must implement"
|
|
|
|
|
2006-04-23 00:47:58 -03:00
|
|
|
def _format_text(self, text):
|
|
|
|
"""
|
|
|
|
Format a paragraph of free-form text for inclusion in the
|
|
|
|
help output at the current indentation level.
|
|
|
|
"""
|
|
|
|
text_width = self.width - self.current_indent
|
2002-11-14 18:00:19 -04:00
|
|
|
indent = " "*self.current_indent
|
2006-04-23 00:47:58 -03:00
|
|
|
return textwrap.fill(text,
|
|
|
|
text_width,
|
2002-11-14 18:00:19 -04:00
|
|
|
initial_indent=indent,
|
2006-04-23 00:47:58 -03:00
|
|
|
subsequent_indent=indent)
|
2006-06-11 16:42:51 -03:00
|
|
|
|
2006-04-23 00:47:58 -03:00
|
|
|
def format_description(self, description):
|
|
|
|
if description:
|
|
|
|
return self._format_text(description) + "\n"
|
|
|
|
else:
|
|
|
|
return ""
|
|
|
|
|
|
|
|
def format_epilog(self, epilog):
|
|
|
|
if epilog:
|
|
|
|
return "\n" + self._format_text(epilog) + "\n"
|
|
|
|
else:
|
|
|
|
return ""
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
|
|
|
|
def expand_default(self, option):
|
|
|
|
if self.parser is None or not self.default_tag:
|
|
|
|
return option.help
|
|
|
|
|
|
|
|
default_value = self.parser.defaults.get(option.dest)
|
|
|
|
if default_value is NO_DEFAULT or default_value is None:
|
|
|
|
default_value = self.NO_DEFAULT_VALUE
|
2002-11-14 18:00:19 -04:00
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
return option.help.replace(self.default_tag, str(default_value))
|
|
|
|
|
|
|
|
def format_option(self, option):
|
2002-11-14 18:00:19 -04:00
|
|
|
# The help for each option consists of two parts:
|
|
|
|
# * the opt strings and metavars
|
|
|
|
# eg. ("-x", or "-fFILENAME, --file=FILENAME")
|
|
|
|
# * the user-supplied help string
|
|
|
|
# eg. ("turn on expert mode", "read data from FILENAME")
|
|
|
|
#
|
|
|
|
# If possible, we write both of these on the same line:
|
|
|
|
# -x turn on expert mode
|
|
|
|
#
|
|
|
|
# But if the opt string list is too long, we put the help
|
|
|
|
# string on a second line, indented to the same column it would
|
|
|
|
# start in if it fit on the first line.
|
|
|
|
# -fFILENAME, --file=FILENAME
|
|
|
|
# read data from FILENAME
|
|
|
|
result = []
|
2004-07-31 13:15:44 -03:00
|
|
|
opts = self.option_strings[option]
|
2002-11-14 18:00:19 -04:00
|
|
|
opt_width = self.help_position - self.current_indent - 2
|
|
|
|
if len(opts) > opt_width:
|
|
|
|
opts = "%*s%s\n" % (self.current_indent, "", opts)
|
|
|
|
indent_first = self.help_position
|
|
|
|
else: # start help on same line as opts
|
|
|
|
opts = "%*s%-*s " % (self.current_indent, "", opt_width, opts)
|
|
|
|
indent_first = 0
|
|
|
|
result.append(opts)
|
|
|
|
if option.help:
|
2004-07-31 13:15:44 -03:00
|
|
|
help_text = self.expand_default(option)
|
|
|
|
help_lines = textwrap.wrap(help_text, self.help_width)
|
2002-11-14 18:00:19 -04:00
|
|
|
result.append("%*s%s\n" % (indent_first, "", help_lines[0]))
|
|
|
|
result.extend(["%*s%s\n" % (self.help_position, "", line)
|
|
|
|
for line in help_lines[1:]])
|
|
|
|
elif opts[-1] != "\n":
|
|
|
|
result.append("\n")
|
|
|
|
return "".join(result)
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def store_option_strings(self, parser):
|
2002-11-14 18:00:19 -04:00
|
|
|
self.indent()
|
|
|
|
max_len = 0
|
|
|
|
for opt in parser.option_list:
|
|
|
|
strings = self.format_option_strings(opt)
|
2004-07-31 13:15:44 -03:00
|
|
|
self.option_strings[opt] = strings
|
2002-11-14 18:00:19 -04:00
|
|
|
max_len = max(max_len, len(strings) + self.current_indent)
|
|
|
|
self.indent()
|
|
|
|
for group in parser.option_groups:
|
|
|
|
for opt in group.option_list:
|
|
|
|
strings = self.format_option_strings(opt)
|
2004-07-31 13:15:44 -03:00
|
|
|
self.option_strings[opt] = strings
|
2002-11-14 18:00:19 -04:00
|
|
|
max_len = max(max_len, len(strings) + self.current_indent)
|
|
|
|
self.dedent()
|
|
|
|
self.dedent()
|
|
|
|
self.help_position = min(max_len + 2, self.max_help_position)
|
2004-07-31 13:15:44 -03:00
|
|
|
self.help_width = self.width - self.help_position
|
2002-11-14 18:00:19 -04:00
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def format_option_strings(self, option):
|
2002-11-14 18:00:19 -04:00
|
|
|
"""Return a comma-separated list of option strings & metavariables."""
|
2003-04-20 23:40:34 -03:00
|
|
|
if option.takes_value():
|
2002-11-14 18:00:19 -04:00
|
|
|
metavar = option.metavar or option.dest.upper()
|
2004-07-31 13:15:44 -03:00
|
|
|
short_opts = [self._short_opt_fmt % (sopt, metavar)
|
|
|
|
for sopt in option._short_opts]
|
|
|
|
long_opts = [self._long_opt_fmt % (lopt, metavar)
|
|
|
|
for lopt in option._long_opts]
|
2002-11-14 18:00:19 -04:00
|
|
|
else:
|
2003-04-20 23:40:34 -03:00
|
|
|
short_opts = option._short_opts
|
|
|
|
long_opts = option._long_opts
|
2002-11-14 18:00:19 -04:00
|
|
|
|
2003-04-20 23:40:34 -03:00
|
|
|
if self.short_first:
|
|
|
|
opts = short_opts + long_opts
|
2002-11-14 18:00:19 -04:00
|
|
|
else:
|
2003-04-20 23:40:34 -03:00
|
|
|
opts = long_opts + short_opts
|
2002-11-14 18:00:19 -04:00
|
|
|
|
2003-04-20 23:40:34 -03:00
|
|
|
return ", ".join(opts)
|
2002-11-14 18:00:19 -04:00
|
|
|
|
|
|
|
class IndentedHelpFormatter (HelpFormatter):
|
|
|
|
"""Format help with indented section bodies.
|
|
|
|
"""
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def __init__(self,
|
|
|
|
indent_increment=2,
|
|
|
|
max_help_position=24,
|
|
|
|
width=None,
|
|
|
|
short_first=1):
|
2002-11-14 18:00:19 -04:00
|
|
|
HelpFormatter.__init__(
|
|
|
|
self, indent_increment, max_help_position, width, short_first)
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def format_usage(self, usage):
|
2006-04-23 00:47:58 -03:00
|
|
|
return _("Usage: %s\n") % usage
|
2002-11-14 18:00:19 -04:00
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def format_heading(self, heading):
|
2002-11-14 18:00:19 -04:00
|
|
|
return "%*s%s:\n" % (self.current_indent, "", heading)
|
|
|
|
|
|
|
|
|
|
|
|
class TitledHelpFormatter (HelpFormatter):
|
|
|
|
"""Format help with underlined section headers.
|
|
|
|
"""
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def __init__(self,
|
|
|
|
indent_increment=0,
|
|
|
|
max_help_position=24,
|
|
|
|
width=None,
|
|
|
|
short_first=0):
|
2002-11-14 18:00:19 -04:00
|
|
|
HelpFormatter.__init__ (
|
|
|
|
self, indent_increment, max_help_position, width, short_first)
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def format_usage(self, usage):
|
|
|
|
return "%s %s\n" % (self.format_heading(_("Usage")), usage)
|
2002-11-14 18:00:19 -04:00
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def format_heading(self, heading):
|
2002-11-14 18:00:19 -04:00
|
|
|
return "%s\n%s\n" % (heading, "=-"[self.level] * len(heading))
|
2003-04-20 23:40:34 -03:00
|
|
|
|
|
|
|
|
2006-04-23 00:47:58 -03:00
|
|
|
def _parse_num(val, type):
|
|
|
|
if val[:2].lower() == "0x": # hexadecimal
|
|
|
|
radix = 16
|
|
|
|
elif val[:2].lower() == "0b": # binary
|
|
|
|
radix = 2
|
|
|
|
val = val[2:] or "0" # have to remove "0b" prefix
|
|
|
|
elif val[:1] == "0": # octal
|
|
|
|
radix = 8
|
|
|
|
else: # decimal
|
|
|
|
radix = 10
|
|
|
|
|
|
|
|
return type(val, radix)
|
|
|
|
|
|
|
|
def _parse_int(val):
|
|
|
|
return _parse_num(val, int)
|
|
|
|
|
|
|
|
def _parse_long(val):
|
|
|
|
return _parse_num(val, long)
|
|
|
|
|
|
|
|
_builtin_cvt = { "int" : (_parse_int, _("integer")),
|
|
|
|
"long" : (_parse_long, _("long integer")),
|
2004-07-31 13:15:44 -03:00
|
|
|
"float" : (float, _("floating-point")),
|
|
|
|
"complex" : (complex, _("complex")) }
|
2002-11-14 18:00:19 -04:00
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def check_builtin(option, opt, value):
|
2002-11-14 18:00:19 -04:00
|
|
|
(cvt, what) = _builtin_cvt[option.type]
|
|
|
|
try:
|
|
|
|
return cvt(value)
|
|
|
|
except ValueError:
|
|
|
|
raise OptionValueError(
|
2004-07-31 13:15:44 -03:00
|
|
|
_("option %s: invalid %s value: %r") % (opt, what, value))
|
2002-11-14 18:00:19 -04:00
|
|
|
|
|
|
|
def check_choice(option, opt, value):
|
|
|
|
if value in option.choices:
|
|
|
|
return value
|
|
|
|
else:
|
|
|
|
choices = ", ".join(map(repr, option.choices))
|
|
|
|
raise OptionValueError(
|
2004-07-31 13:15:44 -03:00
|
|
|
_("option %s: invalid choice: %r (choose from %s)")
|
2002-11-14 18:00:19 -04:00
|
|
|
% (opt, value, choices))
|
|
|
|
|
|
|
|
# Not supplying a default is different from a default of None,
|
|
|
|
# so we need an explicit "not supplied" value.
|
2004-07-31 13:15:44 -03:00
|
|
|
NO_DEFAULT = ("NO", "DEFAULT")
|
2002-11-14 18:00:19 -04:00
|
|
|
|
|
|
|
|
|
|
|
class Option:
|
|
|
|
"""
|
|
|
|
Instance attributes:
|
|
|
|
_short_opts : [string]
|
|
|
|
_long_opts : [string]
|
|
|
|
|
|
|
|
action : string
|
|
|
|
type : string
|
|
|
|
dest : string
|
|
|
|
default : any
|
|
|
|
nargs : int
|
|
|
|
const : any
|
|
|
|
choices : [string]
|
|
|
|
callback : function
|
|
|
|
callback_args : (any*)
|
|
|
|
callback_kwargs : { string : any }
|
|
|
|
help : string
|
|
|
|
metavar : string
|
|
|
|
"""
|
|
|
|
|
|
|
|
# The list of instance attributes that may be set through
|
|
|
|
# keyword args to the constructor.
|
|
|
|
ATTRS = ['action',
|
|
|
|
'type',
|
|
|
|
'dest',
|
|
|
|
'default',
|
|
|
|
'nargs',
|
|
|
|
'const',
|
|
|
|
'choices',
|
|
|
|
'callback',
|
|
|
|
'callback_args',
|
|
|
|
'callback_kwargs',
|
|
|
|
'help',
|
|
|
|
'metavar']
|
|
|
|
|
|
|
|
# The set of actions allowed by option parsers. Explicitly listed
|
|
|
|
# here so the constructor can validate its arguments.
|
|
|
|
ACTIONS = ("store",
|
|
|
|
"store_const",
|
|
|
|
"store_true",
|
|
|
|
"store_false",
|
|
|
|
"append",
|
2006-04-23 00:47:58 -03:00
|
|
|
"append_const",
|
2002-11-14 18:00:19 -04:00
|
|
|
"count",
|
|
|
|
"callback",
|
|
|
|
"help",
|
|
|
|
"version")
|
|
|
|
|
|
|
|
# The set of actions that involve storing a value somewhere;
|
|
|
|
# also listed just for constructor argument validation. (If
|
|
|
|
# the action is one of these, there must be a destination.)
|
|
|
|
STORE_ACTIONS = ("store",
|
|
|
|
"store_const",
|
|
|
|
"store_true",
|
|
|
|
"store_false",
|
|
|
|
"append",
|
2006-04-23 00:47:58 -03:00
|
|
|
"append_const",
|
2002-11-14 18:00:19 -04:00
|
|
|
"count")
|
|
|
|
|
|
|
|
# The set of actions for which it makes sense to supply a value
|
2004-10-26 23:20:04 -03:00
|
|
|
# type, ie. which may consume an argument from the command line.
|
2002-11-14 18:00:19 -04:00
|
|
|
TYPED_ACTIONS = ("store",
|
|
|
|
"append",
|
|
|
|
"callback")
|
|
|
|
|
2004-10-26 23:20:04 -03:00
|
|
|
# The set of actions which *require* a value type, ie. that
|
|
|
|
# always consume an argument from the command line.
|
|
|
|
ALWAYS_TYPED_ACTIONS = ("store",
|
|
|
|
"append")
|
|
|
|
|
2006-04-23 00:47:58 -03:00
|
|
|
# The set of actions which take a 'const' attribute.
|
|
|
|
CONST_ACTIONS = ("store_const",
|
|
|
|
"append_const")
|
|
|
|
|
2002-11-14 18:00:19 -04:00
|
|
|
# The set of known types for option parsers. Again, listed here for
|
|
|
|
# constructor argument validation.
|
|
|
|
TYPES = ("string", "int", "long", "float", "complex", "choice")
|
|
|
|
|
|
|
|
# Dictionary of argument checking functions, which convert and
|
|
|
|
# validate option arguments according to the option type.
|
|
|
|
#
|
|
|
|
# Signature of checking functions is:
|
|
|
|
# check(option : Option, opt : string, value : string) -> any
|
|
|
|
# where
|
|
|
|
# option is the Option instance calling the checker
|
|
|
|
# opt is the actual option seen on the command-line
|
|
|
|
# (eg. "-a", "--file")
|
|
|
|
# value is the option argument seen on the command-line
|
|
|
|
#
|
|
|
|
# The return value should be in the appropriate Python type
|
|
|
|
# for option.type -- eg. an integer if option.type == "int".
|
|
|
|
#
|
|
|
|
# If no checker is defined for a type, arguments will be
|
|
|
|
# unchecked and remain strings.
|
|
|
|
TYPE_CHECKER = { "int" : check_builtin,
|
|
|
|
"long" : check_builtin,
|
|
|
|
"float" : check_builtin,
|
2004-07-31 13:15:44 -03:00
|
|
|
"complex": check_builtin,
|
2002-11-14 18:00:19 -04:00
|
|
|
"choice" : check_choice,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# CHECK_METHODS is a list of unbound method objects; they are called
|
|
|
|
# by the constructor, in order, after all attributes are
|
|
|
|
# initialized. The list is created and filled in later, after all
|
|
|
|
# the methods are actually defined. (I just put it here because I
|
|
|
|
# like to define and document all class attributes in the same
|
|
|
|
# place.) Subclasses that add another _check_*() method should
|
|
|
|
# define their own CHECK_METHODS list that adds their check method
|
|
|
|
# to those from this class.
|
|
|
|
CHECK_METHODS = None
|
|
|
|
|
|
|
|
|
|
|
|
# -- Constructor/initialization methods ----------------------------
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def __init__(self, *opts, **attrs):
|
2003-04-20 23:40:34 -03:00
|
|
|
# Set _short_opts, _long_opts attrs from 'opts' tuple.
|
|
|
|
# Have to be set now, in case no option strings are supplied.
|
|
|
|
self._short_opts = []
|
|
|
|
self._long_opts = []
|
2002-11-14 18:00:19 -04:00
|
|
|
opts = self._check_opt_strings(opts)
|
|
|
|
self._set_opt_strings(opts)
|
|
|
|
|
|
|
|
# Set all other attrs (action, type, etc.) from 'attrs' dict
|
|
|
|
self._set_attrs(attrs)
|
|
|
|
|
|
|
|
# Check all the attributes we just set. There are lots of
|
|
|
|
# complicated interdependencies, but luckily they can be farmed
|
|
|
|
# out to the _check_*() methods listed in CHECK_METHODS -- which
|
|
|
|
# could be handy for subclasses! The one thing these all share
|
|
|
|
# is that they raise OptionError if they discover a problem.
|
|
|
|
for checker in self.CHECK_METHODS:
|
|
|
|
checker(self)
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def _check_opt_strings(self, opts):
|
2002-11-14 18:00:19 -04:00
|
|
|
# Filter out None because early versions of Optik had exactly
|
|
|
|
# one short option and one long option, either of which
|
|
|
|
# could be None.
|
|
|
|
opts = filter(None, opts)
|
|
|
|
if not opts:
|
2003-04-20 23:40:34 -03:00
|
|
|
raise TypeError("at least one option string must be supplied")
|
2002-11-14 18:00:19 -04:00
|
|
|
return opts
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def _set_opt_strings(self, opts):
|
2002-11-14 18:00:19 -04:00
|
|
|
for opt in opts:
|
|
|
|
if len(opt) < 2:
|
|
|
|
raise OptionError(
|
|
|
|
"invalid option string %r: "
|
|
|
|
"must be at least two characters long" % opt, self)
|
|
|
|
elif len(opt) == 2:
|
|
|
|
if not (opt[0] == "-" and opt[1] != "-"):
|
|
|
|
raise OptionError(
|
|
|
|
"invalid short option string %r: "
|
|
|
|
"must be of the form -x, (x any non-dash char)" % opt,
|
|
|
|
self)
|
|
|
|
self._short_opts.append(opt)
|
|
|
|
else:
|
|
|
|
if not (opt[0:2] == "--" and opt[2] != "-"):
|
|
|
|
raise OptionError(
|
|
|
|
"invalid long option string %r: "
|
|
|
|
"must start with --, followed by non-dash" % opt,
|
|
|
|
self)
|
|
|
|
self._long_opts.append(opt)
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def _set_attrs(self, attrs):
|
2002-11-14 18:00:19 -04:00
|
|
|
for attr in self.ATTRS:
|
2008-07-10 12:37:08 -03:00
|
|
|
if attr in attrs:
|
2002-11-14 18:00:19 -04:00
|
|
|
setattr(self, attr, attrs[attr])
|
|
|
|
del attrs[attr]
|
|
|
|
else:
|
|
|
|
if attr == 'default':
|
|
|
|
setattr(self, attr, NO_DEFAULT)
|
|
|
|
else:
|
|
|
|
setattr(self, attr, None)
|
|
|
|
if attrs:
|
2006-05-28 16:13:17 -03:00
|
|
|
attrs = attrs.keys()
|
|
|
|
attrs.sort()
|
2002-11-14 18:00:19 -04:00
|
|
|
raise OptionError(
|
2006-05-28 16:13:17 -03:00
|
|
|
"invalid keyword arguments: %s" % ", ".join(attrs),
|
2002-11-14 18:00:19 -04:00
|
|
|
self)
|
|
|
|
|
|
|
|
|
|
|
|
# -- Constructor validation methods --------------------------------
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def _check_action(self):
|
2002-11-14 18:00:19 -04:00
|
|
|
if self.action is None:
|
|
|
|
self.action = "store"
|
|
|
|
elif self.action not in self.ACTIONS:
|
|
|
|
raise OptionError("invalid action: %r" % self.action, self)
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def _check_type(self):
|
2002-11-14 18:00:19 -04:00
|
|
|
if self.type is None:
|
2004-10-26 23:20:04 -03:00
|
|
|
if self.action in self.ALWAYS_TYPED_ACTIONS:
|
2002-11-14 18:00:19 -04:00
|
|
|
if self.choices is not None:
|
|
|
|
# The "choices" attribute implies "choice" type.
|
|
|
|
self.type = "choice"
|
|
|
|
else:
|
|
|
|
# No type given? "string" is the most sensible default.
|
|
|
|
self.type = "string"
|
|
|
|
else:
|
2006-04-23 00:47:58 -03:00
|
|
|
# Allow type objects or builtin type conversion functions
|
|
|
|
# (int, str, etc.) as an alternative to their names. (The
|
|
|
|
# complicated check of __builtin__ is only necessary for
|
|
|
|
# Python 2.1 and earlier, and is short-circuited by the
|
|
|
|
# first check on modern Pythons.)
|
|
|
|
import __builtin__
|
|
|
|
if ( type(self.type) is types.TypeType or
|
|
|
|
(hasattr(self.type, "__name__") and
|
|
|
|
getattr(__builtin__, self.type.__name__, None) is self.type) ):
|
2004-07-31 13:15:44 -03:00
|
|
|
self.type = self.type.__name__
|
2006-04-23 00:47:58 -03:00
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
if self.type == "str":
|
|
|
|
self.type = "string"
|
|
|
|
|
2002-11-14 18:00:19 -04:00
|
|
|
if self.type not in self.TYPES:
|
|
|
|
raise OptionError("invalid option type: %r" % self.type, self)
|
|
|
|
if self.action not in self.TYPED_ACTIONS:
|
|
|
|
raise OptionError(
|
|
|
|
"must not supply a type for action %r" % self.action, self)
|
|
|
|
|
|
|
|
def _check_choice(self):
|
|
|
|
if self.type == "choice":
|
|
|
|
if self.choices is None:
|
|
|
|
raise OptionError(
|
|
|
|
"must supply a list of choices for type 'choice'", self)
|
2006-04-23 00:47:58 -03:00
|
|
|
elif type(self.choices) not in (types.TupleType, types.ListType):
|
2002-11-14 18:00:19 -04:00
|
|
|
raise OptionError(
|
|
|
|
"choices must be a list of strings ('%s' supplied)"
|
|
|
|
% str(type(self.choices)).split("'")[1], self)
|
|
|
|
elif self.choices is not None:
|
|
|
|
raise OptionError(
|
|
|
|
"must not supply choices for type %r" % self.type, self)
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def _check_dest(self):
|
|
|
|
# No destination given, and we need one for this action. The
|
|
|
|
# self.type check is for callbacks that take a value.
|
|
|
|
takes_value = (self.action in self.STORE_ACTIONS or
|
|
|
|
self.type is not None)
|
|
|
|
if self.dest is None and takes_value:
|
|
|
|
|
2002-11-14 18:00:19 -04:00
|
|
|
# Glean a destination from the first long option string,
|
|
|
|
# or from the first short option string if no long options.
|
|
|
|
if self._long_opts:
|
|
|
|
# eg. "--foo-bar" -> "foo_bar"
|
|
|
|
self.dest = self._long_opts[0][2:].replace('-', '_')
|
|
|
|
else:
|
|
|
|
self.dest = self._short_opts[0][1]
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def _check_const(self):
|
2006-04-23 00:47:58 -03:00
|
|
|
if self.action not in self.CONST_ACTIONS and self.const is not None:
|
2002-11-14 18:00:19 -04:00
|
|
|
raise OptionError(
|
|
|
|
"'const' must not be supplied for action %r" % self.action,
|
|
|
|
self)
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def _check_nargs(self):
|
2002-11-14 18:00:19 -04:00
|
|
|
if self.action in self.TYPED_ACTIONS:
|
|
|
|
if self.nargs is None:
|
|
|
|
self.nargs = 1
|
|
|
|
elif self.nargs is not None:
|
|
|
|
raise OptionError(
|
|
|
|
"'nargs' must not be supplied for action %r" % self.action,
|
|
|
|
self)
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def _check_callback(self):
|
2002-11-14 18:00:19 -04:00
|
|
|
if self.action == "callback":
|
2008-07-10 12:37:08 -03:00
|
|
|
if not hasattr(self.callback, '__call__'):
|
2002-11-14 18:00:19 -04:00
|
|
|
raise OptionError(
|
|
|
|
"callback not callable: %r" % self.callback, self)
|
|
|
|
if (self.callback_args is not None and
|
2006-04-23 00:47:58 -03:00
|
|
|
type(self.callback_args) is not types.TupleType):
|
2002-11-14 18:00:19 -04:00
|
|
|
raise OptionError(
|
|
|
|
"callback_args, if supplied, must be a tuple: not %r"
|
|
|
|
% self.callback_args, self)
|
|
|
|
if (self.callback_kwargs is not None and
|
2006-04-23 00:47:58 -03:00
|
|
|
type(self.callback_kwargs) is not types.DictType):
|
2002-11-14 18:00:19 -04:00
|
|
|
raise OptionError(
|
|
|
|
"callback_kwargs, if supplied, must be a dict: not %r"
|
|
|
|
% self.callback_kwargs, self)
|
|
|
|
else:
|
|
|
|
if self.callback is not None:
|
|
|
|
raise OptionError(
|
|
|
|
"callback supplied (%r) for non-callback option"
|
|
|
|
% self.callback, self)
|
|
|
|
if self.callback_args is not None:
|
|
|
|
raise OptionError(
|
|
|
|
"callback_args supplied for non-callback option", self)
|
|
|
|
if self.callback_kwargs is not None:
|
|
|
|
raise OptionError(
|
|
|
|
"callback_kwargs supplied for non-callback option", self)
|
|
|
|
|
|
|
|
|
|
|
|
CHECK_METHODS = [_check_action,
|
|
|
|
_check_type,
|
|
|
|
_check_choice,
|
|
|
|
_check_dest,
|
|
|
|
_check_const,
|
|
|
|
_check_nargs,
|
|
|
|
_check_callback]
|
|
|
|
|
|
|
|
|
|
|
|
# -- Miscellaneous methods -----------------------------------------
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def __str__(self):
|
2003-04-20 23:40:34 -03:00
|
|
|
return "/".join(self._short_opts + self._long_opts)
|
2002-11-14 18:00:19 -04:00
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
__repr__ = _repr
|
|
|
|
|
|
|
|
def takes_value(self):
|
2002-11-14 18:00:19 -04:00
|
|
|
return self.type is not None
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def get_opt_string(self):
|
|
|
|
if self._long_opts:
|
|
|
|
return self._long_opts[0]
|
|
|
|
else:
|
|
|
|
return self._short_opts[0]
|
|
|
|
|
2002-11-14 18:00:19 -04:00
|
|
|
|
|
|
|
# -- Processing methods --------------------------------------------
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def check_value(self, opt, value):
|
2002-11-14 18:00:19 -04:00
|
|
|
checker = self.TYPE_CHECKER.get(self.type)
|
|
|
|
if checker is None:
|
|
|
|
return value
|
|
|
|
else:
|
|
|
|
return checker(self, opt, value)
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def convert_value(self, opt, value):
|
2002-11-14 18:00:19 -04:00
|
|
|
if value is not None:
|
|
|
|
if self.nargs == 1:
|
2004-07-31 13:15:44 -03:00
|
|
|
return self.check_value(opt, value)
|
2002-11-14 18:00:19 -04:00
|
|
|
else:
|
2004-07-31 13:15:44 -03:00
|
|
|
return tuple([self.check_value(opt, v) for v in value])
|
|
|
|
|
|
|
|
def process(self, opt, value, values, parser):
|
|
|
|
|
|
|
|
# First, convert the value(s) to the right type. Howl if any
|
|
|
|
# value(s) are bogus.
|
|
|
|
value = self.convert_value(opt, value)
|
2002-11-14 18:00:19 -04:00
|
|
|
|
|
|
|
# And then take whatever action is expected of us.
|
|
|
|
# This is a separate method to make life easier for
|
|
|
|
# subclasses to add new actions.
|
|
|
|
return self.take_action(
|
|
|
|
self.action, self.dest, opt, value, values, parser)
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def take_action(self, action, dest, opt, value, values, parser):
|
2002-11-14 18:00:19 -04:00
|
|
|
if action == "store":
|
|
|
|
setattr(values, dest, value)
|
|
|
|
elif action == "store_const":
|
|
|
|
setattr(values, dest, self.const)
|
|
|
|
elif action == "store_true":
|
2003-04-20 23:40:34 -03:00
|
|
|
setattr(values, dest, True)
|
2002-11-14 18:00:19 -04:00
|
|
|
elif action == "store_false":
|
2003-04-20 23:40:34 -03:00
|
|
|
setattr(values, dest, False)
|
2002-11-14 18:00:19 -04:00
|
|
|
elif action == "append":
|
|
|
|
values.ensure_value(dest, []).append(value)
|
2006-04-23 00:47:58 -03:00
|
|
|
elif action == "append_const":
|
|
|
|
values.ensure_value(dest, []).append(self.const)
|
2002-11-14 18:00:19 -04:00
|
|
|
elif action == "count":
|
|
|
|
setattr(values, dest, values.ensure_value(dest, 0) + 1)
|
|
|
|
elif action == "callback":
|
|
|
|
args = self.callback_args or ()
|
|
|
|
kwargs = self.callback_kwargs or {}
|
|
|
|
self.callback(self, opt, value, parser, *args, **kwargs)
|
|
|
|
elif action == "help":
|
|
|
|
parser.print_help()
|
2004-10-26 23:20:04 -03:00
|
|
|
parser.exit()
|
2002-11-14 18:00:19 -04:00
|
|
|
elif action == "version":
|
|
|
|
parser.print_version()
|
2004-10-26 23:20:04 -03:00
|
|
|
parser.exit()
|
2002-11-14 18:00:19 -04:00
|
|
|
else:
|
|
|
|
raise RuntimeError, "unknown action %r" % self.action
|
|
|
|
|
|
|
|
return 1
|
|
|
|
|
|
|
|
# class Option
|
2003-04-20 23:40:34 -03:00
|
|
|
|
|
|
|
|
2002-11-14 18:00:19 -04:00
|
|
|
SUPPRESS_HELP = "SUPPRESS"+"HELP"
|
|
|
|
SUPPRESS_USAGE = "SUPPRESS"+"USAGE"
|
|
|
|
|
2008-01-27 15:01:45 -04:00
|
|
|
try:
|
|
|
|
basestring
|
|
|
|
except NameError:
|
|
|
|
def isbasestring(x):
|
|
|
|
return isinstance(x, (types.StringType, types.UnicodeType))
|
|
|
|
else:
|
|
|
|
def isbasestring(x):
|
2008-01-23 10:20:50 -04:00
|
|
|
return isinstance(x, basestring)
|
2002-11-14 18:00:19 -04:00
|
|
|
|
|
|
|
class Values:
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def __init__(self, defaults=None):
|
2002-11-14 18:00:19 -04:00
|
|
|
if defaults:
|
|
|
|
for (attr, val) in defaults.items():
|
|
|
|
setattr(self, attr, val)
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def __str__(self):
|
|
|
|
return str(self.__dict__)
|
|
|
|
|
|
|
|
__repr__ = _repr
|
2002-11-14 18:00:19 -04:00
|
|
|
|
2006-04-23 00:47:58 -03:00
|
|
|
def __cmp__(self, other):
|
2004-07-31 13:15:44 -03:00
|
|
|
if isinstance(other, Values):
|
2006-04-23 00:47:58 -03:00
|
|
|
return cmp(self.__dict__, other.__dict__)
|
|
|
|
elif isinstance(other, types.DictType):
|
|
|
|
return cmp(self.__dict__, other)
|
2004-07-31 13:15:44 -03:00
|
|
|
else:
|
2006-04-23 00:47:58 -03:00
|
|
|
return -1
|
2004-07-31 13:15:44 -03:00
|
|
|
|
|
|
|
def _update_careful(self, dict):
|
2002-11-14 18:00:19 -04:00
|
|
|
"""
|
|
|
|
Update the option values from an arbitrary dictionary, but only
|
|
|
|
use keys from dict that already have a corresponding attribute
|
|
|
|
in self. Any keys in dict without a corresponding attribute
|
|
|
|
are silently ignored.
|
|
|
|
"""
|
|
|
|
for attr in dir(self):
|
2008-07-10 12:37:08 -03:00
|
|
|
if attr in dict:
|
2002-11-14 18:00:19 -04:00
|
|
|
dval = dict[attr]
|
|
|
|
if dval is not None:
|
|
|
|
setattr(self, attr, dval)
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def _update_loose(self, dict):
|
2002-11-14 18:00:19 -04:00
|
|
|
"""
|
|
|
|
Update the option values from an arbitrary dictionary,
|
|
|
|
using all keys from the dictionary regardless of whether
|
|
|
|
they have a corresponding attribute in self or not.
|
|
|
|
"""
|
|
|
|
self.__dict__.update(dict)
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def _update(self, dict, mode):
|
2002-11-14 18:00:19 -04:00
|
|
|
if mode == "careful":
|
|
|
|
self._update_careful(dict)
|
|
|
|
elif mode == "loose":
|
|
|
|
self._update_loose(dict)
|
|
|
|
else:
|
|
|
|
raise ValueError, "invalid update mode: %r" % mode
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def read_module(self, modname, mode="careful"):
|
2002-11-14 18:00:19 -04:00
|
|
|
__import__(modname)
|
|
|
|
mod = sys.modules[modname]
|
|
|
|
self._update(vars(mod), mode)
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def read_file(self, filename, mode="careful"):
|
2002-11-14 18:00:19 -04:00
|
|
|
vars = {}
|
|
|
|
execfile(filename, vars)
|
|
|
|
self._update(vars, mode)
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def ensure_value(self, attr, value):
|
2002-11-14 18:00:19 -04:00
|
|
|
if not hasattr(self, attr) or getattr(self, attr) is None:
|
|
|
|
setattr(self, attr, value)
|
|
|
|
return getattr(self, attr)
|
|
|
|
|
|
|
|
|
|
|
|
class OptionContainer:
|
|
|
|
|
|
|
|
"""
|
|
|
|
Abstract base class.
|
|
|
|
|
|
|
|
Class attributes:
|
|
|
|
standard_option_list : [Option]
|
|
|
|
list of standard options that will be accepted by all instances
|
|
|
|
of this parser class (intended to be overridden by subclasses).
|
|
|
|
|
|
|
|
Instance attributes:
|
|
|
|
option_list : [Option]
|
|
|
|
the list of Option objects contained by this OptionContainer
|
|
|
|
_short_opt : { string : Option }
|
|
|
|
dictionary mapping short option strings, eg. "-f" or "-X",
|
|
|
|
to the Option instances that implement them. If an Option
|
|
|
|
has multiple short option strings, it will appears in this
|
|
|
|
dictionary multiple times. [1]
|
|
|
|
_long_opt : { string : Option }
|
|
|
|
dictionary mapping long option strings, eg. "--file" or
|
|
|
|
"--exclude", to the Option instances that implement them.
|
|
|
|
Again, a given Option can occur multiple times in this
|
|
|
|
dictionary. [1]
|
|
|
|
defaults : { string : any }
|
|
|
|
dictionary mapping option destination names to default
|
|
|
|
values for each destination [1]
|
|
|
|
|
|
|
|
[1] These mappings are common to (shared by) all components of the
|
|
|
|
controlling OptionParser, where they are initially created.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def __init__(self, option_class, conflict_handler, description):
|
2002-11-14 18:00:19 -04:00
|
|
|
# Initialize the option list and related data structures.
|
|
|
|
# This method must be provided by subclasses, and it must
|
|
|
|
# initialize at least the following instance attributes:
|
|
|
|
# option_list, _short_opt, _long_opt, defaults.
|
|
|
|
self._create_option_list()
|
|
|
|
|
|
|
|
self.option_class = option_class
|
|
|
|
self.set_conflict_handler(conflict_handler)
|
|
|
|
self.set_description(description)
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def _create_option_mappings(self):
|
2002-11-14 18:00:19 -04:00
|
|
|
# For use by OptionParser constructor -- create the master
|
|
|
|
# option mappings used by this OptionParser and all
|
|
|
|
# OptionGroups that it owns.
|
|
|
|
self._short_opt = {} # single letter -> Option instance
|
|
|
|
self._long_opt = {} # long option -> Option instance
|
|
|
|
self.defaults = {} # maps option dest -> default value
|
|
|
|
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def _share_option_mappings(self, parser):
|
2002-11-14 18:00:19 -04:00
|
|
|
# For use by OptionGroup constructor -- use shared option
|
|
|
|
# mappings from the OptionParser that owns this OptionGroup.
|
|
|
|
self._short_opt = parser._short_opt
|
|
|
|
self._long_opt = parser._long_opt
|
|
|
|
self.defaults = parser.defaults
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def set_conflict_handler(self, handler):
|
2004-10-26 23:20:04 -03:00
|
|
|
if handler not in ("error", "resolve"):
|
2002-11-14 18:00:19 -04:00
|
|
|
raise ValueError, "invalid conflict_resolution value %r" % handler
|
|
|
|
self.conflict_handler = handler
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def set_description(self, description):
|
2002-11-14 18:00:19 -04:00
|
|
|
self.description = description
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def get_description(self):
|
|
|
|
return self.description
|
|
|
|
|
2002-11-14 18:00:19 -04:00
|
|
|
|
2006-04-23 00:47:58 -03:00
|
|
|
def destroy(self):
|
|
|
|
"""see OptionParser.destroy()."""
|
|
|
|
del self._short_opt
|
|
|
|
del self._long_opt
|
|
|
|
del self.defaults
|
|
|
|
|
|
|
|
|
2002-11-14 18:00:19 -04:00
|
|
|
# -- Option-adding methods -----------------------------------------
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def _check_conflict(self, option):
|
2002-11-14 18:00:19 -04:00
|
|
|
conflict_opts = []
|
|
|
|
for opt in option._short_opts:
|
2008-07-10 12:37:08 -03:00
|
|
|
if opt in self._short_opt:
|
2002-11-14 18:00:19 -04:00
|
|
|
conflict_opts.append((opt, self._short_opt[opt]))
|
|
|
|
for opt in option._long_opts:
|
2008-07-10 12:37:08 -03:00
|
|
|
if opt in self._long_opt:
|
2002-11-14 18:00:19 -04:00
|
|
|
conflict_opts.append((opt, self._long_opt[opt]))
|
|
|
|
|
|
|
|
if conflict_opts:
|
|
|
|
handler = self.conflict_handler
|
2004-10-26 23:20:04 -03:00
|
|
|
if handler == "error":
|
2002-11-14 18:00:19 -04:00
|
|
|
raise OptionConflictError(
|
|
|
|
"conflicting option string(s): %s"
|
|
|
|
% ", ".join([co[0] for co in conflict_opts]),
|
|
|
|
option)
|
2004-10-26 23:20:04 -03:00
|
|
|
elif handler == "resolve":
|
2002-11-14 18:00:19 -04:00
|
|
|
for (opt, c_option) in conflict_opts:
|
|
|
|
if opt.startswith("--"):
|
|
|
|
c_option._long_opts.remove(opt)
|
|
|
|
del self._long_opt[opt]
|
|
|
|
else:
|
|
|
|
c_option._short_opts.remove(opt)
|
|
|
|
del self._short_opt[opt]
|
|
|
|
if not (c_option._short_opts or c_option._long_opts):
|
|
|
|
c_option.container.option_list.remove(c_option)
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def add_option(self, *args, **kwargs):
|
2002-11-14 18:00:19 -04:00
|
|
|
"""add_option(Option)
|
|
|
|
add_option(opt_str, ..., kwarg=val, ...)
|
|
|
|
"""
|
2006-04-23 00:47:58 -03:00
|
|
|
if type(args[0]) is types.StringType:
|
2002-11-14 18:00:19 -04:00
|
|
|
option = self.option_class(*args, **kwargs)
|
|
|
|
elif len(args) == 1 and not kwargs:
|
|
|
|
option = args[0]
|
|
|
|
if not isinstance(option, Option):
|
|
|
|
raise TypeError, "not an Option instance: %r" % option
|
|
|
|
else:
|
|
|
|
raise TypeError, "invalid arguments"
|
|
|
|
|
|
|
|
self._check_conflict(option)
|
|
|
|
|
|
|
|
self.option_list.append(option)
|
|
|
|
option.container = self
|
|
|
|
for opt in option._short_opts:
|
|
|
|
self._short_opt[opt] = option
|
|
|
|
for opt in option._long_opts:
|
|
|
|
self._long_opt[opt] = option
|
|
|
|
|
|
|
|
if option.dest is not None: # option has a dest, we need a default
|
|
|
|
if option.default is not NO_DEFAULT:
|
|
|
|
self.defaults[option.dest] = option.default
|
2008-07-10 12:37:08 -03:00
|
|
|
elif option.dest not in self.defaults:
|
2002-11-14 18:00:19 -04:00
|
|
|
self.defaults[option.dest] = None
|
|
|
|
|
|
|
|
return option
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def add_options(self, option_list):
|
2002-11-14 18:00:19 -04:00
|
|
|
for option in option_list:
|
|
|
|
self.add_option(option)
|
|
|
|
|
|
|
|
# -- Option query/removal methods ----------------------------------
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def get_option(self, opt_str):
|
2002-11-14 18:00:19 -04:00
|
|
|
return (self._short_opt.get(opt_str) or
|
|
|
|
self._long_opt.get(opt_str))
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def has_option(self, opt_str):
|
2008-07-10 12:37:08 -03:00
|
|
|
return (opt_str in self._short_opt or
|
|
|
|
opt_str in self._long_opt)
|
2002-11-14 18:00:19 -04:00
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def remove_option(self, opt_str):
|
2002-11-14 18:00:19 -04:00
|
|
|
option = self._short_opt.get(opt_str)
|
|
|
|
if option is None:
|
|
|
|
option = self._long_opt.get(opt_str)
|
|
|
|
if option is None:
|
|
|
|
raise ValueError("no such option %r" % opt_str)
|
|
|
|
|
|
|
|
for opt in option._short_opts:
|
|
|
|
del self._short_opt[opt]
|
|
|
|
for opt in option._long_opts:
|
|
|
|
del self._long_opt[opt]
|
|
|
|
option.container.option_list.remove(option)
|
|
|
|
|
|
|
|
|
|
|
|
# -- Help-formatting methods ---------------------------------------
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def format_option_help(self, formatter):
|
2002-11-14 18:00:19 -04:00
|
|
|
if not self.option_list:
|
|
|
|
return ""
|
|
|
|
result = []
|
|
|
|
for option in self.option_list:
|
|
|
|
if not option.help is SUPPRESS_HELP:
|
|
|
|
result.append(formatter.format_option(option))
|
|
|
|
return "".join(result)
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def format_description(self, formatter):
|
|
|
|
return formatter.format_description(self.get_description())
|
2002-11-14 18:00:19 -04:00
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def format_help(self, formatter):
|
|
|
|
result = []
|
2002-11-14 18:00:19 -04:00
|
|
|
if self.description:
|
2004-07-31 13:15:44 -03:00
|
|
|
result.append(self.format_description(formatter))
|
|
|
|
if self.option_list:
|
|
|
|
result.append(self.format_option_help(formatter))
|
|
|
|
return "\n".join(result)
|
2002-11-14 18:00:19 -04:00
|
|
|
|
|
|
|
|
|
|
|
class OptionGroup (OptionContainer):
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def __init__(self, parser, title, description=None):
|
2002-11-14 18:00:19 -04:00
|
|
|
self.parser = parser
|
|
|
|
OptionContainer.__init__(
|
|
|
|
self, parser.option_class, parser.conflict_handler, description)
|
|
|
|
self.title = title
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def _create_option_list(self):
|
2002-11-14 18:00:19 -04:00
|
|
|
self.option_list = []
|
|
|
|
self._share_option_mappings(self.parser)
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def set_title(self, title):
|
2002-11-14 18:00:19 -04:00
|
|
|
self.title = title
|
|
|
|
|
2006-04-23 00:47:58 -03:00
|
|
|
def destroy(self):
|
|
|
|
"""see OptionParser.destroy()."""
|
|
|
|
OptionContainer.destroy(self)
|
|
|
|
del self.option_list
|
|
|
|
|
2002-11-14 18:00:19 -04:00
|
|
|
# -- Help-formatting methods ---------------------------------------
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def format_help(self, formatter):
|
2002-11-14 18:00:19 -04:00
|
|
|
result = formatter.format_heading(self.title)
|
|
|
|
formatter.indent()
|
|
|
|
result += OptionContainer.format_help(self, formatter)
|
|
|
|
formatter.dedent()
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
class OptionParser (OptionContainer):
|
|
|
|
|
|
|
|
"""
|
|
|
|
Class attributes:
|
|
|
|
standard_option_list : [Option]
|
|
|
|
list of standard options that will be accepted by all instances
|
|
|
|
of this parser class (intended to be overridden by subclasses).
|
|
|
|
|
|
|
|
Instance attributes:
|
|
|
|
usage : string
|
|
|
|
a usage string for your program. Before it is displayed
|
|
|
|
to the user, "%prog" will be expanded to the name of
|
2003-04-20 23:40:34 -03:00
|
|
|
your program (self.prog or os.path.basename(sys.argv[0])).
|
|
|
|
prog : string
|
|
|
|
the name of the current program (to override
|
|
|
|
os.path.basename(sys.argv[0])).
|
2006-04-23 00:47:58 -03:00
|
|
|
epilog : string
|
|
|
|
paragraph of help text to print after option help
|
2002-11-14 18:00:19 -04:00
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
option_groups : [OptionGroup]
|
|
|
|
list of option groups in this parser (option groups are
|
|
|
|
irrelevant for parsing the command-line, but very useful
|
|
|
|
for generating help)
|
|
|
|
|
|
|
|
allow_interspersed_args : bool = true
|
2002-11-14 18:00:19 -04:00
|
|
|
if true, positional arguments may be interspersed with options.
|
|
|
|
Assuming -a and -b each take a single argument, the command-line
|
|
|
|
-ablah foo bar -bboo baz
|
|
|
|
will be interpreted the same as
|
|
|
|
-ablah -bboo -- foo bar baz
|
|
|
|
If this flag were false, that command line would be interpreted as
|
|
|
|
-ablah -- foo bar -bboo baz
|
|
|
|
-- ie. we stop processing options as soon as we see the first
|
|
|
|
non-option argument. (This is the tradition followed by
|
|
|
|
Python's getopt module, Perl's Getopt::Std, and other argument-
|
|
|
|
parsing libraries, but it is generally annoying to users.)
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
process_default_values : bool = true
|
|
|
|
if true, option default values are processed similarly to option
|
|
|
|
values from the command line: that is, they are passed to the
|
|
|
|
type-checking function for the option's type (as long as the
|
|
|
|
default value is a string). (This really only matters if you
|
|
|
|
have defined custom types; see SF bug #955889.) Set it to false
|
|
|
|
to restore the behaviour of Optik 1.4.1 and earlier.
|
|
|
|
|
2002-11-14 18:00:19 -04:00
|
|
|
rargs : [string]
|
|
|
|
the argument list currently being parsed. Only set when
|
|
|
|
parse_args() is active, and continually trimmed down as
|
|
|
|
we consume arguments. Mainly there for the benefit of
|
|
|
|
callback options.
|
|
|
|
largs : [string]
|
|
|
|
the list of leftover arguments that we have skipped while
|
|
|
|
parsing options. If allow_interspersed_args is false, this
|
|
|
|
list is always empty.
|
|
|
|
values : Values
|
|
|
|
the set of option values currently being accumulated. Only
|
|
|
|
set when parse_args() is active. Also mainly for callbacks.
|
|
|
|
|
|
|
|
Because of the 'rargs', 'largs', and 'values' attributes,
|
|
|
|
OptionParser is not thread-safe. If, for some perverse reason, you
|
|
|
|
need to parse command-line arguments simultaneously in different
|
|
|
|
threads, use different OptionParser instances.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
standard_option_list = []
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def __init__(self,
|
|
|
|
usage=None,
|
|
|
|
option_list=None,
|
|
|
|
option_class=Option,
|
|
|
|
version=None,
|
|
|
|
conflict_handler="error",
|
|
|
|
description=None,
|
|
|
|
formatter=None,
|
|
|
|
add_help_option=True,
|
2006-04-23 00:47:58 -03:00
|
|
|
prog=None,
|
|
|
|
epilog=None):
|
2002-11-14 18:00:19 -04:00
|
|
|
OptionContainer.__init__(
|
|
|
|
self, option_class, conflict_handler, description)
|
|
|
|
self.set_usage(usage)
|
2003-04-20 23:40:34 -03:00
|
|
|
self.prog = prog
|
2002-11-14 18:00:19 -04:00
|
|
|
self.version = version
|
2004-07-31 13:15:44 -03:00
|
|
|
self.allow_interspersed_args = True
|
|
|
|
self.process_default_values = True
|
2002-11-14 18:00:19 -04:00
|
|
|
if formatter is None:
|
|
|
|
formatter = IndentedHelpFormatter()
|
|
|
|
self.formatter = formatter
|
2004-07-31 13:15:44 -03:00
|
|
|
self.formatter.set_parser(self)
|
2006-04-23 00:47:58 -03:00
|
|
|
self.epilog = epilog
|
2002-11-14 18:00:19 -04:00
|
|
|
|
|
|
|
# Populate the option list; initial sources are the
|
|
|
|
# standard_option_list class attribute, the 'option_list'
|
2004-07-31 13:15:44 -03:00
|
|
|
# argument, and (if applicable) the _add_version_option() and
|
|
|
|
# _add_help_option() methods.
|
2002-11-14 18:00:19 -04:00
|
|
|
self._populate_option_list(option_list,
|
|
|
|
add_help=add_help_option)
|
|
|
|
|
|
|
|
self._init_parsing_state()
|
|
|
|
|
2006-04-23 00:47:58 -03:00
|
|
|
|
|
|
|
def destroy(self):
|
|
|
|
"""
|
|
|
|
Declare that you are done with this OptionParser. This cleans up
|
|
|
|
reference cycles so the OptionParser (and all objects referenced by
|
2006-06-11 16:42:51 -03:00
|
|
|
it) can be garbage-collected promptly. After calling destroy(), the
|
2006-04-23 00:47:58 -03:00
|
|
|
OptionParser is unusable.
|
|
|
|
"""
|
|
|
|
OptionContainer.destroy(self)
|
|
|
|
for group in self.option_groups:
|
|
|
|
group.destroy()
|
|
|
|
del self.option_list
|
|
|
|
del self.option_groups
|
|
|
|
del self.formatter
|
|
|
|
|
|
|
|
|
2002-11-14 18:00:19 -04:00
|
|
|
# -- Private methods -----------------------------------------------
|
|
|
|
# (used by our or OptionContainer's constructor)
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def _create_option_list(self):
|
2002-11-14 18:00:19 -04:00
|
|
|
self.option_list = []
|
|
|
|
self.option_groups = []
|
|
|
|
self._create_option_mappings()
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def _add_help_option(self):
|
|
|
|
self.add_option("-h", "--help",
|
|
|
|
action="help",
|
|
|
|
help=_("show this help message and exit"))
|
|
|
|
|
|
|
|
def _add_version_option(self):
|
|
|
|
self.add_option("--version",
|
|
|
|
action="version",
|
|
|
|
help=_("show program's version number and exit"))
|
|
|
|
|
|
|
|
def _populate_option_list(self, option_list, add_help=True):
|
2002-11-14 18:00:19 -04:00
|
|
|
if self.standard_option_list:
|
|
|
|
self.add_options(self.standard_option_list)
|
|
|
|
if option_list:
|
|
|
|
self.add_options(option_list)
|
|
|
|
if self.version:
|
2004-07-31 13:15:44 -03:00
|
|
|
self._add_version_option()
|
2002-11-14 18:00:19 -04:00
|
|
|
if add_help:
|
2004-07-31 13:15:44 -03:00
|
|
|
self._add_help_option()
|
2002-11-14 18:00:19 -04:00
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def _init_parsing_state(self):
|
2002-11-14 18:00:19 -04:00
|
|
|
# These are set in parse_args() for the convenience of callbacks.
|
|
|
|
self.rargs = None
|
|
|
|
self.largs = None
|
|
|
|
self.values = None
|
|
|
|
|
|
|
|
|
|
|
|
# -- Simple modifier methods ---------------------------------------
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def set_usage(self, usage):
|
2002-11-14 18:00:19 -04:00
|
|
|
if usage is None:
|
2004-07-31 13:15:44 -03:00
|
|
|
self.usage = _("%prog [options]")
|
2002-11-14 18:00:19 -04:00
|
|
|
elif usage is SUPPRESS_USAGE:
|
|
|
|
self.usage = None
|
2004-07-31 13:15:44 -03:00
|
|
|
# For backwards compatibility with Optik 1.3 and earlier.
|
2006-04-23 00:47:58 -03:00
|
|
|
elif usage.lower().startswith("usage: "):
|
2002-11-14 18:00:19 -04:00
|
|
|
self.usage = usage[7:]
|
|
|
|
else:
|
|
|
|
self.usage = usage
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def enable_interspersed_args(self):
|
Merged revisions 66801,66803-66804,66813,66854-66856,66866,66870-66872,66874,66887,66903,66905,66911,66913,66927,66932,66938,66942,66962,66964,66973-66974,66977,66992,66998-66999,67002,67005,67007,67028,67040-67041,67044,67070,67089,67091,67101,67117-67119,67123-67124 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
................
r66801 | andrew.kuchling | 2008-10-04 23:51:59 +0200 (Sat, 04 Oct 2008) | 1 line
Punctuation fix; expand dict.update docstring to be clearer
................
r66803 | benjamin.peterson | 2008-10-05 00:15:31 +0200 (Sun, 05 Oct 2008) | 1 line
fix typo
................
r66804 | andrew.kuchling | 2008-10-05 02:11:56 +0200 (Sun, 05 Oct 2008) | 1 line
#1415508 from Rocky Bernstein: add docstrings for enable_interspersed_args(), disable_interspersed_args()
................
r66813 | andrew.kuchling | 2008-10-06 14:07:04 +0200 (Mon, 06 Oct 2008) | 3 lines
Per Greg Ward, optparse is no longer being externally maintained.
I'll look at the bugs in the Optik bug tracker and copy them to the Python bug
tracker if they're still relevant.
................
r66854 | georg.brandl | 2008-10-08 19:20:20 +0200 (Wed, 08 Oct 2008) | 2 lines
#4059: patch up some sqlite docs.
................
r66855 | georg.brandl | 2008-10-08 19:30:55 +0200 (Wed, 08 Oct 2008) | 2 lines
#4058: fix some whatsnew markup.
................
r66856 | georg.brandl | 2008-10-08 20:47:17 +0200 (Wed, 08 Oct 2008) | 3 lines
#3935: properly support list subclasses in the C impl. of bisect.
Patch reviewed by Raymond.
................
r66866 | benjamin.peterson | 2008-10-09 22:54:43 +0200 (Thu, 09 Oct 2008) | 1 line
update paragraph about __future__ for 2.6
................
r66870 | armin.rigo | 2008-10-10 10:40:44 +0200 (Fri, 10 Oct 2008) | 2 lines
Typo: "ThreadError" is the name in the C source.
................
r66871 | benjamin.peterson | 2008-10-10 22:38:49 +0200 (Fri, 10 Oct 2008) | 1 line
fix a small typo
................
r66872 | benjamin.peterson | 2008-10-10 22:51:37 +0200 (Fri, 10 Oct 2008) | 1 line
talk about how you can unzip with zip
................
r66874 | benjamin.peterson | 2008-10-11 00:23:41 +0200 (Sat, 11 Oct 2008) | 1 line
PyGILState_Acquire -> PyGILState_Ensure
................
r66887 | benjamin.peterson | 2008-10-13 23:51:40 +0200 (Mon, 13 Oct 2008) | 1 line
document how to disable fixers
................
r66903 | benjamin.peterson | 2008-10-15 22:34:09 +0200 (Wed, 15 Oct 2008) | 1 line
don't recurse into directories that start with '.'
................
r66905 | benjamin.peterson | 2008-10-15 23:05:55 +0200 (Wed, 15 Oct 2008) | 1 line
support the optional line argument for idle
................
r66911 | benjamin.peterson | 2008-10-16 01:10:28 +0200 (Thu, 16 Oct 2008) | 41 lines
Merged revisions 66805,66841,66860,66884-66886,66893,66907,66910 via svnmerge from
svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3
........
r66805 | benjamin.peterson | 2008-10-04 20:11:02 -0500 (Sat, 04 Oct 2008) | 1 line
mention what the fixes directory is for
........
r66841 | benjamin.peterson | 2008-10-07 17:48:12 -0500 (Tue, 07 Oct 2008) | 1 line
use assertFalse and assertTrue
........
r66860 | benjamin.peterson | 2008-10-08 16:05:07 -0500 (Wed, 08 Oct 2008) | 1 line
instead of abusing the pattern matcher, use start_tree to find a next binding
........
r66884 | benjamin.peterson | 2008-10-13 15:50:30 -0500 (Mon, 13 Oct 2008) | 1 line
don't print tokens to stdout when -v is given
........
r66885 | benjamin.peterson | 2008-10-13 16:28:57 -0500 (Mon, 13 Oct 2008) | 1 line
add the -x option to disable fixers
........
r66886 | benjamin.peterson | 2008-10-13 16:33:53 -0500 (Mon, 13 Oct 2008) | 1 line
cut down on some crud
........
r66893 | benjamin.peterson | 2008-10-14 17:16:54 -0500 (Tue, 14 Oct 2008) | 1 line
add an optional set literal fixer
........
r66907 | benjamin.peterson | 2008-10-15 16:59:41 -0500 (Wed, 15 Oct 2008) | 1 line
don't write backup files by default
........
r66910 | benjamin.peterson | 2008-10-15 17:43:10 -0500 (Wed, 15 Oct 2008) | 1 line
add the -n option; it stops backupfiles from being written
........
................
r66913 | benjamin.peterson | 2008-10-16 20:52:14 +0200 (Thu, 16 Oct 2008) | 1 line
document that deque indexing is O(n) #4123
................
r66927 | andrew.kuchling | 2008-10-16 22:15:47 +0200 (Thu, 16 Oct 2008) | 1 line
Fix wording (2.6.1 backport candidate)
................
r66932 | benjamin.peterson | 2008-10-16 23:09:28 +0200 (Thu, 16 Oct 2008) | 1 line
check for error conditions in _json #3623
................
r66938 | benjamin.peterson | 2008-10-16 23:27:54 +0200 (Thu, 16 Oct 2008) | 1 line
fix possible ref leak
................
r66942 | benjamin.peterson | 2008-10-16 23:48:06 +0200 (Thu, 16 Oct 2008) | 1 line
fix more possible ref leaks in _json and use Py_CLEAR
................
r66962 | benjamin.peterson | 2008-10-17 22:01:01 +0200 (Fri, 17 Oct 2008) | 1 line
clarify CALL_FUNCTION #4141
................
r66964 | georg.brandl | 2008-10-17 23:41:49 +0200 (Fri, 17 Oct 2008) | 2 lines
Fix duplicate word.
................
r66973 | armin.ronacher | 2008-10-19 10:27:43 +0200 (Sun, 19 Oct 2008) | 3 lines
Fixed #4067 by implementing _attributes and _fields for the AST root node.
................
r66974 | benjamin.peterson | 2008-10-19 15:59:01 +0200 (Sun, 19 Oct 2008) | 1 line
fix compiler warning
................
r66977 | benjamin.peterson | 2008-10-19 21:39:16 +0200 (Sun, 19 Oct 2008) | 1 line
mention -n
................
r66992 | benjamin.peterson | 2008-10-21 22:51:13 +0200 (Tue, 21 Oct 2008) | 1 line
make sure to call iteritems()
................
r66998 | benjamin.peterson | 2008-10-22 22:57:43 +0200 (Wed, 22 Oct 2008) | 1 line
fix a few typos
................
r66999 | benjamin.peterson | 2008-10-22 23:05:30 +0200 (Wed, 22 Oct 2008) | 1 line
and another typo...
................
r67002 | hirokazu.yamamoto | 2008-10-23 02:37:33 +0200 (Thu, 23 Oct 2008) | 1 line
Issue #4183: Some tests didn't run with pickle.HIGHEST_PROTOCOL.
................
r67005 | walter.doerwald | 2008-10-23 15:11:39 +0200 (Thu, 23 Oct 2008) | 2 lines
Use the correct names of the stateless codec functions (Fixes issue 4178).
................
r67007 | benjamin.peterson | 2008-10-23 23:43:48 +0200 (Thu, 23 Oct 2008) | 1 line
only nonempty __slots__ don't work
................
r67028 | benjamin.peterson | 2008-10-26 01:27:07 +0200 (Sun, 26 Oct 2008) | 1 line
don't use a catch-all
................
r67040 | armin.rigo | 2008-10-28 18:01:21 +0100 (Tue, 28 Oct 2008) | 5 lines
Fix one of the tests: it relied on being present in an "output test" in
order to actually test what it was supposed to test, i.e. that the code
in the __del__ method did not crash. Use instead the new helper
test_support.captured_output().
................
r67041 | benjamin.peterson | 2008-10-29 21:33:00 +0100 (Wed, 29 Oct 2008) | 1 line
mention the version gettempdir() was added
................
r67044 | amaury.forgeotdarc | 2008-10-30 00:15:57 +0100 (Thu, 30 Oct 2008) | 3 lines
Correct error message in io.open():
closefd=True is the only accepted value with a file name.
................
r67070 | benjamin.peterson | 2008-10-31 21:41:44 +0100 (Fri, 31 Oct 2008) | 1 line
rephrase has_key doc
................
r67089 | benjamin.peterson | 2008-11-03 21:43:20 +0100 (Mon, 03 Nov 2008) | 1 line
clarify by splitting into multiple paragraphs
................
r67091 | benjamin.peterson | 2008-11-03 23:34:57 +0100 (Mon, 03 Nov 2008) | 1 line
move a FileIO test to test_fileio
................
r67101 | georg.brandl | 2008-11-04 21:49:35 +0100 (Tue, 04 Nov 2008) | 2 lines
#4167: fix markup glitches.
................
r67117 | georg.brandl | 2008-11-06 11:17:58 +0100 (Thu, 06 Nov 2008) | 2 lines
#4268: Use correct module for two toplevel functions.
................
r67118 | georg.brandl | 2008-11-06 11:19:11 +0100 (Thu, 06 Nov 2008) | 2 lines
#4267: small fixes in sqlite3 docs.
................
r67119 | georg.brandl | 2008-11-06 11:20:49 +0100 (Thu, 06 Nov 2008) | 2 lines
#4245: move Thread section to the top.
................
r67123 | georg.brandl | 2008-11-06 19:49:15 +0100 (Thu, 06 Nov 2008) | 2 lines
#4247: add "pass" examples to tutorial.
................
r67124 | andrew.kuchling | 2008-11-06 20:23:02 +0100 (Thu, 06 Nov 2008) | 1 line
Fix grammar error; reword two paragraphs
................
2008-11-07 04:56:27 -04:00
|
|
|
"""Set parsing to not stop on the first non-option, allowing
|
|
|
|
interspersing switches with command arguments. This is the
|
|
|
|
default behavior. See also disable_interspersed_args() and the
|
|
|
|
class documentation description of the attribute
|
|
|
|
allow_interspersed_args."""
|
2004-07-31 13:15:44 -03:00
|
|
|
self.allow_interspersed_args = True
|
|
|
|
|
|
|
|
def disable_interspersed_args(self):
|
Merged revisions 66801,66803-66804,66813,66854-66856,66866,66870-66872,66874,66887,66903,66905,66911,66913,66927,66932,66938,66942,66962,66964,66973-66974,66977,66992,66998-66999,67002,67005,67007,67028,67040-67041,67044,67070,67089,67091,67101,67117-67119,67123-67124 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
................
r66801 | andrew.kuchling | 2008-10-04 23:51:59 +0200 (Sat, 04 Oct 2008) | 1 line
Punctuation fix; expand dict.update docstring to be clearer
................
r66803 | benjamin.peterson | 2008-10-05 00:15:31 +0200 (Sun, 05 Oct 2008) | 1 line
fix typo
................
r66804 | andrew.kuchling | 2008-10-05 02:11:56 +0200 (Sun, 05 Oct 2008) | 1 line
#1415508 from Rocky Bernstein: add docstrings for enable_interspersed_args(), disable_interspersed_args()
................
r66813 | andrew.kuchling | 2008-10-06 14:07:04 +0200 (Mon, 06 Oct 2008) | 3 lines
Per Greg Ward, optparse is no longer being externally maintained.
I'll look at the bugs in the Optik bug tracker and copy them to the Python bug
tracker if they're still relevant.
................
r66854 | georg.brandl | 2008-10-08 19:20:20 +0200 (Wed, 08 Oct 2008) | 2 lines
#4059: patch up some sqlite docs.
................
r66855 | georg.brandl | 2008-10-08 19:30:55 +0200 (Wed, 08 Oct 2008) | 2 lines
#4058: fix some whatsnew markup.
................
r66856 | georg.brandl | 2008-10-08 20:47:17 +0200 (Wed, 08 Oct 2008) | 3 lines
#3935: properly support list subclasses in the C impl. of bisect.
Patch reviewed by Raymond.
................
r66866 | benjamin.peterson | 2008-10-09 22:54:43 +0200 (Thu, 09 Oct 2008) | 1 line
update paragraph about __future__ for 2.6
................
r66870 | armin.rigo | 2008-10-10 10:40:44 +0200 (Fri, 10 Oct 2008) | 2 lines
Typo: "ThreadError" is the name in the C source.
................
r66871 | benjamin.peterson | 2008-10-10 22:38:49 +0200 (Fri, 10 Oct 2008) | 1 line
fix a small typo
................
r66872 | benjamin.peterson | 2008-10-10 22:51:37 +0200 (Fri, 10 Oct 2008) | 1 line
talk about how you can unzip with zip
................
r66874 | benjamin.peterson | 2008-10-11 00:23:41 +0200 (Sat, 11 Oct 2008) | 1 line
PyGILState_Acquire -> PyGILState_Ensure
................
r66887 | benjamin.peterson | 2008-10-13 23:51:40 +0200 (Mon, 13 Oct 2008) | 1 line
document how to disable fixers
................
r66903 | benjamin.peterson | 2008-10-15 22:34:09 +0200 (Wed, 15 Oct 2008) | 1 line
don't recurse into directories that start with '.'
................
r66905 | benjamin.peterson | 2008-10-15 23:05:55 +0200 (Wed, 15 Oct 2008) | 1 line
support the optional line argument for idle
................
r66911 | benjamin.peterson | 2008-10-16 01:10:28 +0200 (Thu, 16 Oct 2008) | 41 lines
Merged revisions 66805,66841,66860,66884-66886,66893,66907,66910 via svnmerge from
svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3
........
r66805 | benjamin.peterson | 2008-10-04 20:11:02 -0500 (Sat, 04 Oct 2008) | 1 line
mention what the fixes directory is for
........
r66841 | benjamin.peterson | 2008-10-07 17:48:12 -0500 (Tue, 07 Oct 2008) | 1 line
use assertFalse and assertTrue
........
r66860 | benjamin.peterson | 2008-10-08 16:05:07 -0500 (Wed, 08 Oct 2008) | 1 line
instead of abusing the pattern matcher, use start_tree to find a next binding
........
r66884 | benjamin.peterson | 2008-10-13 15:50:30 -0500 (Mon, 13 Oct 2008) | 1 line
don't print tokens to stdout when -v is given
........
r66885 | benjamin.peterson | 2008-10-13 16:28:57 -0500 (Mon, 13 Oct 2008) | 1 line
add the -x option to disable fixers
........
r66886 | benjamin.peterson | 2008-10-13 16:33:53 -0500 (Mon, 13 Oct 2008) | 1 line
cut down on some crud
........
r66893 | benjamin.peterson | 2008-10-14 17:16:54 -0500 (Tue, 14 Oct 2008) | 1 line
add an optional set literal fixer
........
r66907 | benjamin.peterson | 2008-10-15 16:59:41 -0500 (Wed, 15 Oct 2008) | 1 line
don't write backup files by default
........
r66910 | benjamin.peterson | 2008-10-15 17:43:10 -0500 (Wed, 15 Oct 2008) | 1 line
add the -n option; it stops backupfiles from being written
........
................
r66913 | benjamin.peterson | 2008-10-16 20:52:14 +0200 (Thu, 16 Oct 2008) | 1 line
document that deque indexing is O(n) #4123
................
r66927 | andrew.kuchling | 2008-10-16 22:15:47 +0200 (Thu, 16 Oct 2008) | 1 line
Fix wording (2.6.1 backport candidate)
................
r66932 | benjamin.peterson | 2008-10-16 23:09:28 +0200 (Thu, 16 Oct 2008) | 1 line
check for error conditions in _json #3623
................
r66938 | benjamin.peterson | 2008-10-16 23:27:54 +0200 (Thu, 16 Oct 2008) | 1 line
fix possible ref leak
................
r66942 | benjamin.peterson | 2008-10-16 23:48:06 +0200 (Thu, 16 Oct 2008) | 1 line
fix more possible ref leaks in _json and use Py_CLEAR
................
r66962 | benjamin.peterson | 2008-10-17 22:01:01 +0200 (Fri, 17 Oct 2008) | 1 line
clarify CALL_FUNCTION #4141
................
r66964 | georg.brandl | 2008-10-17 23:41:49 +0200 (Fri, 17 Oct 2008) | 2 lines
Fix duplicate word.
................
r66973 | armin.ronacher | 2008-10-19 10:27:43 +0200 (Sun, 19 Oct 2008) | 3 lines
Fixed #4067 by implementing _attributes and _fields for the AST root node.
................
r66974 | benjamin.peterson | 2008-10-19 15:59:01 +0200 (Sun, 19 Oct 2008) | 1 line
fix compiler warning
................
r66977 | benjamin.peterson | 2008-10-19 21:39:16 +0200 (Sun, 19 Oct 2008) | 1 line
mention -n
................
r66992 | benjamin.peterson | 2008-10-21 22:51:13 +0200 (Tue, 21 Oct 2008) | 1 line
make sure to call iteritems()
................
r66998 | benjamin.peterson | 2008-10-22 22:57:43 +0200 (Wed, 22 Oct 2008) | 1 line
fix a few typos
................
r66999 | benjamin.peterson | 2008-10-22 23:05:30 +0200 (Wed, 22 Oct 2008) | 1 line
and another typo...
................
r67002 | hirokazu.yamamoto | 2008-10-23 02:37:33 +0200 (Thu, 23 Oct 2008) | 1 line
Issue #4183: Some tests didn't run with pickle.HIGHEST_PROTOCOL.
................
r67005 | walter.doerwald | 2008-10-23 15:11:39 +0200 (Thu, 23 Oct 2008) | 2 lines
Use the correct names of the stateless codec functions (Fixes issue 4178).
................
r67007 | benjamin.peterson | 2008-10-23 23:43:48 +0200 (Thu, 23 Oct 2008) | 1 line
only nonempty __slots__ don't work
................
r67028 | benjamin.peterson | 2008-10-26 01:27:07 +0200 (Sun, 26 Oct 2008) | 1 line
don't use a catch-all
................
r67040 | armin.rigo | 2008-10-28 18:01:21 +0100 (Tue, 28 Oct 2008) | 5 lines
Fix one of the tests: it relied on being present in an "output test" in
order to actually test what it was supposed to test, i.e. that the code
in the __del__ method did not crash. Use instead the new helper
test_support.captured_output().
................
r67041 | benjamin.peterson | 2008-10-29 21:33:00 +0100 (Wed, 29 Oct 2008) | 1 line
mention the version gettempdir() was added
................
r67044 | amaury.forgeotdarc | 2008-10-30 00:15:57 +0100 (Thu, 30 Oct 2008) | 3 lines
Correct error message in io.open():
closefd=True is the only accepted value with a file name.
................
r67070 | benjamin.peterson | 2008-10-31 21:41:44 +0100 (Fri, 31 Oct 2008) | 1 line
rephrase has_key doc
................
r67089 | benjamin.peterson | 2008-11-03 21:43:20 +0100 (Mon, 03 Nov 2008) | 1 line
clarify by splitting into multiple paragraphs
................
r67091 | benjamin.peterson | 2008-11-03 23:34:57 +0100 (Mon, 03 Nov 2008) | 1 line
move a FileIO test to test_fileio
................
r67101 | georg.brandl | 2008-11-04 21:49:35 +0100 (Tue, 04 Nov 2008) | 2 lines
#4167: fix markup glitches.
................
r67117 | georg.brandl | 2008-11-06 11:17:58 +0100 (Thu, 06 Nov 2008) | 2 lines
#4268: Use correct module for two toplevel functions.
................
r67118 | georg.brandl | 2008-11-06 11:19:11 +0100 (Thu, 06 Nov 2008) | 2 lines
#4267: small fixes in sqlite3 docs.
................
r67119 | georg.brandl | 2008-11-06 11:20:49 +0100 (Thu, 06 Nov 2008) | 2 lines
#4245: move Thread section to the top.
................
r67123 | georg.brandl | 2008-11-06 19:49:15 +0100 (Thu, 06 Nov 2008) | 2 lines
#4247: add "pass" examples to tutorial.
................
r67124 | andrew.kuchling | 2008-11-06 20:23:02 +0100 (Thu, 06 Nov 2008) | 1 line
Fix grammar error; reword two paragraphs
................
2008-11-07 04:56:27 -04:00
|
|
|
"""Set parsing to stop on the first non-option. Use this if
|
|
|
|
you have a command processor which runs another command that
|
|
|
|
has options of its own and you want to make sure these options
|
|
|
|
don't get confused.
|
|
|
|
"""
|
2004-07-31 13:15:44 -03:00
|
|
|
self.allow_interspersed_args = False
|
2002-11-14 18:00:19 -04:00
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def set_process_default_values(self, process):
|
|
|
|
self.process_default_values = process
|
2002-11-14 18:00:19 -04:00
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def set_default(self, dest, value):
|
2002-11-14 18:00:19 -04:00
|
|
|
self.defaults[dest] = value
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def set_defaults(self, **kwargs):
|
2002-11-14 18:00:19 -04:00
|
|
|
self.defaults.update(kwargs)
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def _get_all_options(self):
|
|
|
|
options = self.option_list[:]
|
|
|
|
for group in self.option_groups:
|
|
|
|
options.extend(group.option_list)
|
|
|
|
return options
|
|
|
|
|
|
|
|
def get_default_values(self):
|
|
|
|
if not self.process_default_values:
|
|
|
|
# Old, pre-Optik 1.5 behaviour.
|
|
|
|
return Values(self.defaults)
|
|
|
|
|
|
|
|
defaults = self.defaults.copy()
|
|
|
|
for option in self._get_all_options():
|
|
|
|
default = defaults.get(option.dest)
|
2006-04-23 00:47:58 -03:00
|
|
|
if isbasestring(default):
|
2004-07-31 13:15:44 -03:00
|
|
|
opt_str = option.get_opt_string()
|
|
|
|
defaults[option.dest] = option.check_value(opt_str, default)
|
|
|
|
|
|
|
|
return Values(defaults)
|
2002-11-14 18:00:19 -04:00
|
|
|
|
|
|
|
|
|
|
|
# -- OptionGroup methods -------------------------------------------
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def add_option_group(self, *args, **kwargs):
|
2002-11-14 18:00:19 -04:00
|
|
|
# XXX lots of overlap with OptionContainer.add_option()
|
2006-04-23 00:47:58 -03:00
|
|
|
if type(args[0]) is types.StringType:
|
2002-11-14 18:00:19 -04:00
|
|
|
group = OptionGroup(self, *args, **kwargs)
|
|
|
|
elif len(args) == 1 and not kwargs:
|
|
|
|
group = args[0]
|
|
|
|
if not isinstance(group, OptionGroup):
|
|
|
|
raise TypeError, "not an OptionGroup instance: %r" % group
|
|
|
|
if group.parser is not self:
|
|
|
|
raise ValueError, "invalid OptionGroup (wrong parser)"
|
|
|
|
else:
|
|
|
|
raise TypeError, "invalid arguments"
|
|
|
|
|
|
|
|
self.option_groups.append(group)
|
|
|
|
return group
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def get_option_group(self, opt_str):
|
2002-11-14 18:00:19 -04:00
|
|
|
option = (self._short_opt.get(opt_str) or
|
|
|
|
self._long_opt.get(opt_str))
|
|
|
|
if option and option.container is not self:
|
|
|
|
return option.container
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
# -- Option-parsing methods ----------------------------------------
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def _get_args(self, args):
|
2002-11-14 18:00:19 -04:00
|
|
|
if args is None:
|
|
|
|
return sys.argv[1:]
|
|
|
|
else:
|
|
|
|
return args[:] # don't modify caller's list
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def parse_args(self, args=None, values=None):
|
2002-11-14 18:00:19 -04:00
|
|
|
"""
|
|
|
|
parse_args(args : [string] = sys.argv[1:],
|
|
|
|
values : Values = None)
|
|
|
|
-> (values : Values, args : [string])
|
|
|
|
|
|
|
|
Parse the command-line options found in 'args' (default:
|
|
|
|
sys.argv[1:]). Any errors result in a call to 'error()', which
|
|
|
|
by default prints the usage message to stderr and calls
|
|
|
|
sys.exit() with an error message. On success returns a pair
|
|
|
|
(values, args) where 'values' is an Values instance (with all
|
|
|
|
your option values) and 'args' is the list of arguments left
|
|
|
|
over after parsing options.
|
|
|
|
"""
|
|
|
|
rargs = self._get_args(args)
|
|
|
|
if values is None:
|
|
|
|
values = self.get_default_values()
|
|
|
|
|
|
|
|
# Store the halves of the argument list as attributes for the
|
|
|
|
# convenience of callbacks:
|
|
|
|
# rargs
|
|
|
|
# the rest of the command-line (the "r" stands for
|
|
|
|
# "remaining" or "right-hand")
|
|
|
|
# largs
|
|
|
|
# the leftover arguments -- ie. what's left after removing
|
|
|
|
# options and their arguments (the "l" stands for "leftover"
|
|
|
|
# or "left-hand")
|
|
|
|
self.rargs = rargs
|
|
|
|
self.largs = largs = []
|
|
|
|
self.values = values
|
|
|
|
|
|
|
|
try:
|
|
|
|
stop = self._process_args(largs, rargs, values)
|
|
|
|
except (BadOptionError, OptionValueError), err:
|
2006-04-23 00:47:58 -03:00
|
|
|
self.error(str(err))
|
2002-11-14 18:00:19 -04:00
|
|
|
|
|
|
|
args = largs + rargs
|
|
|
|
return self.check_values(values, args)
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def check_values(self, values, args):
|
2002-11-14 18:00:19 -04:00
|
|
|
"""
|
|
|
|
check_values(values : Values, args : [string])
|
|
|
|
-> (values : Values, args : [string])
|
|
|
|
|
|
|
|
Check that the supplied option values and leftover arguments are
|
|
|
|
valid. Returns the option values and leftover arguments
|
|
|
|
(possibly adjusted, possibly completely new -- whatever you
|
|
|
|
like). Default implementation just returns the passed-in
|
|
|
|
values; subclasses may override as desired.
|
|
|
|
"""
|
|
|
|
return (values, args)
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def _process_args(self, largs, rargs, values):
|
2002-11-14 18:00:19 -04:00
|
|
|
"""_process_args(largs : [string],
|
|
|
|
rargs : [string],
|
|
|
|
values : Values)
|
|
|
|
|
|
|
|
Process command-line arguments and populate 'values', consuming
|
|
|
|
options and arguments from 'rargs'. If 'allow_interspersed_args' is
|
|
|
|
false, stop at the first non-option argument. If true, accumulate any
|
|
|
|
interspersed non-option arguments in 'largs'.
|
|
|
|
"""
|
|
|
|
while rargs:
|
|
|
|
arg = rargs[0]
|
|
|
|
# We handle bare "--" explicitly, and bare "-" is handled by the
|
|
|
|
# standard arg handler since the short arg case ensures that the
|
|
|
|
# len of the opt string is greater than 1.
|
|
|
|
if arg == "--":
|
|
|
|
del rargs[0]
|
|
|
|
return
|
|
|
|
elif arg[0:2] == "--":
|
|
|
|
# process a single long option (possibly with value(s))
|
|
|
|
self._process_long_opt(rargs, values)
|
|
|
|
elif arg[:1] == "-" and len(arg) > 1:
|
|
|
|
# process a cluster of short options (possibly with
|
|
|
|
# value(s) for the last one only)
|
|
|
|
self._process_short_opts(rargs, values)
|
|
|
|
elif self.allow_interspersed_args:
|
|
|
|
largs.append(arg)
|
|
|
|
del rargs[0]
|
|
|
|
else:
|
|
|
|
return # stop now, leave this arg in rargs
|
|
|
|
|
|
|
|
# Say this is the original argument list:
|
|
|
|
# [arg0, arg1, ..., arg(i-1), arg(i), arg(i+1), ..., arg(N-1)]
|
|
|
|
# ^
|
|
|
|
# (we are about to process arg(i)).
|
|
|
|
#
|
|
|
|
# Then rargs is [arg(i), ..., arg(N-1)] and largs is a *subset* of
|
|
|
|
# [arg0, ..., arg(i-1)] (any options and their arguments will have
|
|
|
|
# been removed from largs).
|
|
|
|
#
|
|
|
|
# The while loop will usually consume 1 or more arguments per pass.
|
|
|
|
# If it consumes 1 (eg. arg is an option that takes no arguments),
|
|
|
|
# then after _process_arg() is done the situation is:
|
|
|
|
#
|
|
|
|
# largs = subset of [arg0, ..., arg(i)]
|
|
|
|
# rargs = [arg(i+1), ..., arg(N-1)]
|
|
|
|
#
|
|
|
|
# If allow_interspersed_args is false, largs will always be
|
|
|
|
# *empty* -- still a subset of [arg0, ..., arg(i-1)], but
|
|
|
|
# not a very interesting subset!
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def _match_long_opt(self, opt):
|
2002-11-14 18:00:19 -04:00
|
|
|
"""_match_long_opt(opt : string) -> string
|
|
|
|
|
|
|
|
Determine which long option string 'opt' matches, ie. which one
|
|
|
|
it is an unambiguous abbrevation for. Raises BadOptionError if
|
|
|
|
'opt' doesn't unambiguously match any long option string.
|
|
|
|
"""
|
|
|
|
return _match_abbrev(opt, self._long_opt)
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def _process_long_opt(self, rargs, values):
|
2002-11-14 18:00:19 -04:00
|
|
|
arg = rargs.pop(0)
|
|
|
|
|
|
|
|
# Value explicitly attached to arg? Pretend it's the next
|
|
|
|
# argument.
|
|
|
|
if "=" in arg:
|
|
|
|
(opt, next_arg) = arg.split("=", 1)
|
|
|
|
rargs.insert(0, next_arg)
|
2004-07-31 13:15:44 -03:00
|
|
|
had_explicit_value = True
|
2002-11-14 18:00:19 -04:00
|
|
|
else:
|
|
|
|
opt = arg
|
2004-07-31 13:15:44 -03:00
|
|
|
had_explicit_value = False
|
2002-11-14 18:00:19 -04:00
|
|
|
|
|
|
|
opt = self._match_long_opt(opt)
|
|
|
|
option = self._long_opt[opt]
|
|
|
|
if option.takes_value():
|
|
|
|
nargs = option.nargs
|
|
|
|
if len(rargs) < nargs:
|
|
|
|
if nargs == 1:
|
2004-07-31 13:15:44 -03:00
|
|
|
self.error(_("%s option requires an argument") % opt)
|
2002-11-14 18:00:19 -04:00
|
|
|
else:
|
2004-07-31 13:15:44 -03:00
|
|
|
self.error(_("%s option requires %d arguments")
|
2002-11-14 18:00:19 -04:00
|
|
|
% (opt, nargs))
|
|
|
|
elif nargs == 1:
|
|
|
|
value = rargs.pop(0)
|
|
|
|
else:
|
|
|
|
value = tuple(rargs[0:nargs])
|
|
|
|
del rargs[0:nargs]
|
|
|
|
|
|
|
|
elif had_explicit_value:
|
2004-07-31 13:15:44 -03:00
|
|
|
self.error(_("%s option does not take a value") % opt)
|
2002-11-14 18:00:19 -04:00
|
|
|
|
|
|
|
else:
|
|
|
|
value = None
|
|
|
|
|
|
|
|
option.process(opt, value, values, self)
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def _process_short_opts(self, rargs, values):
|
2002-11-14 18:00:19 -04:00
|
|
|
arg = rargs.pop(0)
|
2004-07-31 13:15:44 -03:00
|
|
|
stop = False
|
2002-11-14 18:00:19 -04:00
|
|
|
i = 1
|
|
|
|
for ch in arg[1:]:
|
|
|
|
opt = "-" + ch
|
|
|
|
option = self._short_opt.get(opt)
|
|
|
|
i += 1 # we have consumed a character
|
|
|
|
|
|
|
|
if not option:
|
2006-04-23 00:47:58 -03:00
|
|
|
raise BadOptionError(opt)
|
2002-11-14 18:00:19 -04:00
|
|
|
if option.takes_value():
|
|
|
|
# Any characters left in arg? Pretend they're the
|
|
|
|
# next arg, and stop consuming characters of arg.
|
|
|
|
if i < len(arg):
|
|
|
|
rargs.insert(0, arg[i:])
|
2004-07-31 13:15:44 -03:00
|
|
|
stop = True
|
2002-11-14 18:00:19 -04:00
|
|
|
|
|
|
|
nargs = option.nargs
|
|
|
|
if len(rargs) < nargs:
|
|
|
|
if nargs == 1:
|
2004-07-31 13:15:44 -03:00
|
|
|
self.error(_("%s option requires an argument") % opt)
|
2002-11-14 18:00:19 -04:00
|
|
|
else:
|
2004-07-31 13:15:44 -03:00
|
|
|
self.error(_("%s option requires %d arguments")
|
2002-11-14 18:00:19 -04:00
|
|
|
% (opt, nargs))
|
|
|
|
elif nargs == 1:
|
|
|
|
value = rargs.pop(0)
|
|
|
|
else:
|
|
|
|
value = tuple(rargs[0:nargs])
|
|
|
|
del rargs[0:nargs]
|
|
|
|
|
|
|
|
else: # option doesn't take a value
|
|
|
|
value = None
|
|
|
|
|
|
|
|
option.process(opt, value, values, self)
|
|
|
|
|
|
|
|
if stop:
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
|
|
# -- Feedback methods ----------------------------------------------
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def get_prog_name(self):
|
|
|
|
if self.prog is None:
|
|
|
|
return os.path.basename(sys.argv[0])
|
|
|
|
else:
|
|
|
|
return self.prog
|
|
|
|
|
|
|
|
def expand_prog_name(self, s):
|
|
|
|
return s.replace("%prog", self.get_prog_name())
|
|
|
|
|
|
|
|
def get_description(self):
|
|
|
|
return self.expand_prog_name(self.description)
|
|
|
|
|
2004-10-26 23:20:04 -03:00
|
|
|
def exit(self, status=0, msg=None):
|
|
|
|
if msg:
|
|
|
|
sys.stderr.write(msg)
|
|
|
|
sys.exit(status)
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def error(self, msg):
|
2002-11-14 18:00:19 -04:00
|
|
|
"""error(msg : string)
|
|
|
|
|
|
|
|
Print a usage message incorporating 'msg' to stderr and exit.
|
|
|
|
If you override this in a subclass, it should not return -- it
|
|
|
|
should either exit or raise an exception.
|
|
|
|
"""
|
|
|
|
self.print_usage(sys.stderr)
|
2004-10-26 23:20:04 -03:00
|
|
|
self.exit(2, "%s: error: %s\n" % (self.get_prog_name(), msg))
|
2002-11-14 18:00:19 -04:00
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def get_usage(self):
|
2002-11-14 18:00:19 -04:00
|
|
|
if self.usage:
|
|
|
|
return self.formatter.format_usage(
|
2004-07-31 13:15:44 -03:00
|
|
|
self.expand_prog_name(self.usage))
|
2002-11-14 18:00:19 -04:00
|
|
|
else:
|
|
|
|
return ""
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def print_usage(self, file=None):
|
2002-11-14 18:00:19 -04:00
|
|
|
"""print_usage(file : file = stdout)
|
|
|
|
|
|
|
|
Print the usage message for the current program (self.usage) to
|
|
|
|
'file' (default stdout). Any occurence of the string "%prog" in
|
|
|
|
self.usage is replaced with the name of the current program
|
|
|
|
(basename of sys.argv[0]). Does nothing if self.usage is empty
|
|
|
|
or not defined.
|
|
|
|
"""
|
|
|
|
if self.usage:
|
|
|
|
print >>file, self.get_usage()
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def get_version(self):
|
2002-11-14 18:00:19 -04:00
|
|
|
if self.version:
|
2004-07-31 13:15:44 -03:00
|
|
|
return self.expand_prog_name(self.version)
|
2002-11-14 18:00:19 -04:00
|
|
|
else:
|
|
|
|
return ""
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def print_version(self, file=None):
|
2002-11-14 18:00:19 -04:00
|
|
|
"""print_version(file : file = stdout)
|
|
|
|
|
|
|
|
Print the version message for this program (self.version) to
|
|
|
|
'file' (default stdout). As with print_usage(), any occurence
|
|
|
|
of "%prog" in self.version is replaced by the current program's
|
|
|
|
name. Does nothing if self.version is empty or undefined.
|
|
|
|
"""
|
|
|
|
if self.version:
|
|
|
|
print >>file, self.get_version()
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def format_option_help(self, formatter=None):
|
2002-11-14 18:00:19 -04:00
|
|
|
if formatter is None:
|
|
|
|
formatter = self.formatter
|
|
|
|
formatter.store_option_strings(self)
|
|
|
|
result = []
|
2006-04-23 00:47:58 -03:00
|
|
|
result.append(formatter.format_heading(_("Options")))
|
2002-11-14 18:00:19 -04:00
|
|
|
formatter.indent()
|
|
|
|
if self.option_list:
|
|
|
|
result.append(OptionContainer.format_option_help(self, formatter))
|
|
|
|
result.append("\n")
|
|
|
|
for group in self.option_groups:
|
|
|
|
result.append(group.format_help(formatter))
|
|
|
|
result.append("\n")
|
|
|
|
formatter.dedent()
|
|
|
|
# Drop the last "\n", or the header if no options or option groups:
|
|
|
|
return "".join(result[:-1])
|
|
|
|
|
2006-04-23 00:47:58 -03:00
|
|
|
def format_epilog(self, formatter):
|
|
|
|
return formatter.format_epilog(self.epilog)
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def format_help(self, formatter=None):
|
2002-11-14 18:00:19 -04:00
|
|
|
if formatter is None:
|
|
|
|
formatter = self.formatter
|
|
|
|
result = []
|
|
|
|
if self.usage:
|
|
|
|
result.append(self.get_usage() + "\n")
|
|
|
|
if self.description:
|
|
|
|
result.append(self.format_description(formatter) + "\n")
|
|
|
|
result.append(self.format_option_help(formatter))
|
2006-04-23 00:47:58 -03:00
|
|
|
result.append(self.format_epilog(formatter))
|
2002-11-14 18:00:19 -04:00
|
|
|
return "".join(result)
|
|
|
|
|
2006-06-11 13:24:11 -03:00
|
|
|
# used by test suite
|
|
|
|
def _get_encoding(self, file):
|
2006-07-23 13:05:51 -03:00
|
|
|
encoding = getattr(file, "encoding", None)
|
|
|
|
if not encoding:
|
|
|
|
encoding = sys.getdefaultencoding()
|
|
|
|
return encoding
|
2006-06-11 13:24:11 -03:00
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def print_help(self, file=None):
|
2002-11-14 18:00:19 -04:00
|
|
|
"""print_help(file : file = stdout)
|
|
|
|
|
|
|
|
Print an extended help message, listing all options and any
|
|
|
|
help text provided with them, to 'file' (default stdout).
|
|
|
|
"""
|
|
|
|
if file is None:
|
|
|
|
file = sys.stdout
|
2006-06-11 13:24:11 -03:00
|
|
|
encoding = self._get_encoding(file)
|
|
|
|
file.write(self.format_help().encode(encoding, "replace"))
|
2002-11-14 18:00:19 -04:00
|
|
|
|
|
|
|
# class OptionParser
|
|
|
|
|
|
|
|
|
2004-07-31 13:15:44 -03:00
|
|
|
def _match_abbrev(s, wordmap):
|
2002-11-14 18:00:19 -04:00
|
|
|
"""_match_abbrev(s : string, wordmap : {string : Option}) -> string
|
|
|
|
|
|
|
|
Return the string key in 'wordmap' for which 's' is an unambiguous
|
|
|
|
abbreviation. If 's' is found to be ambiguous or doesn't match any of
|
|
|
|
'words', raise BadOptionError.
|
|
|
|
"""
|
|
|
|
# Is there an exact match?
|
2008-07-10 12:37:08 -03:00
|
|
|
if s in wordmap:
|
2002-11-14 18:00:19 -04:00
|
|
|
return s
|
|
|
|
else:
|
|
|
|
# Isolate all words with s as a prefix.
|
|
|
|
possibilities = [word for word in wordmap.keys()
|
|
|
|
if word.startswith(s)]
|
|
|
|
# No exact match, so there had better be just one possibility.
|
|
|
|
if len(possibilities) == 1:
|
|
|
|
return possibilities[0]
|
|
|
|
elif not possibilities:
|
2006-04-23 00:47:58 -03:00
|
|
|
raise BadOptionError(s)
|
2002-11-14 18:00:19 -04:00
|
|
|
else:
|
|
|
|
# More than one possible completion: ambiguous prefix.
|
2006-05-28 16:13:17 -03:00
|
|
|
possibilities.sort()
|
2006-04-23 00:47:58 -03:00
|
|
|
raise AmbiguousOptionError(s, possibilities)
|
2002-11-14 18:00:19 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Some day, there might be many Option classes. As of Optik 1.3, the
|
|
|
|
# preferred way to instantiate Options is indirectly, via make_option(),
|
|
|
|
# which will become a factory function when there are many Option
|
|
|
|
# classes.
|
|
|
|
make_option = Option
|