| 2013 | return self._parse_known_args2(args, namespace, intermixed=False) |
| 2014 | |
| 2015 | def _parse_known_args2(self, args, namespace, intermixed): |
| 2016 | if args is None: |
| 2017 | # args default to the system args |
| 2018 | args = _sys.argv[1:] |
| 2019 | else: |
| 2020 | # make sure that args are mutable |
| 2021 | args = list(args) |
| 2022 | |
| 2023 | # default Namespace built from parser defaults |
| 2024 | if namespace is None: |
| 2025 | namespace = Namespace() |
| 2026 | |
| 2027 | # add any action defaults that aren't present |
| 2028 | for action in self._actions: |
| 2029 | if action.dest is not SUPPRESS: |
| 2030 | if not hasattr(namespace, action.dest): |
| 2031 | if action.default is not SUPPRESS: |
| 2032 | setattr(namespace, action.dest, action.default) |
| 2033 | |
| 2034 | # add any parser defaults that aren't present |
| 2035 | for dest in self._defaults: |
| 2036 | if not hasattr(namespace, dest): |
| 2037 | setattr(namespace, dest, self._defaults[dest]) |
| 2038 | |
| 2039 | # parse the arguments and exit if there are any errors |
| 2040 | if self.exit_on_error: |
| 2041 | try: |
| 2042 | namespace, args = self._parse_known_args(args, namespace, intermixed) |
| 2043 | except ArgumentError as err: |
| 2044 | self.error(str(err)) |
| 2045 | else: |
| 2046 | namespace, args = self._parse_known_args(args, namespace, intermixed) |
| 2047 | |
| 2048 | if hasattr(namespace, _UNRECOGNIZED_ARGS_ATTR): |
| 2049 | args.extend(getattr(namespace, _UNRECOGNIZED_ARGS_ATTR)) |
| 2050 | delattr(namespace, _UNRECOGNIZED_ARGS_ATTR) |
| 2051 | return namespace, args |
| 2052 | |
| 2053 | def _parse_known_args(self, arg_strings, namespace, intermixed): |
| 2054 | # replace arg strings that are file references |