From 980f4875c256bf84e0e089213471f7a7c971dcd9 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Tue, 11 Oct 2022 13:32:06 -0700 Subject: [PATCH 1/6] Add `-FuzzyMinimumDistance` parameter to `Get-Command` --- .../engine/GetCommandCommand.cs | 20 ++++++++++++++++--- .../Get-Command.Tests.ps1 | 9 +++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/System.Management.Automation/engine/GetCommandCommand.cs b/src/System.Management.Automation/engine/GetCommandCommand.cs index e11b3c30f8c..ad4f097f4b5 100644 --- a/src/System.Management.Automation/engine/GetCommandCommand.cs +++ b/src/System.Management.Automation/engine/GetCommandCommand.cs @@ -339,6 +339,12 @@ public PSTypeName[] ParameterType [Parameter(ParameterSetName = "AllCommandSet")] public SwitchParameter UseFuzzyMatching { get; set; } + /// + /// Gets or sets the minimum fuzzy matching distance. + /// + [Parameter(ParameterSetName = "AllCommandSet")] + public uint FuzzyMinimumDistance { get; set; } = 5; + private readonly List _commandScores = new List(); /// @@ -501,7 +507,7 @@ private void OutputResultsHelper(IEnumerable results) if (UseFuzzyMatching) { - results = _commandScores.OrderBy(static x => x.Score).Select(static x => x.Command).ToList(); + results = _commandScores.Where(x => x.Score <= FuzzyMinimumDistance).OrderBy(static x => x.Score).Select(static x => x.Command).ToList(); } int count = 0; @@ -532,8 +538,16 @@ private void OutputResultsHelper(IEnumerable results) } else { - // Write output as normal command info object. - WriteObject(result); + if (UseFuzzyMatching) + { + PSObject obj = new PSObject(result); + obj.Properties.Add(new PSNoteProperty("Score", _commandScores.First(x => x.Command == result).Score)); + WriteObject(obj); + } + else + { + WriteObject(result); + } } } } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 index 1292992b092..a07ed78e753 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 @@ -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" { From d15ce02ee89feee5a888b6851717fbe1b67b2802 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Tue, 11 Oct 2022 16:11:49 -0700 Subject: [PATCH 2/6] Update src/System.Management.Automation/engine/GetCommandCommand.cs Co-authored-by: Dongbo Wang --- .../engine/GetCommandCommand.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/GetCommandCommand.cs b/src/System.Management.Automation/engine/GetCommandCommand.cs index ad4f097f4b5..5283693e2ea 100644 --- a/src/System.Management.Automation/engine/GetCommandCommand.cs +++ b/src/System.Management.Automation/engine/GetCommandCommand.cs @@ -507,7 +507,11 @@ private void OutputResultsHelper(IEnumerable results) if (UseFuzzyMatching) { - results = _commandScores.Where(x => x.Score <= FuzzyMinimumDistance).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; From 540be8aa8e6648a514db7410362aeb534caff361 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Tue, 11 Oct 2022 16:11:55 -0700 Subject: [PATCH 3/6] Update src/System.Management.Automation/engine/GetCommandCommand.cs Co-authored-by: Dongbo Wang --- src/System.Management.Automation/engine/GetCommandCommand.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/GetCommandCommand.cs b/src/System.Management.Automation/engine/GetCommandCommand.cs index 5283693e2ea..8c3f0929223 100644 --- a/src/System.Management.Automation/engine/GetCommandCommand.cs +++ b/src/System.Management.Automation/engine/GetCommandCommand.cs @@ -545,7 +545,7 @@ private void OutputResultsHelper(IEnumerable results) if (UseFuzzyMatching) { PSObject obj = new PSObject(result); - obj.Properties.Add(new PSNoteProperty("Score", _commandScores.First(x => x.Command == result).Score)); + obj.Properties.Add(new PSNoteProperty("Score", _commandScores[count].Score); WriteObject(obj); } else From bd1489a0e571467e7abae5d8c6158795afdc87c4 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Tue, 11 Oct 2022 16:21:22 -0700 Subject: [PATCH 4/6] address Dongbo's feedback --- .../engine/GetCommandCommand.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/System.Management.Automation/engine/GetCommandCommand.cs b/src/System.Management.Automation/engine/GetCommandCommand.cs index 8c3f0929223..959afe9942d 100644 --- a/src/System.Management.Automation/engine/GetCommandCommand.cs +++ b/src/System.Management.Automation/engine/GetCommandCommand.cs @@ -507,17 +507,16 @@ private void OutputResultsHelper(IEnumerable results) if (UseFuzzyMatching) { - _commandScores = _commandScores + results = _commandScores .Where(x => x.Score <= FuzzyMinimumDistance) .OrderBy(static x => x.Score) + .Select(static x => x.Command) .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)) { @@ -545,7 +544,7 @@ private void OutputResultsHelper(IEnumerable results) if (UseFuzzyMatching) { PSObject obj = new PSObject(result); - obj.Properties.Add(new PSNoteProperty("Score", _commandScores[count].Score); + obj.Properties.Add(new PSNoteProperty("Score", _commandScores[count].Score)); WriteObject(obj); } else @@ -555,6 +554,7 @@ private void OutputResultsHelper(IEnumerable results) } } } + count += 1; } #if LEGACYTELEMETRY From 94786e83346d0bdb12931e9aec21dac03f0c8281 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Tue, 11 Oct 2022 17:18:27 -0700 Subject: [PATCH 5/6] align results with _commandScores --- .../engine/GetCommandCommand.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/System.Management.Automation/engine/GetCommandCommand.cs b/src/System.Management.Automation/engine/GetCommandCommand.cs index 959afe9942d..57210d45b3f 100644 --- a/src/System.Management.Automation/engine/GetCommandCommand.cs +++ b/src/System.Management.Automation/engine/GetCommandCommand.cs @@ -345,7 +345,7 @@ public PSTypeName[] ParameterType [Parameter(ParameterSetName = "AllCommandSet")] public uint FuzzyMinimumDistance { get; set; } = 5; - private readonly List _commandScores = new List(); + private List _commandScores = new List(); /// /// Gets or sets the parameter that determines if return cmdlets based on abbreviation expansion. @@ -507,11 +507,11 @@ private void OutputResultsHelper(IEnumerable results) if (UseFuzzyMatching) { - results = _commandScores + _commandScores = _commandScores .Where(x => x.Score <= FuzzyMinimumDistance) .OrderBy(static x => x.Score) - .Select(static x => x.Command) .ToList(); + results = _commandScores.Select(static x => x.Command); } int count = 0; From 63637946f2e4a9dddb6b9a2dd663c9274b09b631 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Wed, 12 Oct 2022 09:40:52 -0700 Subject: [PATCH 6/6] Fix one CodeFactor issue --- src/System.Management.Automation/engine/GetCommandCommand.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/System.Management.Automation/engine/GetCommandCommand.cs b/src/System.Management.Automation/engine/GetCommandCommand.cs index 57210d45b3f..7beb1f32590 100644 --- a/src/System.Management.Automation/engine/GetCommandCommand.cs +++ b/src/System.Management.Automation/engine/GetCommandCommand.cs @@ -554,6 +554,7 @@ private void OutputResultsHelper(IEnumerable results) } } } + count += 1; }