Skip to content

Commit 04e1564

Browse files
Confirm and WhatIf parameters should have constant descriptions (#768)
1 parent b9c080b commit 04e1564

File tree

5 files changed

+49
-5
lines changed

5 files changed

+49
-5
lines changed

src/Common/MergeUtils.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.ComponentModel;
67
using System.Linq;
78
using Microsoft.PowerShell.PlatyPS.Model;
89

@@ -238,7 +239,7 @@ internal static bool TryGetMergedParameters(List<Parameter>fromHelp, List<Parame
238239
var dm = new DiagnosticMessage(DiagnosticMessageSource.Merge, $"updating {pName}.", DiagnosticSeverity.Information, "TryGetMergedParameters", -1);
239240
diagnosticMessages.Add(dm);
240241

241-
var checkTemplate = string.Format(Constants.FillInParameterDescriptionTemplate, helpParam.Name);
242+
var checkTemplate = TransformUtils.GetParameterTemplateString(helpParam.Name);
242243

243244
var description = helpParam.Description;
244245

src/Model/Constants.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Text;
77
using System.Linq;
88
using System.IO;
9+
using System.Runtime.CompilerServices;
910

1011
namespace Microsoft.PowerShell.PlatyPS.Model
1112
{
@@ -84,6 +85,8 @@ internal static partial class Constants
8485
internal const string FillInGuid = "XXXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX";
8586
internal const string LocaleEnUs = "en-US";
8687
internal const string skippingMessageFmt = "'{0}' exists, skipping. Use -Force to overwrite.";
88+
internal const string ConfirmParameterDescription = "Prompts you for confirmation before running the cmdlet.";
89+
internal const string WhatIfParameterDescription = "Tells PowerShell to run the command in a mode that only reports what would happen, but not actually let the command run or make changes.";
8790

8891
// TODO: ProgressAction is not a common parameter for all versions of PowerShell.
8992
// This should not be added under all circumstances.

src/Transform/TransformBase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ protected IEnumerable<Parameter> GetParameters(CommandInfo cmdletInfo, dynamic?
236236

237237
string descriptionFromHelp = GetParameterDescriptionFromHelp(helpItem, param.Name) ?? param.HelpMessage ?? string.Empty;
238238
param.Description = string.IsNullOrEmpty(descriptionFromHelp) ?
239-
string.Format(Constants.FillInParameterDescriptionTemplate, param.Name) :
239+
TransformUtils.GetParameterTemplateString(param.Name) :
240240
descriptionFromHelp.Trim();
241241

242242
parameters.Add(param);
@@ -517,7 +517,7 @@ private string GetAbbreviatedType(Type type)
517517
}
518518
else
519519
{
520-
if (TranformUtils.TryGetTypeAbbreviation(type.FullName, out string abbreviation))
520+
if (TransformUtils.TryGetTypeAbbreviation(type.FullName, out string abbreviation))
521521
{
522522
return abbreviation;
523523
}
@@ -613,7 +613,7 @@ protected Parameter GetParameterInfo(CommandInfo? cmdletInfo, dynamic? helpItem,
613613
string descriptionFromHelp = GetParameterDescriptionFromHelp(helpItem, param.Name) ?? paramAttribInfo.HelpMessage ?? string.Empty;
614614

615615
param.Description = string.IsNullOrEmpty(descriptionFromHelp) ?
616-
string.Format(Constants.FillInParameterDescriptionTemplate, param.Name) :
616+
TransformUtils.GetParameterTemplateString(param.Name) :
617617
descriptionFromHelp;
618618

619619
param.Aliases = paramInfo.Aliases.ToList();

src/Transform/TransformUtils.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
namespace Microsoft.PowerShell.PlatyPS
1717
{
18-
public class TranformUtils
18+
public class TransformUtils
1919
{
2020
private static Dictionary<string, string> TypeAbbreviations = new Dictionary<string, string> {
2121
{ "System.Management.Automation.AliasAttribute" , "Alias" },
@@ -123,5 +123,26 @@ public static bool TryGetTypeAbbreviation(string fullName, out string abbreviati
123123

124124
return false;
125125
}
126+
127+
public static string GetParameterTemplateString(string paramName)
128+
{
129+
if (string.IsNullOrEmpty(paramName))
130+
{
131+
throw new ArgumentException("Parameter name cannot be null or empty.", nameof(paramName));
132+
}
133+
134+
if (string.Equals(paramName, "Confirm", StringComparison.OrdinalIgnoreCase))
135+
{
136+
return Constants.ConfirmParameterDescription;
137+
}
138+
else if (string.Equals(paramName, "WhatIf", StringComparison.OrdinalIgnoreCase))
139+
{
140+
return Constants.WhatIfParameterDescription;
141+
}
142+
else
143+
{
144+
return string.Format(Constants.FillInParameterDescriptionTemplate, paramName);
145+
}
146+
}
126147
}
127148
}

test/Pester/NewMarkdownHelp.Tests.ps1

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,4 +638,23 @@ Write-Host 'Hello World!'
638638
$file | Should -FileContentMatch 'SupportsWildcards: true'
639639
}
640640
}
641+
642+
Context 'Confirm Whatif description' {
643+
BeforeAll {
644+
function global:Test-ConfirmWhatIfDescription {
645+
[CmdletBinding(SupportsShouldProcess = $true)]
646+
param (
647+
[Parameter()]
648+
[string] $Name
649+
)
650+
}
651+
652+
$file = New-MarkdownCommandHelp -Command (Get-Command 'Test-ConfirmWhatIfDescription') -OutputFolder "$TestDrive/NewMarkDownHelp"
653+
}
654+
655+
It 'should have a description for Confirm and WhatIf parameters' {
656+
$file | Should -FileContentMatch 'Prompts you for confirmation before running the cmdlet.'
657+
$file | Should -FileContentMatch 'Tells PowerShell to run the command in a mode that only reports what would happen, but not actually let the command run or make changes.'
658+
}
659+
}
641660
}

0 commit comments

Comments
 (0)