Skip to content

Commit 730cb8c

Browse files
Merge pull request #69 from Splaxi/implement-show-psmdsyntax
import draft
2 parents 757e7cd + 2ccbda0 commit 730cb8c

File tree

3 files changed

+218
-1
lines changed

3 files changed

+218
-1
lines changed

PSModuleDevelopment/PSModuleDevelopment.psd1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
'Set-PSMDCmdletBinding',
9090
'Set-PSMDModulePath',
9191
'Set-PSMDParameterHelp',
92+
'Show-PSMDSyntax',
9293
'Split-PSMDScriptFile'
9394
)
9495

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
function Show-PSMDSyntax {
2+
<#
3+
.SYNOPSIS
4+
Validate or show parameter set details with colored output
5+
6+
.DESCRIPTION
7+
Analyze a function and it's parameters
8+
9+
The cmdlet / function is capable of validating a string input with function name and parameters
10+
11+
.PARAMETER CommandText
12+
The string that you want to analyze
13+
14+
If there is parameter value present, you have to use the opposite quote strategy to encapsulate the string correctly
15+
16+
E.g. for double quotes
17+
-CommandText 'New-Item -Path "c:\temp\newfile.txt"'
18+
19+
E.g. for single quotes
20+
-CommandText "New-Item -Path 'c:\temp\newfile.txt'"
21+
22+
.PARAMETER Mode
23+
The operation mode of the cmdlet / function
24+
25+
Valid options are:
26+
- Validate
27+
- ShowParameters
28+
29+
.PARAMETER Legend
30+
Include a legend explaining the color mapping
31+
32+
.EXAMPLE
33+
PS C:\> Show-PSMDSyntax -CommandText "New-Item -Path 'c:\temp\newfile.txt'"
34+
35+
This will validate all the parameters that have been passed to the Import-D365Bacpac cmdlet.
36+
All supplied parameters that matches a parameter will be marked with an asterisk.
37+
38+
.EXAMPLE
39+
PS C:\> Show-PSMDSyntax -CommandText "New-Item" -Mode "ShowParameters"
40+
41+
This will display all the parameter sets and their individual parameters.
42+
43+
.NOTES
44+
Author: Mötz Jensen (@Splaxi)
45+
Twitter: https://twitter.com/splaxi
46+
Original github project: https://github.com/d365collaborative/d365fo.tools
47+
48+
#>
49+
[CmdletBinding()]
50+
51+
param (
52+
[Parameter(Mandatory = $true, Position = 1)]
53+
[string] $CommandText,
54+
55+
[Parameter(Mandatory = $true, Position = 2)]
56+
[ValidateSet('Validate', 'ShowParameters')]
57+
[string] $Mode = 'Validate',
58+
59+
[switch] $Legend
60+
)
61+
62+
$commonParameters = 'Verbose', 'Debug', 'ErrorAction', 'WarningAction', 'InformationAction', 'ErrorVariable', 'WarningVariable', 'InformationVariable', 'OutVariable', 'OutBuffer', 'PipelineVariable', 'Confirm', 'WhatIf'
63+
64+
$colorParmsNotFound = Get-PSFConfigValue -FullName "PSModuleDevelopment.ShowSyntax.ParmsNotFound"
65+
$colorCommandName = Get-PSFConfigValue -FullName "PSModuleDevelopment.ShowSyntax.CommandName"
66+
$colorMandatoryParam = Get-PSFConfigValue -FullName "PSModuleDevelopment.ShowSyntax.MandatoryParam"
67+
$colorNonMandatoryParam = Get-PSFConfigValue -FullName "PSModuleDevelopment.ShowSyntax.NonMandatoryParam"
68+
$colorFoundAsterisk = Get-PSFConfigValue -FullName "PSModuleDevelopment.ShowSyntax.FoundAsterisk"
69+
$colorNotFoundAsterisk = Get-PSFConfigValue -FullName "PSModuleDevelopment.ShowSyntax.NotFoundAsterisk"
70+
$colParmValue = Get-PSFConfigValue -FullName "PSModuleDevelopment.ShowSyntax.ParmValue"
71+
72+
#Match to find the command name: Non-Whitespace until the first whitespace
73+
$commandMatch = ($CommandText | Select-String '\S+\s*').Matches
74+
75+
if ($null -eq $commandMatch) {
76+
Write-PSFMessage -Level Host -Message "The function was unable to extract a valid command name from the supplied command text. Please try again."
77+
Stop-PSFFunction -Message "Stopping because of missing command name."
78+
return
79+
}
80+
81+
$commandName = $commandMatch.Value.Trim()
82+
83+
$res = Get-Command $commandName -ErrorAction Ignore
84+
85+
if ($null -eq $res) {
86+
Write-PSFMessage -Level Host -Message "The function was unable to get the help of the command. Make sure that the command name is valid and try again."
87+
Stop-PSFFunction -Message "Stopping because command name didn't return any help."
88+
return
89+
}
90+
91+
$sbHelp = New-Object System.Text.StringBuilder
92+
$sbParmsNotFound = New-Object System.Text.StringBuilder
93+
94+
switch ($Mode) {
95+
"Validate" {
96+
#Match to find the parameters: Whitespace Dash Non-Whitespace
97+
$inputParameterMatch = ($CommandText | Select-String '\s{1}[-]\S+' -AllMatches).Matches
98+
99+
if (-not ($null -eq $inputParameterMatch)) {
100+
$inputParameterNames = $inputParameterMatch.Value.Trim("-", " ")
101+
Write-PSFMessage -Level Verbose -Message "All input parameters - $($inputParameterNames -join ",")" -Target ($inputParameterNames -join ",")
102+
}
103+
else {
104+
Write-PSFMessage -Level Host -Message "The function was unable to extract any parameters from the supplied command text. Please try again."
105+
Stop-PSFFunction -Message "Stopping because of missing input parameters."
106+
return
107+
}
108+
109+
$availableParameterNames = (Get-Command $commandName).Parameters.keys | Where-Object {$commonParameters -NotContains $_}
110+
Write-PSFMessage -Level Verbose -Message "Available parameters - $($availableParameterNames -join ",")" -Target ($availableParameterNames -join ",")
111+
112+
$inputParameterNotFound = $inputParameterNames | Where-Object {$availableParameterNames -NotContains $_}
113+
114+
if ($inputParameterNotFound.Length -gt 0) {
115+
$null = $sbParmsNotFound.AppendLine("Parameters that <c='em'>don't exists</c>")
116+
$inputParameterNotFound | ForEach-Object {
117+
$null = $sbParmsNotFound.AppendLine("<c='$colorParmsNotFound'>$($_)</c>")
118+
}
119+
}
120+
121+
foreach ($parmSet in (Get-Command $commandName).ParameterSets) {
122+
$sb = New-Object System.Text.StringBuilder
123+
$null = $sb.AppendLine("ParameterSet Name: <c='em'>$($parmSet.Name)</c> - Validated List")
124+
$null = $sb.Append("<c='$colorCommandName'>$commandName </c>")
125+
126+
$parmSetParameters = $parmSet.Parameters | Where-Object name -NotIn $commonParameters
127+
128+
foreach ($parameter in $parmSetParameters) {
129+
$parmFoundInCommandText = $parameter.Name -In $inputParameterNames
130+
131+
$color = "$colorNonMandatoryParam"
132+
133+
if ($parameter.IsMandatory -eq $true) { $color = "$colorMandatoryParam" }
134+
135+
$null = $sb.Append("<c='$color'>-$($parameter.Name)</c>")
136+
137+
if ($parmFoundInCommandText) {
138+
$null = $sb.Append("<c='$colorFoundAsterisk'>* </c>")
139+
}
140+
elseif ($parameter.IsMandatory -eq $true) {
141+
$null = $sb.Append("<c='$colorNotFoundAsterisk'>* </c>")
142+
}
143+
else {
144+
$null = $sb.Append(" ")
145+
}
146+
147+
if (-not ($parameter.ParameterType -eq [System.Management.Automation.SwitchParameter])) {
148+
$null = $sb.Append("<c='$colParmValue'>PARAMVALUE </c>")
149+
}
150+
}
151+
152+
$null = $sb.AppendLine("")
153+
Write-PSFHostColor -String "$($sb.ToString())"
154+
}
155+
156+
$null = $sbHelp.AppendLine("")
157+
$null = $sbHelp.AppendLine("<c='$colorParmsNotFound'>$colorParmsNotFound</c> = Parameter not found")
158+
$null = $sbHelp.AppendLine("<c='$colorCommandName'>$colorCommandName</c> = Command Name")
159+
$null = $sbHelp.AppendLine("<c='$colorMandatoryParam'>$colorMandatoryParam</c> = Mandatory Parameter")
160+
$null = $sbHelp.AppendLine("<c='$colorNonMandatoryParam'>$colorNonMandatoryParam</c> = Optional Parameter")
161+
$null = $sbHelp.AppendLine("<c='$colParmValue'>$colParmValue</c> = Parameter value")
162+
$null = $sbHelp.AppendLine("<c='$colorFoundAsterisk'>*</c> = Parameter was filled")
163+
$null = $sbHelp.AppendLine("<c='$colorNotFoundAsterisk'>*</c> = Mandatory missing")
164+
}
165+
166+
"ShowParameters" {
167+
foreach ($parmSet in (Get-Command $commandName).ParameterSets) {
168+
# (Get-Command $commandName).ParameterSets | ForEach-Object {
169+
$sb = New-Object System.Text.StringBuilder
170+
$null = $sb.AppendLine("ParameterSet Name: <c='em'>$($parmSet.Name)</c> - Parameter List")
171+
$null = $sb.Append("<c='$colorCommandName'>$commandName </c>")
172+
173+
$parmSetParameters = $parmSet.Parameters | Where-Object name -NotIn $commonParameters
174+
175+
foreach ($parameter in $parmSetParameters) {
176+
# $parmSetParameters | ForEach-Object {
177+
$color = "$colorNonMandatoryParam"
178+
179+
if ($parameter.IsMandatory -eq $true) { $color = "$colorMandatoryParam" }
180+
181+
$null = $sb.Append("<c='$color'>-$($parameter.Name) </c>")
182+
183+
if (-not ($parameter.ParameterType -eq [System.Management.Automation.SwitchParameter])) {
184+
$null = $sb.Append("<c='$colParmValue'>PARAMVALUE </c>")
185+
}
186+
}
187+
188+
$null = $sb.AppendLine("")
189+
Write-PSFHostColor -String "$($sb.ToString())"
190+
}
191+
192+
$null = $sbHelp.AppendLine("")
193+
$null = $sbHelp.AppendLine("<c='$colorCommandName'>$colorCommandName</c> = Command Name")
194+
$null = $sbHelp.AppendLine("<c='$colorMandatoryParam'>$colorMandatoryParam</c> = Mandatory Parameter")
195+
$null = $sbHelp.AppendLine("<c='$colorNonMandatoryParam'>$colorNonMandatoryParam</c> = Optional Parameter")
196+
$null = $sbHelp.AppendLine("<c='$colParmValue'>$colParmValue</c> = Parameter value")
197+
}
198+
Default {}
199+
}
200+
201+
if ($sbParmsNotFound.ToString().Trim().Length -gt 0) {
202+
Write-PSFHostColor -String "$($sbParmsNotFound.ToString())"
203+
}
204+
205+
if ($Legend) {
206+
Write-PSFHostColor -String "$($sbHelp.ToString())"
207+
}
208+
}
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
11
Set-PSFConfig -Module PSModuleDevelopment -Name "Module.Path" -Value "" -Initialize -Validation "string" -Handler { } -Description "The path to the module currently under development. Used as default path by commnds that work within a module directory."
2-
Set-PSFConfig -Module PSModuleDevelopment -Name 'Package.Path' -Value "$env:TEMP" -Initialize -Validation "string" -Description "The default output path when exporting a module into a nuget package."
2+
Set-PSFConfig -Module PSModuleDevelopment -Name 'Package.Path' -Value "$env:TEMP" -Initialize -Validation "string" -Description "The default output path when exporting a module into a nuget package."
3+
4+
Set-PSFConfig -Module PSModuleDevelopment -Name "ShowSyntax.ParmsNotFound" -Value "Red" -Initialize -Validation "string" -Handler { } -Description "The color to be used for the parameters that could not be found."
5+
Set-PSFConfig -Module PSModuleDevelopment -Name "ShowSyntax.CommandName" -Value "Green" -Initialize -Validation "string" -Handler { } -Description "The color to be used for the command name extracted from the command text."
6+
Set-PSFConfig -Module PSModuleDevelopment -Name "ShowSyntax.MandatoryParam" -Value "Yellow" -Initialize -Validation "string" -Handler { } -Description "The color to be used for the mandatory parameters from the commands parameter sets."
7+
Set-PSFConfig -Module PSModuleDevelopment -Name "ShowSyntax.NonMandatoryParam" -Value "DarkGray" -Initialize -Validation "string" -Handler { } -Description "The color to be used for the non mandatory parameters from the commands parameter sets."
8+
Set-PSFConfig -Module PSModuleDevelopment -Name "ShowSyntax.FoundAsterisk" -Value "Green" -Initialize -Validation "string" -Handler { } -Description "The color to be used for the asterisk that indicates a parameter has been filled / supplied."
9+
Set-PSFConfig -Module PSModuleDevelopment -Name "ShowSyntax.NotFoundAsterisk" -Value "Magenta" -Initialize -Validation "string" -Handler { } -Description "The color to be used for the asterisk that indicates a mandatory parameter has not been filled / supplied."
10+
Set-PSFConfig -Module PSModuleDevelopment -Name "ShowSyntax.ParmValue" -Value "DarkCyan" -Initialize -Validation "string" -Handler { } -Description "The color to be used for the parameter value."

0 commit comments

Comments
 (0)