Skip to content

Commit f89d27c

Browse files
committed
Update PSModuleDevelopment/functions/utility/Show-PSMDSyntax.ps1
1 parent a30f51d commit f89d27c

File tree

1 file changed

+135
-145
lines changed

1 file changed

+135
-145
lines changed

PSModuleDevelopment/functions/utility/Show-PSMDSyntax.ps1

Lines changed: 135 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -68,148 +68,138 @@
6868
$colorNotFoundAsterisk = Get-PSFConfigValue -FullName "PSModuleDevelopment.ShowSyntax.NotFoundAsterisk"
6969
$colParmValue = Get-PSFConfigValue -FullName "PSModuleDevelopment.ShowSyntax.ParmValue"
7070

71-
#Match to find the command name: Non-Whitespace until the first whitespace
72-
$commandMatch = ($CommandText | Select-String '\S+\s*').Matches
73-
74-
if (-not ($null -eq $commandMatch)) {
75-
#$commandName = ($CommandText | Select-String '\S+\s{1}').Matches.Value.Trim()
76-
$commandName = $commandMatch.Value.Trim()
77-
78-
$res = Get-Command $commandName -ErrorAction Ignore
79-
80-
if (-not ($null -eq $res)) {
81-
82-
$null = $sbHelp = New-Object System.Text.StringBuilder
83-
$null = $sbParmsNotFound = New-Object System.Text.StringBuilder
84-
85-
86-
switch ($Mode) {
87-
"Validate" {
88-
#Match to find the parameters: Whitespace Dash Non-Whitespace
89-
$inputParameterMatch = ($CommandText | Select-String '\s{1}[-]\S+' -AllMatches).Matches
90-
91-
if (-not ($null -eq $inputParameterMatch)) {
92-
$inputParameterNames = $inputParameterMatch.Value.Trim("-", " ")
93-
}
94-
else {
95-
Write-PSFMessage -Level Host -Message "The function was unable to extract any parameters from the supplied command text. Please try again."
96-
Stop-PSFFunction -Message "Stopping because of missing input parameters."
97-
return
98-
}
99-
100-
$availableParameterNames = (Get-Command $commandName).Parameters.keys | Where-Object {$commonParameters -NotContains $_}
101-
$inputParameterNotFound = $inputParameterNames | Where-Object {$availableParameterNames -NotContains $_}
102-
103-
$null = $sbParmsNotFound.AppendLine("Parameters that <c='em'>don't exists</c>")
104-
#Show all the parameters that could be match here: $inputParameterNotFound
105-
$inputParameterNotFound | ForEach-Object {
106-
$null = $sbParmsNotFound.AppendLine("<c='$colorParmsNotFound'>$($_)</c>")
107-
}
108-
109-
110-
(Get-Command $commandName).ParameterSets | ForEach-Object {
111-
$null = $sb = New-Object System.Text.StringBuilder
112-
113-
$null = $sb.AppendLine("ParameterSet Name: <c='em'>$($_.Name)</c> - Validated List")
114-
115-
116-
$null = $sb.Append("<c='$colorCommandName'>$commandName </c>")
117-
$parmSetParameters = $_.Parameters | Where-Object name -NotIn $commonParameters
118-
119-
$parmSetParameters | ForEach-Object {
120-
$parmFoundInCommandText = $_.Name -In $inputParameterNames
121-
122-
$color = "$colorNonMandatoryParam"
123-
124-
if ($_.IsMandatory -eq $true) { $color = "$colorMandatoryParam" }
125-
126-
$null = $sb.Append("<c='$color'>-$($_.Name)</c>")
127-
128-
129-
if ($parmFoundInCommandText) {
130-
$color = "$colorFoundAsterisk"
131-
$null = $sb.Append("<c='$color'>* </c>")
132-
}
133-
elseif ($_.IsMandatory -eq $true) {
134-
$color = "$colorNotFoundAsterisk"
135-
$null = $sb.Append("<c='$color'>* </c>")
136-
}
137-
else {
138-
$null = $sb.Append(" ")
139-
140-
}
141-
142-
if (-not ($_.ParameterType -eq [System.Management.Automation.SwitchParameter])) {
143-
$null = $sb.Append("<c='$colParmValue'>PARAMVALUE </c>")
144-
145-
}
146-
}
147-
148-
$null = $sb.AppendLine("")
149-
Write-PSFMessage -Level Host -Message "$($sb.ToString())"
150-
}
151-
152-
$null = $sbHelp.AppendLine("")
153-
$null = $sbHelp.AppendLine("<c='$colorParmsNotFound'>$colorParmsNotFound</c> = Parameter not found")
154-
$null = $sbHelp.AppendLine("<c='$colorCommandName'>$colorCommandName</c> = Command Name")
155-
$null = $sbHelp.AppendLine("<c='$colorMandatoryParam'>$colorMandatoryParam</c> = Mandatory Parameter")
156-
$null = $sbHelp.AppendLine("<c='$colorNonMandatoryParam'>$colorNonMandatoryParam</c> = Optional Parameter")
157-
$null = $sbHelp.AppendLine("<c='$colParmValue'>$colParmValue</c> = Parameter value")
158-
$null = $sbHelp.AppendLine("<c='$colorFoundAsterisk'>*</c> = Parameter was filled")
159-
$null = $sbHelp.AppendLine("<c='$colorNotFoundAsterisk'>*</c> = Mandatory missing")
160-
}
161-
162-
"ShowParameters" {
163-
(Get-Command $commandName).ParameterSets | ForEach-Object {
164-
$null = $sb = New-Object System.Text.StringBuilder
165-
166-
$null = $sb.AppendLine("ParameterSet Name: <c='em'>$($_.Name)</c> - Parameter List")
167-
168-
169-
$null = $sb.Append("<c='$colorCommandName'>$commandName </c>")
170-
$parmSetParameters = $_.Parameters | Where-Object name -NotIn $commonParameters
171-
172-
$parmSetParameters | ForEach-Object {
173-
$color = "$colorNonMandatoryParam"
174-
175-
if ($_.IsMandatory -eq $true) { $color = "$colorMandatoryParam" }
176-
177-
$null = $sb.Append("<c='$color'>-$($_.Name) </c>")
178-
179-
if (-not ($_.ParameterType -eq [System.Management.Automation.SwitchParameter])) {
180-
$null = $sb.Append("<c='$colParmValue'>PARAMVALUE </c>")
181-
182-
}
183-
}
184-
185-
$null = $sb.AppendLine("")
186-
Write-PSFMessage -Level Host -Message "$($sb.ToString())"
187-
}
188-
189-
$null = $sbHelp.AppendLine("")
190-
$null = $sbHelp.AppendLine("<c='$colorCommandName'>$colorCommandName</c> = Command Name")
191-
$null = $sbHelp.AppendLine("<c='$colorMandatoryParam'>$colorMandatoryParam</c> = Mandatory Parameter")
192-
$null = $sbHelp.AppendLine("<c='$colorNonMandatoryParam'>$colorNonMandatoryParam</c> = Optional Parameter")
193-
$null = $sbHelp.AppendLine("<c='$colParmValue'>$colParmValue</c> = Parameter value")
194-
}
195-
Default {}
196-
}
197-
198-
Write-PSFMessage -Level Host -Message "$($sbParmsNotFound.ToString())"
199-
200-
if ($IncludeHelp) {
201-
Write-PSFMessage -Level Host -Message "$($sbHelp.ToString())"
202-
}
203-
}
204-
else {
205-
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."
206-
Stop-PSFFunction -Message "Stopping because command name didn't return any help."
207-
return
208-
}
209-
}
210-
else {
211-
Write-PSFMessage -Level Host -Message "The function was unable to extract a valid command name from the supplied command text. Please try again."
212-
Stop-PSFFunction -Message "Stopping because of missing command name."
213-
return
214-
}
215-
}
71+
#Match to find the command name: Non-Whitespace until the first whitespace
72+
$commandMatch = ($CommandText | Select-String '\S+\s*').Matches
73+
74+
if (-not ($null -eq $commandMatch)) {
75+
$commandName = $commandMatch.Value.Trim()
76+
77+
$res = Get-Command $commandName -ErrorAction Ignore
78+
79+
if (-not ($null -eq $res)) {
80+
81+
$null = $sbHelp = New-Object System.Text.StringBuilder
82+
$null = $sbParmsNotFound = New-Object System.Text.StringBuilder
83+
84+
switch ($Mode) {
85+
"Validate" {
86+
#Match to find the parameters: Whitespace Dash Non-Whitespace
87+
$inputParameterMatch = ($CommandText | Select-String '\s{1}[-]\S+' -AllMatches).Matches
88+
89+
if (-not ($null -eq $inputParameterMatch)) {
90+
$inputParameterNames = $inputParameterMatch.Value.Trim("-", " ")
91+
}
92+
else {
93+
Write-PSFMessage -Level Host -Message "The function was unable to extract any parameters from the supplied command text. Please try again."
94+
Stop-PSFFunction -Message "Stopping because of missing input parameters."
95+
return
96+
}
97+
98+
$availableParameterNames = (Get-Command $commandName).Parameters.keys | Where-Object {$commonParameters -NotContains $_}
99+
$inputParameterNotFound = $inputParameterNames | Where-Object {$availableParameterNames -NotContains $_}
100+
101+
$null = $sbParmsNotFound.AppendLine("Parameters that <c='em'>don't exists</c>")
102+
$inputParameterNotFound | ForEach-Object {
103+
$null = $sbParmsNotFound.AppendLine("<c='$colorParmsNotFound'>$($_)</c>")
104+
}
105+
106+
foreach ($parmSet in (Get-Command $commandName).ParameterSets) {
107+
$null = $sb = New-Object System.Text.StringBuilder
108+
$null = $sb.AppendLine("ParameterSet Name: <c='em'>$($parmSet.Name)</c> - Validated List")
109+
$null = $sb.Append("<c='$colorCommandName'>$commandName </c>")
110+
111+
$parmSetParameters = $parmSet.Parameters | Where-Object name -NotIn $commonParameters
112+
113+
foreach ($parameter in $parmSetParameters) {
114+
$parmFoundInCommandText = $parameter.Name -In $inputParameterNames
115+
116+
$color = "$colorNonMandatoryParam"
117+
118+
if ($parameter.IsMandatory -eq $true) { $color = "$colorMandatoryParam" }
119+
120+
$null = $sb.Append("<c='$color'>-$($parameter.Name)</c>")
121+
122+
if ($parmFoundInCommandText) {
123+
$color = "$colorFoundAsterisk"
124+
$null = $sb.Append("<c='$color'>* </c>")
125+
}
126+
elseif ($parameter.IsMandatory -eq $true) {
127+
$color = "$colorNotFoundAsterisk"
128+
$null = $sb.Append("<c='$color'>* </c>")
129+
}
130+
else {
131+
$null = $sb.Append(" ")
132+
}
133+
134+
if (-not ($parameter.ParameterType -eq [System.Management.Automation.SwitchParameter])) {
135+
$null = $sb.Append("<c='$colParmValue'>PARAMVALUE </c>")
136+
}
137+
}
138+
139+
$null = $sb.AppendLine("")
140+
Write-PSFMessage -Level Host -Message "$($sb.ToString())"
141+
}
142+
143+
$null = $sbHelp.AppendLine("")
144+
$null = $sbHelp.AppendLine("<c='$colorParmsNotFound'>$colorParmsNotFound</c> = Parameter not found")
145+
$null = $sbHelp.AppendLine("<c='$colorCommandName'>$colorCommandName</c> = Command Name")
146+
$null = $sbHelp.AppendLine("<c='$colorMandatoryParam'>$colorMandatoryParam</c> = Mandatory Parameter")
147+
$null = $sbHelp.AppendLine("<c='$colorNonMandatoryParam'>$colorNonMandatoryParam</c> = Optional Parameter")
148+
$null = $sbHelp.AppendLine("<c='$colParmValue'>$colParmValue</c> = Parameter value")
149+
$null = $sbHelp.AppendLine("<c='$colorFoundAsterisk'>*</c> = Parameter was filled")
150+
$null = $sbHelp.AppendLine("<c='$colorNotFoundAsterisk'>*</c> = Mandatory missing")
151+
}
152+
153+
"ShowParameters" {
154+
foreach ($parmSet in (Get-Command $commandName).ParameterSets) {
155+
# (Get-Command $commandName).ParameterSets | ForEach-Object {
156+
$null = $sb = New-Object System.Text.StringBuilder
157+
$null = $sb.AppendLine("ParameterSet Name: <c='em'>$($parmSet.Name)</c> - Parameter List")
158+
$null = $sb.Append("<c='$colorCommandName'>$commandName </c>")
159+
160+
$parmSetParameters = $parmSet.Parameters | Where-Object name -NotIn $commonParameters
161+
162+
foreach ($parameter in $parmSetParameters) {
163+
# $parmSetParameters | ForEach-Object {
164+
$color = "$colorNonMandatoryParam"
165+
166+
if ($parameter.IsMandatory -eq $true) { $color = "$colorMandatoryParam" }
167+
168+
$null = $sb.Append("<c='$color'>-$($parameter.Name) </c>")
169+
170+
if (-not ($parameter.ParameterType -eq [System.Management.Automation.SwitchParameter])) {
171+
$null = $sb.Append("<c='$colParmValue'>PARAMVALUE </c>")
172+
}
173+
}
174+
175+
$null = $sb.AppendLine("")
176+
Write-PSFMessage -Level Host -Message "$($sb.ToString())"
177+
}
178+
179+
$null = $sbHelp.AppendLine("")
180+
$null = $sbHelp.AppendLine("<c='$colorCommandName'>$colorCommandName</c> = Command Name")
181+
$null = $sbHelp.AppendLine("<c='$colorMandatoryParam'>$colorMandatoryParam</c> = Mandatory Parameter")
182+
$null = $sbHelp.AppendLine("<c='$colorNonMandatoryParam'>$colorNonMandatoryParam</c> = Optional Parameter")
183+
$null = $sbHelp.AppendLine("<c='$colParmValue'>$colParmValue</c> = Parameter value")
184+
}
185+
Default {}
186+
}
187+
188+
Write-PSFMessage -Level Host -Message "$($sbParmsNotFound.ToString())"
189+
190+
if ($IncludeHelp) {
191+
Write-PSFMessage -Level Host -Message "$($sbHelp.ToString())"
192+
}
193+
}
194+
else {
195+
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."
196+
Stop-PSFFunction -Message "Stopping because command name didn't return any help."
197+
return
198+
}
199+
}
200+
else {
201+
Write-PSFMessage -Level Host -Message "The function was unable to extract a valid command name from the supplied command text. Please try again."
202+
Stop-PSFFunction -Message "Stopping because of missing command name."
203+
return
204+
}
205+
}

0 commit comments

Comments
 (0)