diff --git a/examples/customcomplete.py b/examples/customcomplete.py
index f539187..aa24d4e 100755
--- a/examples/customcomplete.py
+++ b/examples/customcomplete.py
@@ -40,7 +40,7 @@ def get_main_parser():
     subparsers.dest = "subcommand"
 
     parser = subparsers.add_parser("completion")
-    shtab.add_argument_to(parser, "shell")  # magic!
+    shtab.add_argument_to(parser, "shell", parent=main_parser)  # magic!
 
     parser = subparsers.add_parser("process")
     # `*.txt` file tab completion
diff --git a/shtab/__init__.py b/shtab/__init__.py
index 3592d89..25e1f09 100644
--- a/shtab/__init__.py
+++ b/shtab/__init__.py
@@ -602,22 +602,27 @@ def complete(
     )
 
 
-class PrintCompletionAction(Action):
-    def __call__(self, parser, namespace, values, option_string=None):
-        print(complete(parser, values))
-        parser.exit(0)
+def completion_action(parent=None):
+    class PrintCompletionAction(Action):
+        def __call__(self, parser, namespace, values, option_string=None):
+            print(complete(parent or parser, values))
+            parser.exit(0)
+
+    return PrintCompletionAction
 
 
 def add_argument_to(
     parser,
     option_string="--print-completion",
     help="print shell completion script",
+    parent=None,
 ):
     """
     parser  : argparse.ArgumentParser
     option_string  : str or list[str], iff positional (no `-` prefix) then
       `parser` is assumed to actually be a subparser (subcommand mode)
     help  : str
+    parent  : argparse.ArgumentParser, required in subcommand mode
     """
     if isinstance(
         option_string, str if sys.version_info[0] > 2 else basestring  # NOQA
@@ -627,9 +632,10 @@ def add_argument_to(
         choices=SUPPORTED_SHELLS,
         default=None,
         help=help,
-        action=PrintCompletionAction,
+        action=completion_action(parent),
     )
     if option_string[0][0] != "-":  # subparser mode
         kwargs.update(default=SUPPORTED_SHELLS[0], nargs="?")
+        assert parent is not None, "subcommand mode: parent required"
     parser.add_argument(*option_string, **kwargs)
     return parser

diff --git a/tests/test_shtab.py b/tests/test_shtab.py
index 54625ab..2819848 100644
--- a/tests/test_shtab.py
+++ b/tests/test_shtab.py
@@ -214,17 +214,27 @@ def test_add_argument_to_optional(shell, caplog):
 
 
 @fix_shell
-def test_add_argument_to_positional(shell, caplog):
+def test_add_argument_to_positional(shell, caplog, capsys):
     parser = ArgumentParser(prog="test")
     subparsers = parser.add_subparsers()
     sub = subparsers.add_parser("completion")
-    shtab.add_argument_to(sub, "shell")
+    shtab.add_argument_to(sub, "shell", parent=parser)
+    from argparse import Namespace
+
     with caplog.at_level(logging.INFO):
-        completion = shtab.complete(parser, shell=shell)
+        completion_manual = shtab.complete(parser, shell=shell)
+        with pytest.raises(SystemExit) as exc:
+            sub._actions[-1](sub, Namespace(), shell)
+            assert exc.type == SystemExit
+            assert exc.vaue.code == 0
+    completion, err = capsys.readouterr()
     print(completion)
+    assert completion_manual == completion.rstrip()
+    assert not err
 
     if shell == "bash":
         shell = Bash(completion)
+        shell.compgen('-W "$_shtab_test_commands_"', "c", "completion")
         shell.compgen('-W "$_shtab_test_completion"', "ba", "bash")
         shell.compgen('-W "$_shtab_test_completion"', "z", "zsh")
 

subcommand convenience wrapper doesn't work
https://github.com/iterative/shtab/blob/a01e2b592102496a3a7b6861b448d0d6e78407f4/examples/customcomplete.py#L43 provides completion only for the `subparser` rather than the full (parent) `parser`
tests/test_shtab.py::test_add_argument_to_positional[bash] tests/test_shtab.py::test_add_argument_to_positional[zsh]
tests/test_shtab.py::test_bash[export tests/test_shtab.py::test_bash[--z tests/test_shtab.py::test_bash_compgen tests/test_shtab.py::test_choices tests/test_shtab.py::test_main[bash] tests/test_shtab.py::test_main[zsh] tests/test_shtab.py::test_prog_override[bash] tests/test_shtab.py::test_prog_override[zsh] tests/test_shtab.py::test_prog_scripts[bash] tests/test_shtab.py::test_prog_scripts[zsh] tests/test_shtab.py::test_prefix_override[bash] tests/test_shtab.py::test_prefix_override[zsh] tests/test_shtab.py::test_complete[bash] tests/test_shtab.py::test_complete[zsh] tests/test_shtab.py::test_positional_choices[bash] tests/test_shtab.py::test_positional_choices[zsh] tests/test_shtab.py::test_custom_complete[bash] tests/test_shtab.py::test_custom_complete[zsh] tests/test_shtab.py::test_subparser_custom_complete[bash] tests/test_shtab.py::test_subparser_custom_complete[zsh] tests/test_shtab.py::test_add_argument_to_optional[bash] tests/test_shtab.py::test_add_argument_to_optional[zsh] tests/test_shtab.py::test_get_completer[bash] tests/test_shtab.py::test_get_completer[zsh] tests/test_shtab.py::test_get_completer_invalid