1- function Show-PSMDSyntax {
2- <#
1+ function Show-PSMDSyntax
2+ {
3+ <#
34 . SYNOPSIS
45 Validate or show parameter set details with colored output
56
4647 Original github project: https://github.com/d365collaborative/d365fo.tools
4748
4849#>
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- }
50+ [CmdletBinding ()]
51+ param (
52+ [Parameter (Mandatory = $true , Position = 1 )]
53+ [string ]
54+ $CommandText ,
55+
56+ [Parameter (Position = 2 )]
57+ [ValidateSet (' Validate' , ' ShowParameters' )]
58+ [string ]
59+ $Mode = ' Validate' ,
60+
61+ [switch ]
62+ $Legend
63+ )
64+
65+ $commonParameters = ' Verbose' , ' Debug' , ' ErrorAction' , ' WarningAction' , ' InformationAction' , ' ErrorVariable' , ' WarningVariable' , ' InformationVariable' , ' OutVariable' , ' OutBuffer' , ' PipelineVariable' , ' Confirm' , ' WhatIf'
66+
67+ $colorParmsNotFound = Get-PSFConfigValue - FullName " PSModuleDevelopment.ShowSyntax.ParmsNotFound"
68+ $colorCommandName = Get-PSFConfigValue - FullName " PSModuleDevelopment.ShowSyntax.CommandName"
69+ $colorMandatoryParam = Get-PSFConfigValue - FullName " PSModuleDevelopment.ShowSyntax.MandatoryParam"
70+ $colorNonMandatoryParam = Get-PSFConfigValue - FullName " PSModuleDevelopment.ShowSyntax.NonMandatoryParam"
71+ $colorFoundAsterisk = Get-PSFConfigValue - FullName " PSModuleDevelopment.ShowSyntax.FoundAsterisk"
72+ $colorNotFoundAsterisk = Get-PSFConfigValue - FullName " PSModuleDevelopment.ShowSyntax.NotFoundAsterisk"
73+ $colParmValue = Get-PSFConfigValue - FullName " PSModuleDevelopment.ShowSyntax.ParmValue"
74+
75+ # Match to find the command name: Non-Whitespace until the first whitespace
76+ $commandMatch = ($CommandText | Select-String ' \S+\s*' ).Matches
77+
78+ if ($null -eq $commandMatch )
79+ {
80+ Write-PSFMessage - Level Host - Message " The function was unable to extract a valid command name from the supplied command text. Please try again."
81+ Stop-PSFFunction - Message " Stopping because of missing command name."
82+ return
83+ }
84+
85+ $commandName = $commandMatch.Value.Trim ()
86+
87+ $res = Get-Command $commandName - ErrorAction Ignore
88+
89+ if ($null -eq $res )
90+ {
91+ 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."
92+ Stop-PSFFunction - Message " Stopping because command name didn't return any help."
93+ return
94+ }
95+
96+ $sbHelp = New-Object System.Text.StringBuilder
97+ $sbParmsNotFound = New-Object System.Text.StringBuilder
98+
99+ switch ($Mode )
100+ {
101+ " Validate" {
102+ # Match to find the parameters: Whitespace Dash Non-Whitespace
103+ $inputParameterMatch = ($CommandText | Select-String ' \s{1}[-]\S+' - AllMatches).Matches
104+
105+ if (-not ($null -eq $inputParameterMatch ))
106+ {
107+ $inputParameterNames = $inputParameterMatch.Value.Trim (" -" , " " )
108+ Write-PSFMessage - Level Verbose - Message " All input parameters - $ ( $inputParameterNames -join " ," ) " - Target ($inputParameterNames -join " ," )
109+ }
110+ else
111+ {
112+ Write-PSFMessage - Level Host - Message " The function was unable to extract any parameters from the supplied command text. Please try again."
113+ Stop-PSFFunction - Message " Stopping because of missing input parameters."
114+ return
115+ }
116+
117+ $availableParameterNames = (Get-Command $commandName ).Parameters.keys | Where-Object { $commonParameters -NotContains $_ }
118+ Write-PSFMessage - Level Verbose - Message " Available parameters - $ ( $availableParameterNames -join " ," ) " - Target ($availableParameterNames -join " ," )
119+
120+ $inputParameterNotFound = $inputParameterNames | Where-Object { $availableParameterNames -NotContains $_ }
121+
122+ if ($inputParameterNotFound.Length -gt 0 )
123+ {
124+ $null = $sbParmsNotFound.AppendLine (" Parameters that <c='em'>don't exists</c>" )
125+ $inputParameterNotFound | ForEach-Object {
126+ $null = $sbParmsNotFound.AppendLine (" <c='$colorParmsNotFound '>$ ( $_ ) </c>" )
127+ }
128+ }
129+
130+ foreach ($parmSet in (Get-Command $commandName ).ParameterSets)
131+ {
132+ $sb = New-Object System.Text.StringBuilder
133+ $null = $sb.AppendLine (" ParameterSet Name: <c='em'>$ ( $parmSet.Name ) </c> - Validated List" )
134+ $null = $sb.Append (" <c='$colorCommandName '>$commandName </c>" )
135+
136+ $parmSetParameters = $parmSet.Parameters | Where-Object name -NotIn $commonParameters
137+
138+ foreach ($parameter in $parmSetParameters )
139+ {
140+ $parmFoundInCommandText = $parameter.Name -In $inputParameterNames
141+
142+ $color = " $colorNonMandatoryParam "
143+
144+ if ($parameter.IsMandatory -eq $true ) { $color = " $colorMandatoryParam " }
145+
146+ $null = $sb.Append (" <c='$color '>-$ ( $parameter.Name ) </c>" )
147+
148+ if ($parmFoundInCommandText )
149+ {
150+ $null = $sb.Append (" <c='$colorFoundAsterisk '>* </c>" )
151+ }
152+ elseif ($parameter.IsMandatory -eq $true )
153+ {
154+ $null = $sb.Append (" <c='$colorNotFoundAsterisk '>* </c>" )
155+ }
156+ else
157+ {
158+ $null = $sb.Append (" " )
159+ }
160+
161+ if (-not ($parameter.ParameterType -eq [System.Management.Automation.SwitchParameter ]))
162+ {
163+ $null = $sb.Append (" <c='$colParmValue '>PARAMVALUE </c>" )
164+ }
165+ }
166+
167+ $null = $sb.AppendLine (" " )
168+ Write-PSFHostColor - String " $ ( $sb.ToString ()) "
169+ }
170+
171+ $null = $sbHelp.AppendLine (" " )
172+ $null = $sbHelp.AppendLine (" <c='$colorParmsNotFound '>$colorParmsNotFound </c> = Parameter not found" )
173+ $null = $sbHelp.AppendLine (" <c='$colorCommandName '>$colorCommandName </c> = Command Name" )
174+ $null = $sbHelp.AppendLine (" <c='$colorMandatoryParam '>$colorMandatoryParam </c> = Mandatory Parameter" )
175+ $null = $sbHelp.AppendLine (" <c='$colorNonMandatoryParam '>$colorNonMandatoryParam </c> = Optional Parameter" )
176+ $null = $sbHelp.AppendLine (" <c='$colParmValue '>$colParmValue </c> = Parameter value" )
177+ $null = $sbHelp.AppendLine (" <c='$colorFoundAsterisk '>*</c> = Parameter was filled" )
178+ $null = $sbHelp.AppendLine (" <c='$colorNotFoundAsterisk '>*</c> = Mandatory missing" )
179+ }
180+
181+ " ShowParameters" {
182+ foreach ($parmSet in (Get-Command $commandName ).ParameterSets)
183+ {
184+ # (Get-Command $commandName).ParameterSets | ForEach-Object {
185+ $sb = New-Object System.Text.StringBuilder
186+ $null = $sb.AppendLine (" ParameterSet Name: <c='em'>$ ( $parmSet.Name ) </c> - Parameter List" )
187+ $null = $sb.Append (" <c='$colorCommandName '>$commandName </c>" )
188+
189+ $parmSetParameters = $parmSet.Parameters | Where-Object name -NotIn $commonParameters
190+
191+ foreach ($parameter in $parmSetParameters )
192+ {
193+ # $parmSetParameters | ForEach-Object {
194+ $color = " $colorNonMandatoryParam "
195+
196+ if ($parameter.IsMandatory -eq $true ) { $color = " $colorMandatoryParam " }
197+
198+ $null = $sb.Append (" <c='$color '>-$ ( $parameter.Name ) </c>" )
199+
200+ if (-not ($parameter.ParameterType -eq [System.Management.Automation.SwitchParameter ]))
201+ {
202+ $null = $sb.Append (" <c='$colParmValue '>PARAMVALUE </c>" )
203+ }
204+ }
205+
206+ $null = $sb.AppendLine (" " )
207+ Write-PSFHostColor - String " $ ( $sb.ToString ()) "
208+ }
209+
210+ $null = $sbHelp.AppendLine (" " )
211+ $null = $sbHelp.AppendLine (" <c='$colorCommandName '>$colorCommandName </c> = Command Name" )
212+ $null = $sbHelp.AppendLine (" <c='$colorMandatoryParam '>$colorMandatoryParam </c> = Mandatory Parameter" )
213+ $null = $sbHelp.AppendLine (" <c='$colorNonMandatoryParam '>$colorNonMandatoryParam </c> = Optional Parameter" )
214+ $null = $sbHelp.AppendLine (" <c='$colParmValue '>$colParmValue </c> = Parameter value" )
215+ }
216+ Default { }
217+ }
218+
219+ if ($sbParmsNotFound.ToString ().Trim().Length -gt 0 )
220+ {
221+ Write-PSFHostColor - String " $ ( $sbParmsNotFound.ToString ()) "
222+ }
223+
224+ if ($Legend )
225+ {
226+ Write-PSFHostColor - String " $ ( $sbHelp.ToString ()) "
227+ }
208228}
0 commit comments