Skip to content

Commit 04b0a35

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

File tree

1 file changed

+111
-110
lines changed

1 file changed

+111
-110
lines changed
Lines changed: 111 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
function Show-PSMDSyntax
2-
{
3-
<#
1+
function Show-PSMDSyntax {
2+
<#
43
.SYNOPSIS
54
Validate or show parameter set details with colored output
65
@@ -68,138 +67,140 @@
6867
$colorNotFoundAsterisk = Get-PSFConfigValue -FullName "PSModuleDevelopment.ShowSyntax.NotFoundAsterisk"
6968
$colParmValue = Get-PSFConfigValue -FullName "PSModuleDevelopment.ShowSyntax.ParmValue"
7069

71-
#Match to find the command name: Non-Whitespace until the first whitespace
72-
$commandMatch = ($CommandText | Select-String '\S+\s*').Matches
70+
#Match to find the command name: Non-Whitespace until the first whitespace
71+
$commandMatch = ($CommandText | Select-String '\S+\s*').Matches
7372

74-
if (-not ($null -eq $commandMatch)) {
75-
$commandName = $commandMatch.Value.Trim()
73+
if (-not ($null -eq $commandMatch)) {
74+
$commandName = $commandMatch.Value.Trim()
7675

77-
$res = Get-Command $commandName -ErrorAction Ignore
76+
$res = Get-Command $commandName -ErrorAction Ignore
7877

79-
if (-not ($null -eq $res)) {
78+
if (-not ($null -eq $res)) {
8079

81-
$null = $sbHelp = New-Object System.Text.StringBuilder
82-
$null = $sbParmsNotFound = New-Object System.Text.StringBuilder
80+
$null = $sbHelp = New-Object System.Text.StringBuilder
81+
$null = $sbParmsNotFound = New-Object System.Text.StringBuilder
8382

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
83+
switch ($Mode) {
84+
"Validate" {
85+
#Match to find the parameters: Whitespace Dash Non-Whitespace
86+
$inputParameterMatch = ($CommandText | Select-String '\s{1}[-]\S+' -AllMatches).Matches
8887

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-
}
88+
if (-not ($null -eq $inputParameterMatch)) {
89+
$inputParameterNames = $inputParameterMatch.Value.Trim("-", " ")
90+
}
91+
else {
92+
Write-PSFMessage -Level Host -Message "The function was unable to extract any parameters from the supplied command text. Please try again."
93+
Stop-PSFFunction -Message "Stopping because of missing input parameters."
94+
return
95+
}
9796

98-
$availableParameterNames = (Get-Command $commandName).Parameters.keys | Where-Object {$commonParameters -NotContains $_}
99-
$inputParameterNotFound = $inputParameterNames | Where-Object {$availableParameterNames -NotContains $_}
97+
$availableParameterNames = (Get-Command $commandName).Parameters.keys | Where-Object {$commonParameters -NotContains $_}
98+
$inputParameterNotFound = $inputParameterNames | Where-Object {$availableParameterNames -NotContains $_}
10099

101-
$null = $sbParmsNotFound.AppendLine("Parameters that <c='em'>don't exists</c>")
102-
$inputParameterNotFound | ForEach-Object {
103-
$null = $sbParmsNotFound.AppendLine("<c='$colorParmsNotFound'>$($_)</c>")
104-
}
100+
$null = $sbParmsNotFound.AppendLine("Parameters that <c='em'>don't exists</c>")
101+
$inputParameterNotFound | ForEach-Object {
102+
$null = $sbParmsNotFound.AppendLine("<c='$colorParmsNotFound'>$($_)</c>")
103+
}
105104

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>")
105+
foreach ($parmSet in (Get-Command $commandName).ParameterSets) {
106+
$null = $sb = New-Object System.Text.StringBuilder
107+
$null = $sb.AppendLine("ParameterSet Name: <c='em'>$($parmSet.Name)</c> - Validated List")
108+
$null = $sb.Append("<c='$colorCommandName'>$commandName </c>")
110109

111-
$parmSetParameters = $parmSet.Parameters | Where-Object name -NotIn $commonParameters
110+
$parmSetParameters = $parmSet.Parameters | Where-Object name -NotIn $commonParameters
112111

113-
foreach ($parameter in $parmSetParameters) {
114-
$parmFoundInCommandText = $parameter.Name -In $inputParameterNames
112+
foreach ($parameter in $parmSetParameters) {
113+
$parmFoundInCommandText = $parameter.Name -In $inputParameterNames
115114

116-
$color = "$colorNonMandatoryParam"
115+
$color = "$colorNonMandatoryParam"
117116

118-
if ($parameter.IsMandatory -eq $true) { $color = "$colorMandatoryParam" }
117+
if ($parameter.IsMandatory -eq $true) { $color = "$colorMandatoryParam" }
119118

120-
$null = $sb.Append("<c='$color'>-$($parameter.Name)</c>")
119+
$null = $sb.Append("<c='$color'>-$($parameter.Name)</c>")
121120

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-
}
121+
if ($parmFoundInCommandText) {
122+
$color = "$colorFoundAsterisk"
123+
$null = $sb.Append("<c='$color'>* </c>")
124+
}
125+
elseif ($parameter.IsMandatory -eq $true) {
126+
$color = "$colorNotFoundAsterisk"
127+
$null = $sb.Append("<c='$color'>* </c>")
128+
}
129+
else {
130+
$null = $sb.Append(" ")
131+
}
133132

134-
if (-not ($parameter.ParameterType -eq [System.Management.Automation.SwitchParameter])) {
135-
$null = $sb.Append("<c='$colParmValue'>PARAMVALUE </c>")
136-
}
137-
}
133+
if (-not ($parameter.ParameterType -eq [System.Management.Automation.SwitchParameter])) {
134+
$null = $sb.Append("<c='$colParmValue'>PARAMVALUE </c>")
135+
}
136+
}
138137

139-
$null = $sb.AppendLine("")
140-
Write-PSFMessage -Level Host -Message "$($sb.ToString())"
141-
}
138+
$null = $sb.AppendLine("")
139+
Write-PSFMessage -Level Host -Message "$($sb.ToString())"
140+
}
142141

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-
}
142+
$null = $sbHelp.AppendLine("")
143+
$null = $sbHelp.AppendLine("<c='$colorParmsNotFound'>$colorParmsNotFound</c> = Parameter not found")
144+
$null = $sbHelp.AppendLine("<c='$colorCommandName'>$colorCommandName</c> = Command Name")
145+
$null = $sbHelp.AppendLine("<c='$colorMandatoryParam'>$colorMandatoryParam</c> = Mandatory Parameter")
146+
$null = $sbHelp.AppendLine("<c='$colorNonMandatoryParam'>$colorNonMandatoryParam</c> = Optional Parameter")
147+
$null = $sbHelp.AppendLine("<c='$colParmValue'>$colParmValue</c> = Parameter value")
148+
$null = $sbHelp.AppendLine("<c='$colorFoundAsterisk'>*</c> = Parameter was filled")
149+
$null = $sbHelp.AppendLine("<c='$colorNotFoundAsterisk'>*</c> = Mandatory missing")
150+
}
152151

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>")
152+
"ShowParameters" {
153+
foreach ($parmSet in (Get-Command $commandName).ParameterSets) {
154+
# (Get-Command $commandName).ParameterSets | ForEach-Object {
155+
$null = $sb = New-Object System.Text.StringBuilder
156+
$null = $sb.AppendLine("ParameterSet Name: <c='em'>$($parmSet.Name)</c> - Parameter List")
157+
$null = $sb.Append("<c='$colorCommandName'>$commandName </c>")
159158

160-
$parmSetParameters = $parmSet.Parameters | Where-Object name -NotIn $commonParameters
159+
$parmSetParameters = $parmSet.Parameters | Where-Object name -NotIn $commonParameters
161160

162-
foreach ($parameter in $parmSetParameters) {
163-
# $parmSetParameters | ForEach-Object {
164-
$color = "$colorNonMandatoryParam"
161+
foreach ($parameter in $parmSetParameters) {
162+
# $parmSetParameters | ForEach-Object {
163+
$color = "$colorNonMandatoryParam"
165164

166-
if ($parameter.IsMandatory -eq $true) { $color = "$colorMandatoryParam" }
165+
if ($parameter.IsMandatory -eq $true) { $color = "$colorMandatoryParam" }
167166

168-
$null = $sb.Append("<c='$color'>-$($parameter.Name) </c>")
167+
$null = $sb.Append("<c='$color'>-$($parameter.Name) </c>")
169168

170-
if (-not ($parameter.ParameterType -eq [System.Management.Automation.SwitchParameter])) {
171-
$null = $sb.Append("<c='$colParmValue'>PARAMVALUE </c>")
172-
}
173-
}
169+
if (-not ($parameter.ParameterType -eq [System.Management.Automation.SwitchParameter])) {
170+
$null = $sb.Append("<c='$colParmValue'>PARAMVALUE </c>")
171+
}
172+
}
174173

175-
$null = $sb.AppendLine("")
176-
Write-PSFMessage -Level Host -Message "$($sb.ToString())"
177-
}
174+
$null = $sb.AppendLine("")
175+
Write-PSFMessage -Level Host -Message "$($sb.ToString())"
176+
}
178177

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-
}
178+
$null = $sbHelp.AppendLine("")
179+
$null = $sbHelp.AppendLine("<c='$colorCommandName'>$colorCommandName</c> = Command Name")
180+
$null = $sbHelp.AppendLine("<c='$colorMandatoryParam'>$colorMandatoryParam</c> = Mandatory Parameter")
181+
$null = $sbHelp.AppendLine("<c='$colorNonMandatoryParam'>$colorNonMandatoryParam</c> = Optional Parameter")
182+
$null = $sbHelp.AppendLine("<c='$colParmValue'>$colParmValue</c> = Parameter value")
183+
}
184+
Default {}
185+
}
187186

188-
Write-PSFMessage -Level Host -Message "$($sbParmsNotFound.ToString())"
187+
if ($sbParmsNotFound.Length -gt 0) {
188+
Write-PSFMessage -Level Host -Message "$($sbParmsNotFound.ToString())"
189+
}
189190

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-
}
191+
if ($IncludeHelp) {
192+
Write-PSFMessage -Level Host -Message "$($sbHelp.ToString())"
193+
}
194+
}
195+
else {
196+
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."
197+
Stop-PSFFunction -Message "Stopping because command name didn't return any help."
198+
return
199+
}
200+
}
201+
else {
202+
Write-PSFMessage -Level Host -Message "The function was unable to extract a valid command name from the supplied command text. Please try again."
203+
Stop-PSFFunction -Message "Stopping because of missing command name."
204+
return
205+
}
206+
}

0 commit comments

Comments
 (0)