Skip to content

Commit 483ae03

Browse files
committed
import draft
1 parent 8b43816 commit 483ae03

File tree

2 files changed

+219
-1
lines changed

2 files changed

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