33"""
44import logging
55import subprocess
6+ import sys
67from argparse import ArgumentParser
78
89import pytest
@@ -177,7 +178,7 @@ def test_custom_complete(shell, caplog):
177178def 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
198225def test_add_argument_to_optional (shell , caplog ):
199226 parser = ArgumentParser (prog = "test" )
@@ -213,7 +240,7 @@ def test_add_argument_to_optional(shell, caplog):
213240def 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