Skip to content

Commit f9b0795

Browse files
authored
Merge pull request #16 from hajs/patch-1
2 parents 0ede804 + 5f72e7e commit f9b0795

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

shtab/main.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ def get_main_parser():
2424
"--prefix", help="prepended to generated functions to avoid clashes"
2525
)
2626
parser.add_argument("--preamble", help="prepended to generated script")
27+
parser.add_argument(
28+
"--prog", help="custom program name (overrides `parser.prog`)"
29+
)
2730
parser.add_argument(
2831
"-u",
2932
"--error-unimportable",
@@ -50,6 +53,8 @@ def main(argv=None):
5053
other_parser = getattr(module, other_parser)
5154
if callable(other_parser):
5255
other_parser = other_parser()
56+
if args.prog:
57+
other_parser.prog = args.prog
5358
print(
5459
complete(
5560
other_parser,

tests/test_shtab.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from shtab.main import get_main_parser, main
1515

1616
SUPPORTED_SHELLS = "bash", "zsh"
17+
fix_shell = pytest.mark.parametrize("shell", SUPPORTED_SHELLS)
1718

1819

1920
class Bash(object):
@@ -70,15 +71,42 @@ def test_choices():
7071
assert "" not in shtab.Required.FILE
7172

7273

73-
@pytest.mark.parametrize("shell", SUPPORTED_SHELLS)
74+
@fix_shell
7475
def test_main(shell, caplog):
7576
with caplog.at_level(logging.INFO):
7677
main(["-s", shell, "shtab.main.get_main_parser"])
7778

7879
assert not caplog.record_tuples
7980

8081

81-
@pytest.mark.parametrize("shell", SUPPORTED_SHELLS)
82+
@fix_shell
83+
def test_prog_override(shell, caplog, capsys):
84+
with caplog.at_level(logging.INFO):
85+
main(["-s", shell, "--prog", "foo", "shtab.main.get_main_parser"])
86+
87+
captured = capsys.readouterr()
88+
assert not captured.err
89+
if shell == "bash":
90+
assert "complete -o nospace -F _shtab_shtab foo" in captured.out
91+
92+
assert not caplog.record_tuples
93+
94+
95+
@fix_shell
96+
def test_prefix_override(shell, caplog, capsys):
97+
with caplog.at_level(logging.INFO):
98+
main(["-s", shell, "--prefix", "foo", "shtab.main.get_main_parser"])
99+
100+
captured = capsys.readouterr()
101+
assert not captured.err
102+
if shell == "bash":
103+
shell = Bash(captured.out)
104+
shell.compgen('-W "$_shtab_foo_options_"', "--h", "--help")
105+
106+
assert not caplog.record_tuples
107+
108+
109+
@fix_shell
82110
def test_complete(shell, caplog):
83111
parser = get_main_parser()
84112
with caplog.at_level(logging.INFO):
@@ -92,7 +120,7 @@ def test_complete(shell, caplog):
92120
assert not caplog.record_tuples
93121

94122

95-
@pytest.mark.parametrize("shell", SUPPORTED_SHELLS)
123+
@fix_shell
96124
def test_positional_choices(shell, caplog):
97125
parser = ArgumentParser(prog="test")
98126
parser.add_argument("posA", choices=["one", "two"])

0 commit comments

Comments
 (0)