Add -FuzzyMinimumDistance parameter to Get-Command (#18261)

This commit is contained in:
Steve Lee
2022-10-12 16:08:20 -07:00
committed by GitHub
parent 49579e221f
commit 8a45d14fad
2 changed files with 33 additions and 5 deletions
@@ -339,7 +339,13 @@ namespace Microsoft.PowerShell.Commands
[Parameter(ParameterSetName = "AllCommandSet")]
public SwitchParameter UseFuzzyMatching { get; set; }
private readonly List<CommandScore> _commandScores = new List<CommandScore>();
/// <summary>
/// Gets or sets the minimum fuzzy matching distance.
/// </summary>
[Parameter(ParameterSetName = "AllCommandSet")]
public uint FuzzyMinimumDistance { get; set; } = 5;
private List<CommandScore> _commandScores = new List<CommandScore>();
/// <summary>
/// Gets or sets the parameter that determines if return cmdlets based on abbreviation expansion.
@@ -501,13 +507,16 @@ namespace Microsoft.PowerShell.Commands
if (UseFuzzyMatching)
{
results = _commandScores.OrderBy(static x => x.Score).Select(static x => x.Command).ToList();
_commandScores = _commandScores
.Where(x => x.Score <= FuzzyMinimumDistance)
.OrderBy(static x => x.Score)
.ToList();
results = _commandScores.Select(static x => x.Command);
}
int count = 0;
foreach (CommandInfo result in results)
{
count += 1;
// Only write the command if it is visible to the requestor
if (SessionState.IsVisible(origin, result))
{
@@ -532,11 +541,21 @@ namespace Microsoft.PowerShell.Commands
}
else
{
// Write output as normal command info object.
WriteObject(result);
if (UseFuzzyMatching)
{
PSObject obj = new PSObject(result);
obj.Properties.Add(new PSNoteProperty("Score", _commandScores[count].Score));
WriteObject(obj);
}
else
{
WriteObject(result);
}
}
}
}
count += 1;
}
#if LEGACYTELEMETRY
@@ -7,6 +7,7 @@ Describe "Get-Command Feature tests" -Tag Feature {
$cmds = Get-Command get-hlp -UseFuzzyMatch
$cmds.Count | Should -BeGreaterThan 0
$cmds[0].Name | Should -BeExactly 'Get-Help' -Because "This should be closest match so shows up first"
$cmds[0].Score | Should -Be 1
}
It "Should match native commands" {
@@ -20,6 +21,14 @@ Describe "Get-Command Feature tests" -Tag Feature {
$cmds.Count | Should -BeGreaterThan 0
$cmds.Name | Should -Contain $expectedcmd
}
It "Should use minimum distance" {
$cmds = Get-Command get-hlp -UseFuzzyMatch -FuzzyMinimumDistance 3
$cmds.Count | Should -BeGreaterThan 0
foreach ($cmd in $cmds) {
$cmd.Score | Should -BeLessOrEqual 3
}
}
}
Context "-UseAbbreviationExpansion tests" {