@@ -92,6 +92,7 @@ def do_music(self, args: argparse.Namespace) -> None:
92
92
flag_parser .add_argument ('-c' , '--count_flag' , help = 'count flag' , action = 'count' )
93
93
flag_parser .add_argument ('-s' , '--suppressed_flag' , help = argparse .SUPPRESS , action = 'store_true' )
94
94
flag_parser .add_argument ('-r' , '--remainder_flag' , nargs = argparse .REMAINDER , help = 'a remainder flag' )
95
+ flag_parser .add_argument ('-q' , '--required_flag' , required = True , help = 'a required flag' , action = 'store_true' )
95
96
96
97
@with_argparser (flag_parser )
97
98
def do_flag (self , args : argparse .Namespace ) -> None :
@@ -100,6 +101,7 @@ def do_flag(self, args: argparse.Namespace) -> None:
100
101
# Uses non-default flag prefix value (+)
101
102
plus_flag_parser = Cmd2ArgumentParser (prefix_chars = '+' )
102
103
plus_flag_parser .add_argument ('+n' , '++normal_flag' , help = 'a normal flag' , action = 'store_true' )
104
+ plus_flag_parser .add_argument ('+q' , '++required_flag' , required = True , help = 'a required flag' , action = 'store_true' )
103
105
104
106
@with_argparser (plus_flag_parser )
105
107
def do_plus_flag (self , args : argparse .Namespace ) -> None :
@@ -356,61 +358,77 @@ def test_subcommand_completions(ac_app, subcommand, text, completions):
356
358
assert ac_app .completion_matches == sorted (completions , key = ac_app .default_sort_key )
357
359
358
360
359
- @pytest .mark .parametrize ('command_and_args, text, completions ' , [
361
+ @pytest .mark .parametrize ('command_and_args, text, completion_matches, display_matches ' , [
360
362
# Complete all flags (suppressed will not show)
361
- ('flag' , '-' , ['--append_const_flag' , '--append_flag' , '--count_flag' , '--help' , '--normal_flag' ,
362
- '--remainder_flag' , '-a' , '-c' , '-h' , '-n' , '-o' , '-r' ]),
363
- ('flag' , '--' , ['--append_const_flag' , '--append_flag' , '--count_flag' , '--help' ,
364
- '--normal_flag' , '--remainder_flag' ]),
363
+ ('flag' , '-' ,
364
+ ['--append_const_flag' , '--append_flag' , '--count_flag' , '--help' , '--normal_flag' ,
365
+ '--remainder_flag' , '--required_flag' , '-a' , '-c' , '-h' , '-n' , '-o' , '-q' , '-r' ],
366
+ ['-q, --required_flag' , '[-o, --append_const_flag]' , '[-a, --append_flag]' , '[-c, --count_flag]' , '[-h, --help]' ,
367
+ '[-n, --normal_flag]' , '[-r, --remainder_flag]' ]),
368
+ ('flag' , '--' ,
369
+ ['--append_const_flag' , '--append_flag' , '--count_flag' , '--help' ,
370
+ '--normal_flag' , '--remainder_flag' , '--required_flag' ],
371
+ ['--required_flag' , '[--append_const_flag]' , '[--append_flag]' , '[--count_flag]' , '[--help]' ,
372
+ '[--normal_flag]' , '[--remainder_flag]' ]),
365
373
366
374
# Complete individual flag
367
- ('flag' , '-n' , ['-n ' ]),
368
- ('flag' , '--n' , ['--normal_flag ' ]),
375
+ ('flag' , '-n' , ['-n ' ], [ '[-n]' ] ),
376
+ ('flag' , '--n' , ['--normal_flag ' ], [ '[--normal_flag]' ] ),
369
377
370
378
# No flags should complete until current flag has its args
371
- ('flag --append_flag' , '-' , []),
379
+ ('flag --append_flag' , '-' , [], [] ),
372
380
373
381
# Complete REMAINDER flag name
374
- ('flag' , '-r' , ['-r ' ]),
375
- ('flag' , '--r ' , ['--remainder_flag ' ]),
382
+ ('flag' , '-r' , ['-r ' ], [ '[-r]' ] ),
383
+ ('flag' , '--rem ' , ['--remainder_flag ' ], [ '[--remainder_flag] ' ]),
376
384
377
385
# No flags after a REMAINDER should complete
378
- ('flag -r value' , '-' , []),
379
- ('flag --remainder_flag value' , '--' , []),
386
+ ('flag -r value' , '-' , [], [] ),
387
+ ('flag --remainder_flag value' , '--' , [], [] ),
380
388
381
389
# Suppressed flag should not complete
382
- ('flag' , '-s' , []),
383
- ('flag' , '--s' , []),
390
+ ('flag' , '-s' , [], [] ),
391
+ ('flag' , '--s' , [], [] ),
384
392
385
393
# A used flag should not show in completions
386
- ('flag -n' , '--' , ['--append_const_flag' , '--append_flag' , '--count_flag' , '--help' , '--remainder_flag' ]),
394
+ ('flag -n' , '--' ,
395
+ ['--append_const_flag' , '--append_flag' , '--count_flag' , '--help' , '--remainder_flag' , '--required_flag' ],
396
+ ['--required_flag' , '[--append_const_flag]' , '[--append_flag]' , '[--count_flag]' , '[--help]' , '[--remainder_flag]' ]),
387
397
388
398
# Flags with actions set to append, append_const, and count will always show even if they've been used
389
- ('flag --append_const_flag -c --append_flag value' , '--' , ['--append_const_flag' , '--append_flag' , '--count_flag' ,
390
- '--help' , '--normal_flag' , '--remainder_flag' ]),
399
+ ('flag --append_const_flag -c --append_flag value' , '--' ,
400
+ ['--append_const_flag' , '--append_flag' , '--count_flag' , '--help' ,
401
+ '--normal_flag' , '--remainder_flag' , '--required_flag' ],
402
+ ['--required_flag' , '[--append_const_flag]' , '[--append_flag]' , '[--count_flag]' , '[--help]' ,
403
+ '[--normal_flag]' , '[--remainder_flag]' ]),
391
404
392
405
# Non-default flag prefix character (+)
393
- ('plus_flag' , '+' , ['++help' , '++normal_flag' , '+h' , '+n' ]),
394
- ('plus_flag' , '++' , ['++help' , '++normal_flag' ]),
406
+ ('plus_flag' , '+' ,
407
+ ['++help' , '++normal_flag' , '+h' , '+n' , '+q' , '++required_flag' ],
408
+ ['+q, ++required_flag' , '[+h, ++help]' , '[+n, ++normal_flag]' ]),
409
+ ('plus_flag' , '++' ,
410
+ ['++help' , '++normal_flag' , '++required_flag' ],
411
+ ['++required_flag' , '[++help]' , '[++normal_flag]' ]),
395
412
396
413
# Flag completion should not occur after '--' since that tells argparse all remaining arguments are non-flags
397
- ('flag --' , '--' , []),
398
- ('flag --help --' , '--' , []),
399
- ('plus_flag --' , '++' , []),
400
- ('plus_flag ++help --' , '++' , [])
414
+ ('flag --' , '--' , [], [] ),
415
+ ('flag --help --' , '--' , [], [] ),
416
+ ('plus_flag --' , '++' , [], [] ),
417
+ ('plus_flag ++help --' , '++' , [], [] )
401
418
])
402
- def test_autcomp_flag_completion (ac_app , command_and_args , text , completions ):
419
+ def test_autcomp_flag_completion (ac_app , command_and_args , text , completion_matches , display_matches ):
403
420
line = '{} {}' .format (command_and_args , text )
404
421
endidx = len (line )
405
422
begidx = endidx - len (text )
406
423
407
424
first_match = complete_tester (text , line , begidx , endidx , ac_app )
408
- if completions :
425
+ if completion_matches :
409
426
assert first_match is not None
410
427
else :
411
428
assert first_match is None
412
429
413
- assert ac_app .completion_matches == sorted (completions , key = ac_app .default_sort_key )
430
+ assert (ac_app .completion_matches == sorted (completion_matches , key = ac_app .default_sort_key ) and
431
+ ac_app .display_matches == sorted (display_matches , key = ac_app .default_sort_key ))
414
432
415
433
416
434
@pytest .mark .parametrize ('flag, text, completions' , [
0 commit comments