Introduce the concept of a foreign argument.
When an embedding application wants to integrate its argument
handling with typ, it can re-use typ's ArgumentParser subclass
and add its own arguments to it. They will be ignored and
not passed to spawned instances of typ.
diff --git a/typ/arg_parser.py b/typ/arg_parser.py
index f7a6026..831d0cd 100644
--- a/typ/arg_parser.py
+++ b/typ/arg_parser.py
@@ -280,6 +280,8 @@
v = d[k]
argname = _argname_from_key(k)
action = self._action_for_key(k)
+ if not action:
+ continue
action_str = _action_str(action)
if k == 'tests':
tests = v
@@ -310,8 +312,9 @@
if action.dest == key:
return action
- assert False, ('Could not find an action for %s' # pragma: no cover
- % key)
+ # Assume foreign argument: something used by the embedder of typ, for
+ # example.
+ return None
def _action_str(action):
diff --git a/typ/tests/arg_parser_test.py b/typ/tests/arg_parser_test.py
index 8e3a7bd..3d443f6 100644
--- a/typ/tests/arg_parser_test.py
+++ b/typ/tests/arg_parser_test.py
@@ -44,6 +44,13 @@
check(['--jobs', '3'])
check(['-vv'], ['--verbose', '--verbose'])
+ def test_argv_from_args_foreign_argument(self):
+ parser = ArgumentParser()
+ parser.add_argument('--some-foreign-argument', default=False,
+ action='store_true')
+ args = parser.parse_args(['--some-foreign-argument', '--verbose'])
+ self.assertEqual(['--verbose'], ArgumentParser().argv_from_args(args))
+
def test_valid_shard_options(self):
parser = ArgumentParser()