Skip to content

Commit 8a45d14

Browse files
authored
Add -FuzzyMinimumDistance parameter to Get-Command (PowerShell#18261)
1 parent 49579e2 commit 8a45d14

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

src/System.Management.Automation/engine/GetCommandCommand.cs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,13 @@ public PSTypeName[] ParameterType
339339
[Parameter(ParameterSetName = "AllCommandSet")]
340340
public SwitchParameter UseFuzzyMatching { get; set; }
341341

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>();
343349

344350
/// <summary>
345351
/// Gets or sets the parameter that determines if return cmdlets based on abbreviation expansion.
@@ -501,13 +507,16 @@ private void OutputResultsHelper(IEnumerable<CommandInfo> results)
501507

502508
if (UseFuzzyMatching)
503509
{
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);
505515
}
506516

507517
int count = 0;
508518
foreach (CommandInfo result in results)
509519
{
510-
count += 1;
511520
// Only write the command if it is visible to the requestor
512521
if (SessionState.IsVisible(origin, result))
513522
{
@@ -532,11 +541,21 @@ private void OutputResultsHelper(IEnumerable<CommandInfo> results)
532541
}
533542
else
534543
{
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+
}
537554
}
538555
}
539556
}
557+
558+
count += 1;
540559
}
541560

542561
#if LEGACYTELEMETRY

test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Describe "Get-Command Feature tests" -Tag Feature {
77
$cmds = Get-Command get-hlp -UseFuzzyMatch
88
$cmds.Count | Should -BeGreaterThan 0
99
$cmds[0].Name | Should -BeExactly 'Get-Help' -Because "This should be closest match so shows up first"
10+
$cmds[0].Score | Should -Be 1
1011
}
1112

1213
It "Should match native commands" {
@@ -20,6 +21,14 @@ Describe "Get-Command Feature tests" -Tag Feature {
2021
$cmds.Count | Should -BeGreaterThan 0
2122
$cmds.Name | Should -Contain $expectedcmd
2223
}
24+
25+
It "Should use minimum distance" {
26+
$cmds = Get-Command get-hlp -UseFuzzyMatch -FuzzyMinimumDistance 3
27+
$cmds.Count | Should -BeGreaterThan 0
28+
foreach ($cmd in $cmds) {
29+
$cmd.Score | Should -BeLessOrEqual 3
30+
}
31+
}
2332
}
2433

2534
Context "-UseAbbreviationExpansion tests" {

0 commit comments

Comments
 (0)