@@ -339,7 +339,13 @@ public PSTypeName[] ParameterType
339
339
[ Parameter ( ParameterSetName = "AllCommandSet" ) ]
340
340
public SwitchParameter UseFuzzyMatching { get ; set ; }
341
341
342
- private readonly List < CommandScore > _commandScores = new List < CommandScore > ( ) ;
342
+ /// <summary>
343
+ /// Gets or sets the minimum fuzzy matching distance.
344
+ /// </summary>
345
+ [ Parameter ( ParameterSetName = "AllCommandSet" ) ]
346
+ public uint FuzzyMinimumDistance { get ; set ; } = 5 ;
347
+
348
+ private List < CommandScore > _commandScores = new List < CommandScore > ( ) ;
343
349
344
350
/// <summary>
345
351
/// Gets or sets the parameter that determines if return cmdlets based on abbreviation expansion.
@@ -501,13 +507,16 @@ private void OutputResultsHelper(IEnumerable<CommandInfo> results)
501
507
502
508
if ( UseFuzzyMatching )
503
509
{
504
- results = _commandScores . OrderBy ( static x => x . Score ) . Select ( static x => x . Command ) . ToList ( ) ;
510
+ _commandScores = _commandScores
511
+ . Where ( x => x . Score <= FuzzyMinimumDistance )
512
+ . OrderBy ( static x => x . Score )
513
+ . ToList ( ) ;
514
+ results = _commandScores . Select ( static x => x . Command ) ;
505
515
}
506
516
507
517
int count = 0 ;
508
518
foreach ( CommandInfo result in results )
509
519
{
510
- count += 1 ;
511
520
// Only write the command if it is visible to the requestor
512
521
if ( SessionState . IsVisible ( origin , result ) )
513
522
{
@@ -532,11 +541,21 @@ private void OutputResultsHelper(IEnumerable<CommandInfo> results)
532
541
}
533
542
else
534
543
{
535
- // Write output as normal command info object.
536
- WriteObject ( result ) ;
544
+ if ( UseFuzzyMatching )
545
+ {
546
+ PSObject obj = new PSObject ( result ) ;
547
+ obj . Properties . Add ( new PSNoteProperty ( "Score" , _commandScores [ count ] . Score ) ) ;
548
+ WriteObject ( obj ) ;
549
+ }
550
+ else
551
+ {
552
+ WriteObject ( result ) ;
553
+ }
537
554
}
538
555
}
539
556
}
557
+
558
+ count += 1 ;
540
559
}
541
560
542
561
#if LEGACYTELEMETRY
0 commit comments