Skip to content

Commit df6ef65

Browse files
committed
Fix terminating conditions
1 parent 022e43f commit df6ef65

File tree

1 file changed

+103
-105
lines changed

1 file changed

+103
-105
lines changed

PSModuleDevelopment/functions/utility/Show-PSMDSyntax.ps1

Lines changed: 103 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
Include a legend explaining the color mapping
3131
3232
.EXAMPLE
33-
PS C:\> Show-PSMDSyntax -CommandText "New-Item -Path 'c:\temp\newfile.txt'" -Mode "Validate"
33+
PS C:\> Show-PSMDSyntax -CommandText "New-Item -Path 'c:\temp\newfile.txt'"
3434
3535
This will validate all the parameters that have been passed to the Import-D365Bacpac cmdlet.
3636
All supplied parameters that matches a parameter will be marked with an asterisk.
@@ -54,7 +54,7 @@
5454

5555
[Parameter(Mandatory = $true, Position = 2)]
5656
[ValidateSet('Validate', 'ShowParameters')]
57-
[string] $Mode,
57+
[string] $Mode = 'Validate',
5858

5959
[switch] $Legend
6060
)
@@ -72,137 +72,135 @@
7272
#Match to find the command name: Non-Whitespace until the first whitespace
7373
$commandMatch = ($CommandText | Select-String '\S+\s*').Matches
7474

75-
if (-not ($null -eq $commandMatch)) {
76-
$commandName = $commandMatch.Value.Trim()
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()
7782

78-
$res = Get-Command $commandName -ErrorAction Ignore
83+
$res = Get-Command $commandName -ErrorAction Ignore
7984

80-
if (-not ($null -eq $res)) {
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+
8191

82-
$null = $sbHelp = New-Object System.Text.StringBuilder
83-
$null = $sbParmsNotFound = New-Object System.Text.StringBuilder
92+
$null = $sbHelp = New-Object System.Text.StringBuilder
93+
$null = $sbParmsNotFound = New-Object System.Text.StringBuilder
8494

85-
switch ($Mode) {
86-
"Validate" {
87-
#Match to find the parameters: Whitespace Dash Non-Whitespace
88-
$inputParameterMatch = ($CommandText | Select-String '\s{1}[-]\S+' -AllMatches).Matches
95+
switch ($Mode) {
96+
"Validate" {
97+
#Match to find the parameters: Whitespace Dash Non-Whitespace
98+
$inputParameterMatch = ($CommandText | Select-String '\s{1}[-]\S+' -AllMatches).Matches
8999

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

99-
$availableParameterNames = (Get-Command $commandName).Parameters.keys | Where-Object {$commonParameters -NotContains $_}
100-
$inputParameterNotFound = $inputParameterNames | Where-Object {$availableParameterNames -NotContains $_}
109+
$availableParameterNames = (Get-Command $commandName).Parameters.keys | Where-Object {$commonParameters -NotContains $_}
110+
$inputParameterNotFound = $inputParameterNames | Where-Object {$availableParameterNames -NotContains $_}
101111

102-
$null = $sbParmsNotFound.AppendLine("Parameters that <c='em'>don't exists</c>")
103-
$inputParameterNotFound | ForEach-Object {
104-
$null = $sbParmsNotFound.AppendLine("<c='$colorParmsNotFound'>$($_)</c>")
105-
}
112+
$null = $sbParmsNotFound.AppendLine("Parameters that <c='em'>don't exists</c>")
113+
$inputParameterNotFound | ForEach-Object {
114+
$null = $sbParmsNotFound.AppendLine("<c='$colorParmsNotFound'>$($_)</c>")
115+
}
106116

107-
foreach ($parmSet in (Get-Command $commandName).ParameterSets) {
108-
$null = $sb = New-Object System.Text.StringBuilder
109-
$null = $sb.AppendLine("ParameterSet Name: <c='em'>$($parmSet.Name)</c> - Validated List")
110-
$null = $sb.Append("<c='$colorCommandName'>$commandName </c>")
117+
foreach ($parmSet in (Get-Command $commandName).ParameterSets) {
118+
$null = $sb = New-Object System.Text.StringBuilder
119+
$null = $sb.AppendLine("ParameterSet Name: <c='em'>$($parmSet.Name)</c> - Validated List")
120+
$null = $sb.Append("<c='$colorCommandName'>$commandName </c>")
111121

112-
$parmSetParameters = $parmSet.Parameters | Where-Object name -NotIn $commonParameters
122+
$parmSetParameters = $parmSet.Parameters | Where-Object name -NotIn $commonParameters
113123

114-
foreach ($parameter in $parmSetParameters) {
115-
$parmFoundInCommandText = $parameter.Name -In $inputParameterNames
124+
foreach ($parameter in $parmSetParameters) {
125+
$parmFoundInCommandText = $parameter.Name -In $inputParameterNames
116126

117-
$color = "$colorNonMandatoryParam"
127+
$color = "$colorNonMandatoryParam"
118128

119-
if ($parameter.IsMandatory -eq $true) { $color = "$colorMandatoryParam" }
129+
if ($parameter.IsMandatory -eq $true) { $color = "$colorMandatoryParam" }
120130

121-
$null = $sb.Append("<c='$color'>-$($parameter.Name)</c>")
131+
$null = $sb.Append("<c='$color'>-$($parameter.Name)</c>")
122132

123-
if ($parmFoundInCommandText) {
124-
$color = "$colorFoundAsterisk"
125-
$null = $sb.Append("<c='$color'>* </c>")
126-
}
127-
elseif ($parameter.IsMandatory -eq $true) {
128-
$color = "$colorNotFoundAsterisk"
129-
$null = $sb.Append("<c='$color'>* </c>")
130-
}
131-
else {
132-
$null = $sb.Append(" ")
133-
}
134-
135-
if (-not ($parameter.ParameterType -eq [System.Management.Automation.SwitchParameter])) {
136-
$null = $sb.Append("<c='$colParmValue'>PARAMVALUE </c>")
137-
}
138-
}
133+
if ($parmFoundInCommandText) {
134+
$color = "$colorFoundAsterisk"
135+
$null = $sb.Append("<c='$color'>* </c>")
136+
}
137+
elseif ($parameter.IsMandatory -eq $true) {
138+
$color = "$colorNotFoundAsterisk"
139+
$null = $sb.Append("<c='$color'>* </c>")
140+
}
141+
else {
142+
$null = $sb.Append(" ")
143+
}
139144

140-
$null = $sb.AppendLine("")
141-
Write-PSFMessage -Level Host -Message "$($sb.ToString())"
145+
if (-not ($parameter.ParameterType -eq [System.Management.Automation.SwitchParameter])) {
146+
$null = $sb.Append("<c='$colParmValue'>PARAMVALUE </c>")
142147
}
143-
144-
$null = $sbHelp.AppendLine("")
145-
$null = $sbHelp.AppendLine("<c='$colorParmsNotFound'>$colorParmsNotFound</c> = Parameter not found")
146-
$null = $sbHelp.AppendLine("<c='$colorCommandName'>$colorCommandName</c> = Command Name")
147-
$null = $sbHelp.AppendLine("<c='$colorMandatoryParam'>$colorMandatoryParam</c> = Mandatory Parameter")
148-
$null = $sbHelp.AppendLine("<c='$colorNonMandatoryParam'>$colorNonMandatoryParam</c> = Optional Parameter")
149-
$null = $sbHelp.AppendLine("<c='$colParmValue'>$colParmValue</c> = Parameter value")
150-
$null = $sbHelp.AppendLine("<c='$colorFoundAsterisk'>*</c> = Parameter was filled")
151-
$null = $sbHelp.AppendLine("<c='$colorNotFoundAsterisk'>*</c> = Mandatory missing")
152148
}
149+
150+
$null = $sb.AppendLine("")
151+
Write-PSFMessage -Level Host -Message "$($sb.ToString())"
152+
}
153+
154+
$null = $sbHelp.AppendLine("")
155+
$null = $sbHelp.AppendLine("<c='$colorParmsNotFound'>$colorParmsNotFound</c> = Parameter not found")
156+
$null = $sbHelp.AppendLine("<c='$colorCommandName'>$colorCommandName</c> = Command Name")
157+
$null = $sbHelp.AppendLine("<c='$colorMandatoryParam'>$colorMandatoryParam</c> = Mandatory Parameter")
158+
$null = $sbHelp.AppendLine("<c='$colorNonMandatoryParam'>$colorNonMandatoryParam</c> = Optional Parameter")
159+
$null = $sbHelp.AppendLine("<c='$colParmValue'>$colParmValue</c> = Parameter value")
160+
$null = $sbHelp.AppendLine("<c='$colorFoundAsterisk'>*</c> = Parameter was filled")
161+
$null = $sbHelp.AppendLine("<c='$colorNotFoundAsterisk'>*</c> = Mandatory missing")
162+
}
153163

154-
"ShowParameters" {
155-
foreach ($parmSet in (Get-Command $commandName).ParameterSets) {
156-
# (Get-Command $commandName).ParameterSets | ForEach-Object {
157-
$null = $sb = New-Object System.Text.StringBuilder
158-
$null = $sb.AppendLine("ParameterSet Name: <c='em'>$($parmSet.Name)</c> - Parameter List")
159-
$null = $sb.Append("<c='$colorCommandName'>$commandName </c>")
164+
"ShowParameters" {
165+
foreach ($parmSet in (Get-Command $commandName).ParameterSets) {
166+
# (Get-Command $commandName).ParameterSets | ForEach-Object {
167+
$null = $sb = New-Object System.Text.StringBuilder
168+
$null = $sb.AppendLine("ParameterSet Name: <c='em'>$($parmSet.Name)</c> - Parameter List")
169+
$null = $sb.Append("<c='$colorCommandName'>$commandName </c>")
160170

161-
$parmSetParameters = $parmSet.Parameters | Where-Object name -NotIn $commonParameters
162-
163-
foreach ($parameter in $parmSetParameters) {
164-
# $parmSetParameters | ForEach-Object {
165-
$color = "$colorNonMandatoryParam"
171+
$parmSetParameters = $parmSet.Parameters | Where-Object name -NotIn $commonParameters
166172

167-
if ($parameter.IsMandatory -eq $true) { $color = "$colorMandatoryParam" }
173+
foreach ($parameter in $parmSetParameters) {
174+
# $parmSetParameters | ForEach-Object {
175+
$color = "$colorNonMandatoryParam"
168176

169-
$null = $sb.Append("<c='$color'>-$($parameter.Name) </c>")
177+
if ($parameter.IsMandatory -eq $true) { $color = "$colorMandatoryParam" }
170178

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

176-
$null = $sb.AppendLine("")
177-
Write-PSFMessage -Level Host -Message "$($sb.ToString())"
181+
if (-not ($parameter.ParameterType -eq [System.Management.Automation.SwitchParameter])) {
182+
$null = $sb.Append("<c='$colParmValue'>PARAMVALUE </c>")
178183
}
179-
180-
$null = $sbHelp.AppendLine("")
181-
$null = $sbHelp.AppendLine("<c='$colorCommandName'>$colorCommandName</c> = Command Name")
182-
$null = $sbHelp.AppendLine("<c='$colorMandatoryParam'>$colorMandatoryParam</c> = Mandatory Parameter")
183-
$null = $sbHelp.AppendLine("<c='$colorNonMandatoryParam'>$colorNonMandatoryParam</c> = Optional Parameter")
184-
$null = $sbHelp.AppendLine("<c='$colParmValue'>$colParmValue</c> = Parameter value")
185184
}
186-
Default {}
187-
}
188-
189-
if ($sbParmsNotFound.Length -gt 0) {
190-
Write-PSFMessage -Level Host -Message "$($sbParmsNotFound.ToString())"
185+
186+
$null = $sb.AppendLine("")
187+
Write-PSFMessage -Level Host -Message "$($sb.ToString())"
191188
}
192189

193-
if ($IncludeHelp) {
194-
Write-PSFMessage -Level Host -Message "$($sbHelp.ToString())"
195-
}
196-
}
197-
else {
198-
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."
199-
Stop-PSFFunction -Message "Stopping because command name didn't return any help."
200-
return
190+
$null = $sbHelp.AppendLine("")
191+
$null = $sbHelp.AppendLine("<c='$colorCommandName'>$colorCommandName</c> = Command Name")
192+
$null = $sbHelp.AppendLine("<c='$colorMandatoryParam'>$colorMandatoryParam</c> = Mandatory Parameter")
193+
$null = $sbHelp.AppendLine("<c='$colorNonMandatoryParam'>$colorNonMandatoryParam</c> = Optional Parameter")
194+
$null = $sbHelp.AppendLine("<c='$colParmValue'>$colParmValue</c> = Parameter value")
201195
}
196+
Default {}
202197
}
203-
else {
204-
Write-PSFMessage -Level Host -Message "The function was unable to extract a valid command name from the supplied command text. Please try again."
205-
Stop-PSFFunction -Message "Stopping because of missing command name."
206-
return
198+
199+
if ($sbParmsNotFound.Length -gt 0) {
200+
Write-PSFMessage -Level Host -Message "$($sbParmsNotFound.ToString())"
201+
}
202+
203+
if ($IncludeHelp) {
204+
Write-PSFMessage -Level Host -Message "$($sbHelp.ToString())"
207205
}
208206
}

0 commit comments

Comments
 (0)