Skip to content

Commit 1e1d6ef

Browse files
authored
bump version, merge pull request #47 from jimporter/subparser-help
2 parents 76c5ead + b1bf591 commit 1e1d6ef

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

shtab/__init__.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ def wordify(string):
141141
return string.replace("-", "_").replace(".", " ").replace(" ", "_")
142142

143143

144+
def get_public_subcommands(sub):
145+
"""Get all the publicly-visible subcommands for a given subparser."""
146+
public_parsers = {id(sub.choices[i.dest]) for i in sub._get_subactions()}
147+
return {k for k, v in sub.choices.items() if id(v) in public_parsers}
148+
149+
144150
def get_bash_commands(root_parser, root_prefix, choice_functions=None):
145151
"""
146152
Recursive subcommand parser traversal, returning lists of information on
@@ -222,7 +228,8 @@ def recurse(parser, prefix):
222228
elif isinstance(positional.choices, dict):
223229
# subparser, so append to list of subparsers & recurse
224230
log.debug("subcommand:%s", choice)
225-
if positional.choices[choice].add_help:
231+
public_cmds = get_public_subcommands(positional)
232+
if choice in public_cmds:
226233
discovered_subparsers.append(str(choice))
227234
this_positional_choices.append(str(choice))
228235
(
@@ -577,8 +584,9 @@ def format_positional(opt):
577584
root_arguments.append(format_positional(opt))
578585
else: # subparser
579586
log.debug("choices:{}:{}".format(root_prefix, sorted(sub.choices)))
587+
public_cmds = get_public_subcommands(sub)
580588
for cmd, subparser in sub.choices.items():
581-
if not subparser.add_help:
589+
if cmd not in public_cmds:
582590
log.debug("skip:subcommand:%s", cmd)
583591
continue
584592
log.debug("subcommand:%s", cmd)

tests/test_shtab.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44
import logging
55
import subprocess
6+
import sys
67
from argparse import ArgumentParser
78

89
import pytest
@@ -177,7 +178,7 @@ def test_custom_complete(shell, caplog):
177178
def test_subparser_custom_complete(shell, caplog):
178179
parser = ArgumentParser(prog="test")
179180
subparsers = parser.add_subparsers()
180-
sub = subparsers.add_parser("sub")
181+
sub = subparsers.add_parser("sub", help="help message")
181182
sub.add_argument("posA").complete = {"bash": "_shtab_test_some_func"}
182183
preamble = {"bash": "_shtab_test_some_func() { compgen -W 'one two' -- $1 ;}"}
183184
with caplog.at_level(logging.INFO):
@@ -194,6 +195,32 @@ def test_subparser_custom_complete(shell, caplog):
194195
assert not caplog.record_tuples
195196

196197

198+
@fix_shell
199+
@pytest.mark.skipif(sys.version_info[0] == 2, reason="requires Python 3.x")
200+
def test_subparser_aliases(shell, caplog):
201+
parser = ArgumentParser(prog="test")
202+
subparsers = parser.add_subparsers()
203+
sub = subparsers.add_parser("sub", aliases=["xsub", "ysub"], help="help message")
204+
sub.add_argument("posA").complete = {"bash": "_shtab_test_some_func"}
205+
preamble = {"bash": "_shtab_test_some_func() { compgen -W 'one two' -- $1 ;}"}
206+
with caplog.at_level(logging.INFO):
207+
completion = shtab.complete(parser, shell=shell, preamble=preamble)
208+
print(completion)
209+
210+
if shell == "bash":
211+
shell = Bash(completion)
212+
shell.compgen('-W "${_shtab_test_subparsers[*]}"', "s", "sub")
213+
shell.compgen('-W "$_shtab_test_pos_0_choices"', "s", "sub")
214+
shell.compgen('-W "${_shtab_test_subparsers[*]}"', "x", "xsub")
215+
shell.compgen('-W "$_shtab_test_pos_0_choices"', "x", "xsub")
216+
shell.compgen('-W "${_shtab_test_subparsers[*]}"', "y", "ysub")
217+
shell.compgen('-W "$_shtab_test_pos_0_choices"', "y", "ysub")
218+
shell.test('"$($_shtab_test_sub_pos_0_COMPGEN o)" = "one"')
219+
shell.test('-z "$_shtab_test_COMPGEN"')
220+
221+
assert not caplog.record_tuples
222+
223+
197224
@fix_shell
198225
def test_add_argument_to_optional(shell, caplog):
199226
parser = ArgumentParser(prog="test")
@@ -213,7 +240,7 @@ def test_add_argument_to_optional(shell, caplog):
213240
def test_add_argument_to_positional(shell, caplog, capsys):
214241
parser = ArgumentParser(prog="test")
215242
subparsers = parser.add_subparsers()
216-
sub = subparsers.add_parser("completion")
243+
sub = subparsers.add_parser("completion", help="help message")
217244
shtab.add_argument_to(sub, "shell", parent=parser)
218245
from argparse import Namespace
219246

0 commit comments

Comments
 (0)