Show More
@@ -10,3 +10,4 try: | |||||
10 | from argparse import * |
|
10 | from argparse import * | |
11 | except ImportError: |
|
11 | except ImportError: | |
12 | from _argparse import * |
|
12 | from _argparse import * | |
|
13 | from _argparse import SUPPRESS |
@@ -1,4 +1,18 | |||||
1 | # Author: Steven J. Bethard <steven.bethard@gmail.com>. |
|
1 | # -*- coding: utf-8 -*- | |
|
2 | ||||
|
3 | # Copyright © 2006-2009 Steven J. Bethard <steven.bethard@gmail.com>. | |||
|
4 | # | |||
|
5 | # Licensed under the Apache License, Version 2.0 (the "License"); you may not | |||
|
6 | # use this file except in compliance with the License. You may obtain a copy | |||
|
7 | # of the License at | |||
|
8 | # | |||
|
9 | # http://www.apache.org/licenses/LICENSE-2.0 | |||
|
10 | # | |||
|
11 | # Unless required by applicable law or agreed to in writing, software | |||
|
12 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | |||
|
13 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | |||
|
14 | # License for the specific language governing permissions and limitations | |||
|
15 | # under the License. | |||
2 |
|
16 | |||
3 | """Command-line parsing library |
|
17 | """Command-line parsing library | |
4 |
|
18 | |||
@@ -83,10 +97,40 import textwrap as _textwrap | |||||
83 |
|
97 | |||
84 | from gettext import gettext as _ |
|
98 | from gettext import gettext as _ | |
85 |
|
99 | |||
|
100 | try: | |||
|
101 | _set = set | |||
|
102 | except NameError: | |||
|
103 | from sets import Set as _set | |||
|
104 | ||||
|
105 | try: | |||
|
106 | _basestring = basestring | |||
|
107 | except NameError: | |||
|
108 | _basestring = str | |||
|
109 | ||||
|
110 | try: | |||
|
111 | _sorted = sorted | |||
|
112 | except NameError: | |||
|
113 | ||||
|
114 | def _sorted(iterable, reverse=False): | |||
|
115 | result = list(iterable) | |||
|
116 | result.sort() | |||
|
117 | if reverse: | |||
|
118 | result.reverse() | |||
|
119 | return result | |||
|
120 | ||||
86 |
|
121 | |||
87 | def _callable(obj): |
|
122 | def _callable(obj): | |
88 | return hasattr(obj, '__call__') or hasattr(obj, '__bases__') |
|
123 | return hasattr(obj, '__call__') or hasattr(obj, '__bases__') | |
89 |
|
124 | |||
|
125 | # silence Python 2.6 buggy warnings about Exception.message | |||
|
126 | if _sys.version_info[:2] == (2, 6): | |||
|
127 | import warnings | |||
|
128 | warnings.filterwarnings( | |||
|
129 | action='ignore', | |||
|
130 | message='BaseException.message has been deprecated as of Python 2.6', | |||
|
131 | category=DeprecationWarning, | |||
|
132 | module='argparse') | |||
|
133 | ||||
90 |
|
134 | |||
91 | SUPPRESS = '==SUPPRESS==' |
|
135 | SUPPRESS = '==SUPPRESS==' | |
92 |
|
136 | |||
@@ -119,7 +163,7 class _AttributeHolder(object): | |||||
119 | return '%s(%s)' % (type_name, ', '.join(arg_strings)) |
|
163 | return '%s(%s)' % (type_name, ', '.join(arg_strings)) | |
120 |
|
164 | |||
121 | def _get_kwargs(self): |
|
165 | def _get_kwargs(self): | |
122 | return sorted(self.__dict__.items()) |
|
166 | return _sorted(self.__dict__.items()) | |
123 |
|
167 | |||
124 | def _get_args(self): |
|
168 | def _get_args(self): | |
125 | return [] |
|
169 | return [] | |
@@ -372,7 +416,7 class HelpFormatter(object): | |||||
372 |
|
416 | |||
373 | def _format_actions_usage(self, actions, groups): |
|
417 | def _format_actions_usage(self, actions, groups): | |
374 | # find group indices and identify actions in groups |
|
418 | # find group indices and identify actions in groups | |
375 | group_actions = set() |
|
419 | group_actions = _set() | |
376 | inserts = {} |
|
420 | inserts = {} | |
377 | for group in groups: |
|
421 | for group in groups: | |
378 | try: |
|
422 | try: | |
@@ -442,7 +486,7 class HelpFormatter(object): | |||||
442 | parts.append(part) |
|
486 | parts.append(part) | |
443 |
|
487 | |||
444 | # insert things at the necessary indices |
|
488 | # insert things at the necessary indices | |
445 | for i in sorted(inserts, reverse=True): |
|
489 | for i in _sorted(inserts, reverse=True): | |
446 | parts[i:i] = [inserts[i]] |
|
490 | parts[i:i] = [inserts[i]] | |
447 |
|
491 | |||
448 | # join all the action items with spaces |
|
492 | # join all the action items with spaces | |
@@ -987,7 +1031,7 class _VersionAction(Action): | |||||
987 | version=None, |
|
1031 | version=None, | |
988 | dest=SUPPRESS, |
|
1032 | dest=SUPPRESS, | |
989 | default=SUPPRESS, |
|
1033 | default=SUPPRESS, | |
990 | help="show program's version number and exit"): |
|
1034 | help=None): | |
991 | super(_VersionAction, self).__init__( |
|
1035 | super(_VersionAction, self).__init__( | |
992 | option_strings=option_strings, |
|
1036 | option_strings=option_strings, | |
993 | dest=dest, |
|
1037 | dest=dest, | |
@@ -1131,8 +1175,6 class Namespace(_AttributeHolder): | |||||
1131 | for name in kwargs: |
|
1175 | for name in kwargs: | |
1132 | setattr(self, name, kwargs[name]) |
|
1176 | setattr(self, name, kwargs[name]) | |
1133 |
|
1177 | |||
1134 | __hash__ = None |
|
|||
1135 |
|
||||
1136 | def __eq__(self, other): |
|
1178 | def __eq__(self, other): | |
1137 | return vars(self) == vars(other) |
|
1179 | return vars(self) == vars(other) | |
1138 |
|
1180 | |||
@@ -1674,7 +1716,7 class ArgumentParser(_AttributeHolder, _ActionsContainer): | |||||
1674 | if not hasattr(namespace, action.dest): |
|
1716 | if not hasattr(namespace, action.dest): | |
1675 | if action.default is not SUPPRESS: |
|
1717 | if action.default is not SUPPRESS: | |
1676 | default = action.default |
|
1718 | default = action.default | |
1677 | if isinstance(action.default, basestring): |
|
1719 | if isinstance(action.default, _basestring): | |
1678 | default = self._get_value(action, default) |
|
1720 | default = self._get_value(action, default) | |
1679 | setattr(namespace, action.dest, default) |
|
1721 | setattr(namespace, action.dest, default) | |
1680 |
|
1722 | |||
@@ -1734,8 +1776,8 class ArgumentParser(_AttributeHolder, _ActionsContainer): | |||||
1734 | arg_strings_pattern = ''.join(arg_string_pattern_parts) |
|
1776 | arg_strings_pattern = ''.join(arg_string_pattern_parts) | |
1735 |
|
1777 | |||
1736 | # converts arg strings to the appropriate and then takes the action |
|
1778 | # converts arg strings to the appropriate and then takes the action | |
1737 | seen_actions = set() |
|
1779 | seen_actions = _set() | |
1738 | seen_non_default_actions = set() |
|
1780 | seen_non_default_actions = _set() | |
1739 |
|
1781 | |||
1740 | def take_action(action, argument_strings, option_string=None): |
|
1782 | def take_action(action, argument_strings, option_string=None): | |
1741 | seen_actions.add(action) |
|
1783 | seen_actions.add(action) | |
@@ -2148,7 +2190,7 class ArgumentParser(_AttributeHolder, _ActionsContainer): | |||||
2148 | value = action.const |
|
2190 | value = action.const | |
2149 | else: |
|
2191 | else: | |
2150 | value = action.default |
|
2192 | value = action.default | |
2151 | if isinstance(value, basestring): |
|
2193 | if isinstance(value, _basestring): | |
2152 | value = self._get_value(action, value) |
|
2194 | value = self._get_value(action, value) | |
2153 | self._check_value(action, value) |
|
2195 | self._check_value(action, value) | |
2154 |
|
2196 |
General Comments 0
You need to be logged in to leave comments.
Login now