@@ -426,13 +426,7 @@ fn longs_and_visible_aliases(p: &clap::Command) -> Vec<CompletionCandidate> {
426
426
. filter_map ( |a| {
427
427
a. get_long_and_visible_aliases ( ) . map ( |longs| {
428
428
longs. into_iter ( ) . map ( |s| {
429
- CompletionCandidate :: new ( format ! ( "--{}" , s) )
430
- . help ( a. get_help ( ) . cloned ( ) )
431
- . id ( Some ( format ! ( "arg::{}" , a. get_id( ) ) ) )
432
- . tag ( Some (
433
- a. get_help_heading ( ) . unwrap_or ( "Options" ) . to_owned ( ) . into ( ) ,
434
- ) )
435
- . hide ( a. is_hide_set ( ) )
429
+ populate_arg_candidate ( CompletionCandidate :: new ( format ! ( "--{}" , s) ) , a)
436
430
} )
437
431
} )
438
432
} )
@@ -448,12 +442,7 @@ fn hidden_longs_aliases(p: &clap::Command) -> Vec<CompletionCandidate> {
448
442
. filter_map ( |a| {
449
443
a. get_aliases ( ) . map ( |longs| {
450
444
longs. into_iter ( ) . map ( |s| {
451
- CompletionCandidate :: new ( format ! ( "--{}" , s) )
452
- . help ( a. get_help ( ) . cloned ( ) )
453
- . id ( Some ( format ! ( "arg::{}" , a. get_id( ) ) ) )
454
- . tag ( Some (
455
- a. get_help_heading ( ) . unwrap_or ( "Options" ) . to_owned ( ) . into ( ) ,
456
- ) )
445
+ populate_arg_candidate ( CompletionCandidate :: new ( format ! ( "--{}" , s) ) , a)
457
446
. hide ( true )
458
447
} )
459
448
} )
@@ -471,24 +460,31 @@ fn shorts_and_visible_aliases(p: &clap::Command) -> Vec<CompletionCandidate> {
471
460
. filter_map ( |a| {
472
461
a. get_short_and_visible_aliases ( ) . map ( |shorts| {
473
462
shorts. into_iter ( ) . map ( |s| {
474
- CompletionCandidate :: new ( s. to_string ( ) )
475
- . help (
476
- a. get_help ( )
477
- . cloned ( )
478
- . or_else ( || a. get_long ( ) . map ( |long| format ! ( "--{long}" ) . into ( ) ) ) ,
479
- )
480
- . id ( Some ( format ! ( "arg::{}" , a. get_id( ) ) ) )
481
- . tag ( Some (
482
- a. get_help_heading ( ) . unwrap_or ( "Options" ) . to_owned ( ) . into ( ) ,
483
- ) )
484
- . hide ( a. is_hide_set ( ) )
463
+ populate_arg_candidate ( CompletionCandidate :: new ( s. to_string ( ) ) , a) . help (
464
+ a. get_help ( )
465
+ . cloned ( )
466
+ . or_else ( || a. get_long ( ) . map ( |long| format ! ( "--{long}" ) . into ( ) ) ) ,
467
+ )
485
468
} )
486
469
} )
487
470
} )
488
471
. flatten ( )
489
472
. collect ( )
490
473
}
491
474
475
+ fn populate_arg_candidate ( candidate : CompletionCandidate , arg : & clap:: Arg ) -> CompletionCandidate {
476
+ candidate
477
+ . help ( arg. get_help ( ) . cloned ( ) )
478
+ . id ( Some ( format ! ( "arg::{}" , arg. get_id( ) ) ) )
479
+ . tag ( Some (
480
+ arg. get_help_heading ( )
481
+ . unwrap_or ( "Options" )
482
+ . to_owned ( )
483
+ . into ( ) ,
484
+ ) )
485
+ . hide ( arg. is_hide_set ( ) )
486
+ }
487
+
492
488
/// Get the possible values for completion
493
489
fn possible_values ( a : & clap:: Arg ) -> Option < Vec < clap:: builder:: PossibleValue > > {
494
490
if !a. get_num_args ( ) . expect ( "built" ) . takes_values ( ) {
@@ -511,34 +507,31 @@ fn subcommands(p: &clap::Command) -> Vec<CompletionCandidate> {
511
507
. flat_map ( |sc| {
512
508
sc. get_name_and_visible_aliases ( )
513
509
. into_iter ( )
514
- . map ( |s| {
515
- CompletionCandidate :: new ( s. to_string ( ) )
516
- . help ( sc. get_about ( ) . cloned ( ) )
517
- . id ( Some ( format ! ( "command::{}" , sc. get_name( ) ) ) )
518
- . tag ( Some (
519
- p. get_subcommand_help_heading ( )
520
- . unwrap_or ( "Commands" )
521
- . to_owned ( )
522
- . into ( ) ,
523
- ) )
524
- . hide ( sc. is_hide_set ( ) )
525
- } )
510
+ . map ( |s| populate_command_candidate ( CompletionCandidate :: new ( s. to_string ( ) ) , p, sc) )
526
511
. chain ( sc. get_aliases ( ) . map ( |s| {
527
- CompletionCandidate :: new ( s. to_string ( ) )
528
- . help ( sc. get_about ( ) . cloned ( ) )
529
- . id ( Some ( format ! ( "command::{}" , sc. get_name( ) ) ) )
530
- . tag ( Some (
531
- p. get_subcommand_help_heading ( )
532
- . unwrap_or ( "Commands" )
533
- . to_owned ( )
534
- . into ( ) ,
535
- ) )
512
+ populate_command_candidate ( CompletionCandidate :: new ( s. to_string ( ) ) , p, sc)
536
513
. hide ( true )
537
514
} ) )
538
515
} )
539
516
. collect ( )
540
517
}
541
518
519
+ fn populate_command_candidate (
520
+ candidate : CompletionCandidate ,
521
+ cmd : & clap:: Command ,
522
+ subcommand : & clap:: Command ,
523
+ ) -> CompletionCandidate {
524
+ candidate
525
+ . help ( subcommand. get_about ( ) . cloned ( ) )
526
+ . id ( Some ( format ! ( "command::{}" , subcommand. get_name( ) ) ) )
527
+ . tag ( Some (
528
+ cmd. get_subcommand_help_heading ( )
529
+ . unwrap_or ( "Commands" )
530
+ . to_owned ( )
531
+ . into ( ) ,
532
+ ) )
533
+ . hide ( subcommand. is_hide_set ( ) )
534
+ }
542
535
/// Parse the short flags and find the first `takes_values` option.
543
536
fn parse_shortflags < ' c , ' s > (
544
537
cmd : & ' c clap:: Command ,
0 commit comments