Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 # Author: Steven J. Bethard <steven.bethard@gmail.com>. | 1 # Author: Steven J. Bethard <steven.bethard@gmail.com>. |
2 | 2 |
3 """Command-line parsing library | 3 """Command-line parsing library |
4 | 4 |
5 This module is an optparse-inspired command-line parsing library that: | 5 This module is an optparse-inspired command-line parsing library that: |
6 | 6 |
7 - handles both optional and positional arguments | 7 - handles both optional and positional arguments |
8 - produces highly informative usage messages | 8 - produces highly informative usage messages |
9 - supports parsers that dispatch to sub-parsers | 9 - supports parsers that dispatch to sub-parsers |
10 | 10 |
(...skipping 1107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1118 if string == '-': | 1118 if string == '-': |
1119 if 'r' in self._mode: | 1119 if 'r' in self._mode: |
1120 return _sys.stdin | 1120 return _sys.stdin |
1121 elif 'w' in self._mode: | 1121 elif 'w' in self._mode: |
1122 return _sys.stdout | 1122 return _sys.stdout |
1123 else: | 1123 else: |
1124 msg = _('argument "-" with mode %r' % self._mode) | 1124 msg = _('argument "-" with mode %r' % self._mode) |
1125 raise ValueError(msg) | 1125 raise ValueError(msg) |
1126 | 1126 |
1127 # all other arguments are used as file names | 1127 # all other arguments are used as file names |
1128 if self._bufsize: | 1128 try: |
1129 return open(string, self._mode, self._bufsize) | 1129 if self._bufsize: |
1130 else: | 1130 return open(string, self._mode, self._bufsize) |
1131 return open(string, self._mode) | 1131 else: |
1132 return open(string, self._mode) | |
1133 except IOError: | |
1134 err = _sys.exc_info()[1] | |
merwok
2010/11/25 11:28:06
Why are you using sys.exc_info instead of catching
SilentGhost
2010/11/25 11:52:32
I presume it's done to support both 2.7 and 3.x ve
| |
1135 raise ValueError("IOError: %s" % err) | |
1132 | 1136 |
1133 def __repr__(self): | 1137 def __repr__(self): |
1134 args = [self._mode, self._bufsize] | 1138 args = [self._mode, self._bufsize] |
1135 args_str = ', '.join([repr(arg) for arg in args if arg is not None]) | 1139 args_str = ', '.join([repr(arg) for arg in args if arg is not None]) |
1136 return '%s(%s)' % (type(self).__name__, args_str) | 1140 return '%s(%s)' % (type(self).__name__, args_str) |
1137 | 1141 |
1138 # =========================== | 1142 # =========================== |
1139 # Optional and Positional Parsing | 1143 # Optional and Positional Parsing |
1140 # =========================== | 1144 # =========================== |
1141 | 1145 |
(...skipping 561 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1703 if not hasattr(namespace, dest): | 1707 if not hasattr(namespace, dest): |
1704 setattr(namespace, dest, self._defaults[dest]) | 1708 setattr(namespace, dest, self._defaults[dest]) |
1705 | 1709 |
1706 # parse the arguments and exit if there are any errors | 1710 # parse the arguments and exit if there are any errors |
1707 try: | 1711 try: |
1708 namespace, args = self._parse_known_args(args, namespace) | 1712 namespace, args = self._parse_known_args(args, namespace) |
1709 if hasattr(namespace, _UNRECOGNIZED_ARGS_ATTR): | 1713 if hasattr(namespace, _UNRECOGNIZED_ARGS_ATTR): |
1710 args.extend(getattr(namespace, _UNRECOGNIZED_ARGS_ATTR)) | 1714 args.extend(getattr(namespace, _UNRECOGNIZED_ARGS_ATTR)) |
1711 delattr(namespace, _UNRECOGNIZED_ARGS_ATTR) | 1715 delattr(namespace, _UNRECOGNIZED_ARGS_ATTR) |
1712 return namespace, args | 1716 return namespace, args |
1713 except (ArgumentError, IOError): | 1717 except ArgumentError: |
1714 err = _sys.exc_info()[1] | 1718 err = _sys.exc_info()[1] |
1715 self.error(str(err)) | 1719 self.error(str(err)) |
1716 | 1720 |
1717 def _parse_known_args(self, arg_strings, namespace): | 1721 def _parse_known_args(self, arg_strings, namespace): |
1718 # replace arg strings that are file references | 1722 # replace arg strings that are file references |
1719 if self.fromfile_prefix_chars is not None: | 1723 if self.fromfile_prefix_chars is not None: |
1720 arg_strings = self._read_args_from_files(arg_strings) | 1724 arg_strings = self._read_args_from_files(arg_strings) |
1721 | 1725 |
1722 # map all mutually exclusive arguments to the other arguments | 1726 # map all mutually exclusive arguments to the other arguments |
1723 # they can't occur with | 1727 # they can't occur with |
(...skipping 497 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2221 result = type_func(arg_string) | 2225 result = type_func(arg_string) |
2222 | 2226 |
2223 # ArgumentTypeErrors indicate errors | 2227 # ArgumentTypeErrors indicate errors |
2224 except ArgumentTypeError: | 2228 except ArgumentTypeError: |
2225 name = getattr(action.type, '__name__', repr(action.type)) | 2229 name = getattr(action.type, '__name__', repr(action.type)) |
2226 msg = str(_sys.exc_info()[1]) | 2230 msg = str(_sys.exc_info()[1]) |
2227 raise ArgumentError(action, msg) | 2231 raise ArgumentError(action, msg) |
2228 | 2232 |
2229 # TypeErrors or ValueErrors also indicate errors | 2233 # TypeErrors or ValueErrors also indicate errors |
2230 except (TypeError, ValueError): | 2234 except (TypeError, ValueError): |
2235 err = _sys.exc_info()[1] | |
merwok
2010/11/25 11:28:06
Ditto.
| |
2231 name = getattr(action.type, '__name__', repr(action.type)) | 2236 name = getattr(action.type, '__name__', repr(action.type)) |
2232 msg = _('invalid %s value: %r') | 2237 msg = _('invalid %s value: %r details: "%s"') |
2233 raise ArgumentError(action, msg % (name, arg_string)) | 2238 raise ArgumentError(action, msg % (name, arg_string, err)) |
2234 | 2239 |
2235 # return the converted value | 2240 # return the converted value |
2236 return result | 2241 return result |
2237 | 2242 |
2238 def _check_value(self, action, value): | 2243 def _check_value(self, action, value): |
2239 # converted value must be one of the choices (if specified) | 2244 # converted value must be one of the choices (if specified) |
2240 if action.choices is not None and value not in action.choices: | 2245 if action.choices is not None and value not in action.choices: |
2241 tup = value, ', '.join(map(repr, action.choices)) | 2246 tup = value, ', '.join(map(repr, action.choices)) |
2242 msg = _('invalid choice: %r (choose from %s)') % tup | 2247 msg = _('invalid choice: %r (choose from %s)') % tup |
2243 raise ArgumentError(action, msg) | 2248 raise ArgumentError(action, msg) |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2326 """error(message: string) | 2331 """error(message: string) |
2327 | 2332 |
2328 Prints a usage message incorporating the message to stderr and | 2333 Prints a usage message incorporating the message to stderr and |
2329 exits. | 2334 exits. |
2330 | 2335 |
2331 If you override this in a subclass, it should not return -- it | 2336 If you override this in a subclass, it should not return -- it |
2332 should either exit or raise an exception. | 2337 should either exit or raise an exception. |
2333 """ | 2338 """ |
2334 self.print_usage(_sys.stderr) | 2339 self.print_usage(_sys.stderr) |
2335 self.exit(2, _('%s: error: %s\n') % (self.prog, message)) | 2340 self.exit(2, _('%s: error: %s\n') % (self.prog, message)) |
LEFT | RIGHT |