@@ -161,7 +161,6 @@ def get_option_strings(parser):
161
161
def recurse (parser , prefix ):
162
162
"""recurse through subparsers, appending to the return lists"""
163
163
subparsers = []
164
- option_strings = []
165
164
compgens = []
166
165
choices = []
167
166
nargs = []
@@ -181,21 +180,22 @@ def recurse(parser, prefix):
181
180
182
181
if hasattr (positional , "complete" ):
183
182
# shtab `.complete = ...` functions
184
- compgens .append (u"{}_pos_{}_COMPGEN={}" .format (
185
- prefix , i , complete2pattern (positional .complete , "bash" , choice_type2fn )))
183
+ compgens .append (
184
+ f'{ prefix } _pos_{ i } _COMPGEN={ complete2pattern (positional .complete , "bash" , choice_type2fn )} '
185
+ )
186
+
186
187
187
188
if positional .choices :
188
189
# choices (including subparsers & shtab `.complete` functions)
189
- log .debug ("choices:{}:{}" . format ( prefix , sorted (positional .choices )) )
190
+ log .debug (f "choices:{ prefix } :{ sorted (positional .choices )} " )
190
191
191
192
this_positional_choices = []
192
193
for choice in positional .choices :
193
194
if isinstance (choice , Choice ):
194
195
# append special completion type to `compgens`
195
196
# NOTE: overrides `.complete` attribute
196
- log .debug ("Choice.{}:{}:{}" .format (choice .type , prefix , positional .dest ))
197
- compgens .append (u"{}_pos_{}_COMPGEN={}" .format (
198
- prefix , i , choice_type2fn [choice .type ]))
197
+ log .debug (f"Choice.{ choice .type } :{ prefix } :{ positional .dest } " )
198
+ compgens .append (f"{ prefix } _pos_{ i } _COMPGEN={ choice_type2fn [choice .type ]} " )
199
199
elif isinstance (positional .choices , dict ):
200
200
# subparser, so append to list of subparsers & recurse
201
201
log .debug ("subcommand:%s" , choice )
@@ -211,8 +211,9 @@ def recurse(parser, prefix):
211
211
new_nargs ,
212
212
) = recurse (
213
213
positional .choices [choice ],
214
- prefix + "_" + wordify (choice ),
214
+ f" { prefix } _ { wordify (choice )} " ,
215
215
)
216
+
216
217
sub_subparsers .extend (new_subparsers )
217
218
sub_option_strings .extend (new_option_strings )
218
219
sub_compgens .extend (new_compgens )
@@ -225,31 +226,37 @@ def recurse(parser, prefix):
225
226
this_positional_choices .append (str (choice ))
226
227
227
228
if this_positional_choices :
228
- choices .append (u"{}_pos_{}_choices='{}'" .format (
229
- prefix , i , " " .join (this_positional_choices )))
229
+ choices .append (
230
+ f"""{ prefix } _pos_{ i } _choices='{ " " .join (this_positional_choices )} '"""
231
+ )
232
+
230
233
231
234
# skip default `nargs` values
232
235
if positional .nargs not in (None , "1" , "?" ):
233
- nargs .append (u"{ }_pos_{}_nargs={}" . format ( prefix , i , positional .nargs ) )
236
+ nargs .append (f" { prefix } _pos_{ i } _nargs={ positional .nargs } " )
234
237
235
238
if discovered_subparsers :
236
239
subparsers .append (u"{}_subparsers=('{}')" .format (prefix ,
237
240
"' '" .join (discovered_subparsers )))
238
- log .debug ("subcommands:{}:{}" .format (prefix , discovered_subparsers ))
241
+ log .debug (f"subcommands:{ prefix } :{ discovered_subparsers } " )
242
+
243
+ option_strings = [
244
+ u"{}_option_strings=('{}')" .format (
245
+ prefix , "' '" .join (get_option_strings (parser ))
246
+ )
247
+ ]
239
248
240
- # optional arguments
241
- option_strings .append (u"{}_option_strings=('{}')" .format (
242
- prefix , "' '" .join (get_option_strings (parser ))))
243
249
for optional in parser ._get_optional_actions ():
244
250
if optional == SUPPRESS :
245
251
continue
246
252
247
253
for option_string in optional .option_strings :
248
254
if hasattr (optional , "complete" ):
249
255
# shtab `.complete = ...` functions
250
- compgens .append (u"{}_{}_COMPGEN={}" .format (
251
- prefix , wordify (option_string ),
252
- complete2pattern (optional .complete , "bash" , choice_type2fn )))
256
+ compgens .append (
257
+ f'{ prefix } _{ wordify (option_string )} _COMPGEN={ complete2pattern (optional .complete , "bash" , choice_type2fn )} '
258
+ )
259
+
253
260
254
261
if optional .choices :
255
262
# choices (including shtab `.complete` functions)
@@ -258,21 +265,24 @@ def recurse(parser, prefix):
258
265
# append special completion type to `compgens`
259
266
# NOTE: overrides `.complete` attribute
260
267
if isinstance (choice , Choice ):
261
- log .debug ("Choice.{}:{}:{}" .format (choice .type , prefix , optional .dest ))
262
- compgens .append (u"{}_{}_COMPGEN={}" .format (
263
- prefix , wordify (option_string ), choice_type2fn [choice .type ]))
268
+ log .debug (f"Choice.{ choice .type } :{ prefix } :{ optional .dest } " )
269
+ compgens .append (
270
+ f"{ prefix } _{ wordify (option_string )} _COMPGEN={ choice_type2fn [choice .type ]} "
271
+ )
272
+
264
273
else :
265
274
# simple choice
266
275
this_optional_choices .append (str (choice ))
267
276
268
277
if this_optional_choices :
269
- choices .append (u"{}_{}_choices='{}'" .format (
270
- prefix , wordify (option_string ), " " .join (this_optional_choices )))
278
+ choices .append (
279
+ f"""{ prefix } _{ wordify (option_string )} _choices='{ " " .join (this_optional_choices )} '"""
280
+ )
281
+
271
282
272
283
# Check for nargs.
273
284
if optional .nargs is not None and optional .nargs != 1 :
274
- nargs .append (u"{}_{}_nargs={}" .format (prefix , wordify (option_string ),
275
- optional .nargs ))
285
+ nargs .append (f"{ prefix } _{ wordify (option_string )} _nargs={ optional .nargs } " )
276
286
277
287
# append recursion results
278
288
subparsers .extend (sub_subparsers )
@@ -466,29 +476,53 @@ def complete_zsh(parser, root_prefix=None, preamble="", choice_functions=None):
466
476
choice_type2fn .update (choice_functions )
467
477
468
478
def format_optional (opt ):
469
- return (('{nargs}{options}"[{help}]"' if isinstance (
470
- opt , FLAG_OPTION ) else '{nargs}{options}"[{help}]:{dest}:{pattern}"' ).format (
471
- nargs = ('"(- :)"' if isinstance (opt , OPTION_END ) else
472
- '"*"' if isinstance (opt , OPTION_MULTI ) else "" ),
473
- options = ("{{{}}}" .format ("," .join (opt .option_strings ))
474
- if len (opt .option_strings ) > 1 else '"{}"' .format ("" .join (
475
- opt .option_strings ))),
479
+ return (
480
+ (
481
+ '{nargs}{options}"[{help}]"'
482
+ if isinstance (opt , FLAG_OPTION )
483
+ else '{nargs}{options}"[{help}]:{dest}:{pattern}"'
484
+ )
485
+ .format (
486
+ nargs = (
487
+ '"(- :)"'
488
+ if isinstance (opt , OPTION_END )
489
+ else '"*"'
490
+ if isinstance (opt , OPTION_MULTI )
491
+ else ""
492
+ ),
493
+ options = (
494
+ "{{{}}}" .format ("," .join (opt .option_strings ))
495
+ if len (opt .option_strings ) > 1
496
+ else '"{}"' .format ("" .join (opt .option_strings ))
497
+ ),
476
498
help = escape_zsh (opt .help or "" ),
477
499
dest = opt .dest ,
478
- pattern = complete2pattern (opt .complete , "zsh" , choice_type2fn ) if hasattr (
479
- opt , "complete" ) else
480
- (choice_type2fn [opt .choices [0 ].type ] if isinstance (opt .choices [0 ], Choice ) else
481
- "({})" .format (" " .join (map (str , opt .choices )))) if opt .choices else "" ,
482
- ).replace ('""' , "" ))
500
+ pattern = complete2pattern (opt .complete , "zsh" , choice_type2fn )
501
+ if hasattr (opt , "complete" )
502
+ else (
503
+ choice_type2fn [opt .choices [0 ].type ]
504
+ if isinstance (opt .choices [0 ], Choice )
505
+ else f'({ " " .join (map (str , opt .choices ))} )'
506
+ )
507
+ if opt .choices
508
+ else "" ,
509
+ )
510
+ .replace ('""' , "" )
511
+ )
483
512
484
513
def format_positional (opt ):
485
514
return '"{nargs}:{help}:{pattern}"' .format (
486
515
nargs = {"+" : "(*)" , "*" : "(*):" }.get (opt .nargs , "" ),
487
516
help = escape_zsh ((opt .help or opt .dest ).strip ().split ("\n " )[0 ]),
488
- pattern = complete2pattern (opt .complete , "zsh" , choice_type2fn ) if hasattr (
489
- opt , "complete" ) else
490
- (choice_type2fn [opt .choices [0 ].type ] if isinstance (opt .choices [0 ], Choice ) else
491
- "({})" .format (" " .join (map (str , opt .choices )))) if opt .choices else "" ,
517
+ pattern = complete2pattern (opt .complete , "zsh" , choice_type2fn )
518
+ if hasattr (opt , "complete" )
519
+ else (
520
+ choice_type2fn [opt .choices [0 ].type ]
521
+ if isinstance (opt .choices [0 ], Choice )
522
+ else f'({ " " .join (map (str , opt .choices ))} )'
523
+ )
524
+ if opt .choices
525
+ else "" ,
492
526
)
493
527
494
528
# {cmd: {"help": help, "arguments": [arguments]}}
@@ -506,11 +540,11 @@ def recurse(parser, prefix, paths=None):
506
540
for sub in parser ._get_positional_actions ():
507
541
if sub .help == SUPPRESS or not sub .choices :
508
542
continue
509
- if not sub . choices or not isinstance (sub .choices , dict ):
543
+ if not isinstance (sub .choices , dict ):
510
544
# positional argument
511
545
all_commands [prefix ]["arguments" ].append (format_positional (sub ))
512
546
else : # subparser
513
- log .debug ("choices:{}:{}" . format ( prefix , sorted (sub .choices )) )
547
+ log .debug (f "choices:{ prefix } :{ sorted (sub .choices )} " )
514
548
public_cmds = get_public_subcommands (sub )
515
549
for cmd , subparser in sub .choices .items ():
516
550
if cmd not in public_cmds :
@@ -528,7 +562,7 @@ def recurse(parser, prefix, paths=None):
528
562
format_positional (opt ) for opt in subparser ._get_positional_actions ()
529
563
if not isinstance (opt .choices , dict ) if opt .help != SUPPRESS )
530
564
531
- new_pref = prefix + "_" + wordify (cmd )
565
+ new_pref = f" { prefix } _ { wordify (cmd )} "
532
566
options = all_commands [new_pref ] = {
533
567
"cmd" : cmd , "help" : (subparser .description or "" ).strip ().split ("\n " )[0 ],
534
568
"arguments" : arguments , "paths" : [* paths , cmd ]}
@@ -592,8 +626,11 @@ def command_option(prefix, options):
592
626
593
627
def command_list (prefix , options ):
594
628
name = " " .join ([prog , * options ["paths" ]])
595
- commands = "\n " .join ('"{}:{}"' .format (cmd , escape_zsh (opt ["help" ]))
596
- for cmd , opt in sorted (options ["commands" ].items ()))
629
+ commands = "\n " .join (
630
+ f'"{ cmd } :{ escape_zsh (opt ["help" ])} "'
631
+ for cmd , opt in sorted (options ["commands" ].items ())
632
+ )
633
+
597
634
return """
598
635
{prefix}_commands() {{
599
636
local _commands=(
@@ -656,19 +693,12 @@ def complete_tcsh(parser, root_prefix=None, preamble="", choice_functions=None):
656
693
def get_specials (arg , arg_type , arg_sel ):
657
694
if arg .choices :
658
695
choice_strs = ' ' .join (map (str , arg .choices ))
659
- yield "'{}/{}/({})/'" .format (
660
- arg_type ,
661
- arg_sel ,
662
- choice_strs ,
663
- )
696
+ yield f"'{ arg_type } /{ arg_sel } /({ choice_strs } )/'"
664
697
elif hasattr (arg , "complete" ):
665
- complete_fn = complete2pattern (arg .complete , 'tcsh' , choice_type2fn )
666
- if complete_fn :
667
- yield "'{}/{}/{}/'" .format (
668
- arg_type ,
669
- arg_sel ,
670
- complete_fn ,
671
- )
698
+ if complete_fn := complete2pattern (
699
+ arg .complete , 'tcsh' , choice_type2fn
700
+ ):
701
+ yield f"'{ arg_type } /{ arg_sel } /{ complete_fn } /'"
672
702
673
703
def recurse_parser (cparser , positional_idx , requirements = None ):
674
704
log_prefix = '| ' * positional_idx
0 commit comments