Skip to content
Myr

test_bashcomplete.py

This file

Path

tests/test_bashcomplete.py

297 lines · 11.2 KiB · Python

Last changed by

Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75

History of this file

Take it

bashcomplete: Adapt to changes for chain commandsMarkus Unterwaditzerbefore adoption37011e7

lines 1–5why, one step back
1# -*- coding: utf-8 -*-
23import click4from click._bashcomplete import get_choices5

Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f

line 6why, one step back
6

Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75

lines 7–15why, one step back
7def choices_without_help(cli, args, incomplete):
8 completions = get_choices(cli, 'dummy', args, incomplete)9 return [c[0] for c in completions]101112def choices_with_help(cli, args, incomplete):13 return list(get_choices(cli, 'dummy', args, incomplete))1415

Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f

lines 16–21why, one step back
16def test_single_command():
17 @click.command()18 @click.option('--local-opt')19 def cli(local_opt):20 pass21

Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75

lines 22–23why, one step back
22 assert choices_without_help(cli, [], '-') == ['--local-opt']
23 assert choices_without_help(cli, [], '') == []

Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f

lines 24–25why, one step back
24
25

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 26–31why, one step back
26def test_boolean_flag():
27 @click.command()28 @click.option('--shout/--no-shout', default=False)29 def cli(local_opt):30 pass31

Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75

line 32why, one step back
32 assert choices_without_help(cli, [], '-') == ['--shout', '--no-shout']

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 33–45why, one step back
33
3435def test_multi_value_option():36 @click.group()37 @click.option('--pos', nargs=2, type=float)38 def cli(local_opt):39 pass4041 @cli.command()42 @click.option('--local-opt')43 def sub(local_opt):44 pass45

Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75

lines 46–49why, one step back
46 assert choices_without_help(cli, [], '-') == ['--pos']
47 assert choices_without_help(cli, ['--pos'], '') == []48 assert choices_without_help(cli, ['--pos', '1.0'], '') == []49 assert choices_without_help(cli, ['--pos', '1.0', '1.0'], '') == ['sub']

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 50–57why, one step back
50
5152def test_multi_option():53 @click.command()54 @click.option('--message', '-m', multiple=True)55 def cli(local_opt):56 pass57

Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75

lines 58–59why, one step back
58 assert choices_without_help(cli, [], '-') == ['--message', '-m']
59 assert choices_without_help(cli, ['-m'], '') == []

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 60–61why, one step back
60
61

Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f

line 62why, one step back
62def test_small_chain():

bashcomplete: Adapt to changes for chain commandsMarkus Unterwaditzerbefore adoption37011e7

lines 63–72why, one step back
63 @click.group()
64 @click.option('--global-opt')65 def cli(global_opt):66 pass6768 @cli.command()69 @click.option('--local-opt')70 def sub(local_opt):71 pass72

Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75

lines 73–76why, one step back
73 assert choices_without_help(cli, [], '') == ['sub']
74 assert choices_without_help(cli, [], '-') == ['--global-opt']75 assert choices_without_help(cli, ['sub'], '') == []76 assert choices_without_help(cli, ['sub'], '-') == ['--local-opt']

Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f

lines 77–94why, one step back
77
7879def test_long_chain():80 @click.group('cli')81 @click.option('--cli-opt')82 def cli(cli_opt):83 pass8485 @cli.group('asub')86 @click.option('--asub-opt')87 def asub(asub_opt):88 pass8990 @asub.group('bsub')91 @click.option('--bsub-opt')92 def bsub(bsub_opt):93 pass94

Merge pull request #755 from stopthatcow/feature/bash_autocompletion_6.6Markus Unterwaditzerbefore adoption5f2ff4f

lines 95–97why, one step back
95 COLORS = ['red', 'green', 'blue']
96 def get_colors(ctx, args, incomplete):97 for c in COLORS:

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

lines 98–99why, one step back
98 if c.startswith(incomplete):
99 yield c

Merge pull request #755 from stopthatcow/feature/bash_autocompletion_6.6Markus Unterwaditzerbefore adoption5f2ff4f

line 100why, one step back
100

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

lines 101–105why, one step back
101 def search_colors(ctx, args, incomplete):
102 for c in COLORS:103 if incomplete in c:104 yield c105

Merge pull request #755 from stopthatcow/feature/bash_autocompletion_6.6Markus Unterwaditzerbefore adoption5f2ff4f

lines 106–107why, one step back
106 CSUB_OPT_CHOICES = ['foo', 'bar']
107 CSUB_CHOICES = ['bar', 'baz']

Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f

line 108why, one step back
108 @bsub.command('csub')

Merge pull request #755 from stopthatcow/feature/bash_autocompletion_6.6Markus Unterwaditzerbefore adoption5f2ff4f

lines 109–110why, one step back
109 @click.option('--csub-opt', type=click.Choice(CSUB_OPT_CHOICES))
110 @click.option('--csub', type=click.Choice(CSUB_CHOICES))

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

line 111why, one step back
111 @click.option('--search-color', autocompletion=search_colors)

Merge pull request #755 from stopthatcow/feature/bash_autocompletion_6.6Markus Unterwaditzerbefore adoption5f2ff4f

lines 112–113why, one step back
112 @click.argument('color', autocompletion=get_colors)
113 def csub(csub_opt, color):

Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f

lines 114–115why, one step back
114 pass
115

Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75

lines 116–129why, one step back
116 assert choices_without_help(cli, [], '-') == ['--cli-opt']
117 assert choices_without_help(cli, [], '') == ['asub']118 assert choices_without_help(cli, ['asub'], '-') == ['--asub-opt']119 assert choices_without_help(cli, ['asub'], '') == ['bsub']120 assert choices_without_help(cli, ['asub', 'bsub'], '-') == ['--bsub-opt']121 assert choices_without_help(cli, ['asub', 'bsub'], '') == ['csub']122 assert choices_without_help(cli, ['asub', 'bsub', 'csub'], '-') == ['--csub-opt', '--csub', '--search-color']123 assert choices_without_help(cli, ['asub', 'bsub', 'csub', '--csub-opt'], '') == CSUB_OPT_CHOICES124 assert choices_without_help(cli, ['asub', 'bsub', 'csub'], '--csub') == ['--csub-opt', '--csub']125 assert choices_without_help(cli, ['asub', 'bsub', 'csub', '--csub'], '') == CSUB_CHOICES126 assert choices_without_help(cli, ['asub', 'bsub', 'csub', '--csub-opt'], 'f') == ['foo']127 assert choices_without_help(cli, ['asub', 'bsub', 'csub'], '') == COLORS128 assert choices_without_help(cli, ['asub', 'bsub', 'csub'], 'b') == ['blue']129 assert choices_without_help(cli, ['asub', 'bsub', 'csub', '--search-color'], 'een') == ['green']

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 130–131why, one step back
130
131

Merge branch 'stopthatcow-feature/fix_chained_args_completion'Markus Unterwaditzerbefore adoptionb224988

lines 132–137why, one step back
132def test_chaining():
133 @click.group('cli', chain=True)134 @click.option('--cli-opt')135 def cli(cli_opt):136 pass137

Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75

line 138why, one step back
138 @cli.command()

Merge branch 'stopthatcow-feature/fix_chained_args_completion'Markus Unterwaditzerbefore adoptionb224988

lines 139–142why, one step back
139 @click.option('--asub-opt')
140 def asub(asub_opt):141 pass142

Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75

line 143why, one step back
143 @cli.command(help='bsub help')

Merge branch 'stopthatcow-feature/fix_chained_args_completion'Markus Unterwaditzerbefore adoptionb224988

line 144why, one step back
144 @click.option('--bsub-opt')

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

line 145why, one step back
145 @click.argument('arg', type=click.Choice(['arg1', 'arg2']), required=True)

Merge branch 'stopthatcow-feature/fix_chained_args_completion'Markus Unterwaditzerbefore adoptionb224988

lines 146–148why, one step back
146 def bsub(bsub_opt, arg):
147 pass148

Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75

line 149why, one step back
149 @cli.command()

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

lines 150–154why, one step back
150 @click.option('--csub-opt')
151 @click.argument('arg', type=click.Choice(['carg1', 'carg2']), required=False)152 def csub(csub_opt, arg):153 pass154

Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75

lines 155–164why, one step back
155 assert choices_without_help(cli, [], '-') == ['--cli-opt']
156 assert choices_without_help(cli, [], '') == ['asub', 'bsub', 'csub']157 assert choices_without_help(cli, ['asub'], '-') == ['--asub-opt']158 assert choices_without_help(cli, ['asub'], '') == ['bsub', 'csub']159 assert choices_without_help(cli, ['bsub'], '') == ['arg1', 'arg2']160 assert choices_without_help(cli, ['asub', '--asub-opt'], '') == []161 assert choices_without_help(cli, ['asub', '--asub-opt', '5', 'bsub'], '-') == ['--bsub-opt']162 assert choices_without_help(cli, ['asub', 'bsub'], '-') == ['--bsub-opt']163 assert choices_with_help(cli, ['asub'], 'b') == [('bsub', 'bsub help')]164 assert choices_without_help(cli, ['asub', 'csub'], '-') == ['--csub-opt']

Merge branch 'stopthatcow-feature/fix_chained_args_completion'Markus Unterwaditzerbefore adoptionb224988

lines 165–166why, one step back
165
166

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 167–174why, one step back
167def test_argument_choice():
168 @click.command()169 @click.argument('arg1', required=False, type=click.Choice(['arg11', 'arg12']))170 @click.argument('arg2', required=False, type=click.Choice(['arg21', 'arg22']))171 @click.argument('arg3', required=False, type=click.Choice(['arg', 'argument']))172 def cli():173 pass174

Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75

lines 175–179why, one step back
175 assert choices_without_help(cli, [], '') == ['arg11', 'arg12']
176 assert choices_without_help(cli, [], 'arg') == ['arg11', 'arg12']177 assert choices_without_help(cli, ['arg11'], '') == ['arg21', 'arg22']178 assert choices_without_help(cli, ['arg12', 'arg21'], '') == ['arg', 'argument']179 assert choices_without_help(cli, ['arg12', 'arg21'], 'argu') == ['argument']

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 180–183why, one step back
180
181182def test_option_choice():183 @click.command()

Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75

line 184why, one step back
184 @click.option('--opt1', type=click.Choice(['opt11', 'opt12']), help='opt1 help')

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 185–189why, one step back
185 @click.option('--opt2', type=click.Choice(['opt21', 'opt22']))
186 @click.option('--opt3', type=click.Choice(['opt', 'option']))187 def cli():188 pass189

Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75

lines 190–204why, one step back
190 assert choices_with_help(cli, [], '-') == [('--opt1', 'opt1 help'),
191 ('--opt2', None),192 ('--opt3', None)]193 assert choices_without_help(cli, [], '--opt') == ['--opt1', '--opt2', '--opt3']194 assert choices_without_help(cli, [], '--opt1=') == ['opt11', 'opt12']195 assert choices_without_help(cli, [], '--opt2=') == ['opt21', 'opt22']196 assert choices_without_help(cli, ['--opt2'], '=') == ['opt21', 'opt22']197 assert choices_without_help(cli, ['--opt2', '='], 'opt') == ['opt21', 'opt22']198 assert choices_without_help(cli, ['--opt1'], '') == ['opt11', 'opt12']199 assert choices_without_help(cli, ['--opt2'], '') == ['opt21', 'opt22']200 assert choices_without_help(cli, ['--opt1', 'opt11', '--opt2'], '') == ['opt21', 'opt22']201 assert choices_without_help(cli, ['--opt2', 'opt21'], '-') == ['--opt1', '--opt3']202 assert choices_without_help(cli, ['--opt1', 'opt11'], '-') == ['--opt2', '--opt3']203 assert choices_without_help(cli, ['--opt1'], 'opt') == ['opt11', 'opt12']204 assert choices_without_help(cli, ['--opt3'], 'opti') == ['option']

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

line 205why, one step back
205

Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75

line 206why, one step back
206 assert choices_without_help(cli, ['--opt1', 'invalid_opt'], '-') == ['--opt2', '--opt3']

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 207–216why, one step back
207
208209def test_option_and_arg_choice():210 @click.command()211 @click.option('--opt1', type=click.Choice(['opt11', 'opt12']))212 @click.argument('arg1', required=False, type=click.Choice(['arg11', 'arg12']))213 @click.option('--opt2', type=click.Choice(['opt21', 'opt22']))214 def cli():215 pass216

Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75

lines 217–220why, one step back
217 assert choices_without_help(cli, ['--opt1'], '') == ['opt11', 'opt12']
218 assert choices_without_help(cli, [''], '--opt1=') == ['opt11', 'opt12']219 assert choices_without_help(cli, [], '') == ['arg11', 'arg12']220 assert choices_without_help(cli, ['--opt2'], '') == ['opt21', 'opt22']

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 221–229why, one step back
221
222223def test_boolean_flag_choice():224 @click.command()225 @click.option('--shout/--no-shout', default=False)226 @click.argument('arg', required=False, type=click.Choice(['arg1', 'arg2']))227 def cli(local_opt):228 pass229

Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75

lines 230–231why, one step back
230 assert choices_without_help(cli, [], '-') == ['--shout', '--no-shout']
231 assert choices_without_help(cli, ['--shout'], '') == ['arg1', 'arg2']

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 232–240why, one step back
232
233234def test_multi_value_option_choice():235 @click.command()236 @click.option('--pos', nargs=2, type=click.Choice(['pos1', 'pos2']))237 @click.argument('arg', required=False, type=click.Choice(['arg1', 'arg2']))238 def cli(local_opt):239 pass240

Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75

lines 241–244why, one step back
241 assert choices_without_help(cli, ['--pos'], '') == ['pos1', 'pos2']
242 assert choices_without_help(cli, ['--pos', 'pos1'], '') == ['pos1', 'pos2']243 assert choices_without_help(cli, ['--pos', 'pos1', 'pos2'], '') == ['arg1', 'arg2']244 assert choices_without_help(cli, ['--pos', 'pos1', 'pos2', 'arg1'], '') == []

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 245–253why, one step back
245
246247def test_multi_option_choice():248 @click.command()249 @click.option('--message', '-m', multiple=True, type=click.Choice(['m1', 'm2']))250 @click.argument('arg', required=False, type=click.Choice(['arg1', 'arg2']))251 def cli(local_opt):252 pass253

Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75

lines 254–256why, one step back
254 assert choices_without_help(cli, ['-m'], '') == ['m1', 'm2']
255 assert choices_without_help(cli, ['-m', 'm1', '-m'], '') == ['m1', 'm2']256 assert choices_without_help(cli, ['-m', 'm1'], '') == ['arg1', 'arg2']

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 257–264why, one step back
257
258259def test_variadic_argument_choice():260 @click.command()261 @click.argument('src', nargs=-1, type=click.Choice(['src1', 'src2']))262 def cli(local_opt):263 pass264

Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75

line 265why, one step back
265 assert choices_without_help(cli, ['src1', 'src2'], '') == ['src1', 'src2']

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 266–272why, one step back
266
267268def test_long_chain_choice():269 @click.group()270 def cli():271 pass272

Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75

line 273why, one step back
273 @cli.group()

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 274–278why, one step back
274 @click.option('--sub-opt', type=click.Choice(['subopt1', 'subopt2']))
275 @click.argument('sub-arg', required=False, type=click.Choice(['subarg1', 'subarg2']))276 def sub(sub_opt):277 pass278

Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75

line 279why, one step back
279 @sub.command(short_help='bsub help')

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 280–285why, one step back
280 @click.option('--bsub-opt', type=click.Choice(['bsubopt1', 'bsubopt2']))
281 @click.argument('bsub-arg1', required=False, type=click.Choice(['bsubarg1', 'bsubarg2']))282 @click.argument('bbsub-arg2', required=False, type=click.Choice(['bbsubarg1', 'bbsubarg2']))283 def bsub(bsub_opt):284 pass285

Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75

lines 286–288why, one step back
286 assert choices_with_help(cli, ['sub'], '') == [('subarg1', None), ('subarg2', None), ('bsub', 'bsub help')]
287 assert choices_without_help(cli, ['sub', '--sub-opt'], '') == ['subopt1', 'subopt2']288 assert choices_without_help(cli, ['sub', '--sub-opt', 'subopt1'], '') == \

Merge pull request #806 from stopthatcow/feature/790Markus Unterwaditzerbefore adoption5210849

line 289why, one step back
289 ['subarg1', 'subarg2', 'bsub']

Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75

lines 290–293why, one step back
290 assert choices_without_help(cli,
291 ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub'], '-') == ['--bsub-opt']292 assert choices_without_help(cli,293 ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub', '--bsub-opt'], '') == \

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

line 294why, one step back
294 ['bsubopt1', 'bsubopt2']

Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75

line 295why, one step back
295 assert choices_without_help(cli,

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

line 296why, one step back
296 ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub', '--bsub-opt', 'bsubopt1', 'bsubarg1'],

Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75

line 297why, one step back
297 '') == ['bbsubarg1', 'bbsubarg2']