Skip to content

Commit 02834bf

Browse files
author
James Brundage
committed
New-PipeScript: Adding -ParameterHelp (Fixes #172)
1 parent a027938 commit 02834bf

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

New-PipeScript.ps1.ps1

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
Creates new PipeScript and PowerShell ScriptBlocks.
88
.EXAMPLE
99
New-PipeScript -Parameter @{a='b'}
10+
.EXAMPLE
11+
New-PipeScript -Parameter ([Net.HttpWebRequest].GetProperties()) -ParameterHelp @{
12+
Accept='
13+
HTTP Accept.
14+
15+
HTTP Accept indicates what content types the web request will accept as a response.
16+
'
17+
}
1018
#>
1119
[Alias('New-ScriptBlock')]
1220
param(
@@ -73,7 +81,11 @@
7381
# The type used for automatically generated parameters.
7482
# By default, ```[PSObject]```.
7583
[type]
76-
$AutoParameterType = [PSObject]
84+
$AutoParameterType = [PSObject],
85+
86+
# If provided, will add inline help to parameters.
87+
[Collections.IDictionary]
88+
$ParameterHelp
7789
)
7890

7991
begin {
@@ -84,6 +96,19 @@
8496
$allEndBlocks = @()
8597
$allProcessBlocks = @()
8698
$allHeaders = @()
99+
100+
filter embedParameterHelp {
101+
if ($_ -notmatch '^\s\<\#' -and $_ -notmatch '^\s\#') {
102+
$commentLines = @($_ -split '(?>\r\n|\n)')
103+
if ($commentLines.Count -gt 1) {
104+
'<#' + [Environment]::NewLine + "$_".Trim() + [Environment]::newLine + '#>'
105+
} else {
106+
"# $_"
107+
}
108+
} else {
109+
$_
110+
}
111+
}
87112
}
88113

89114
process {
@@ -102,6 +127,9 @@
102127
$parameterName = $EachParameter.Key
103128
$ParametersToCreate[$EachParameter.Key] =
104129
@(
130+
if ($parameterHelp -and $parameterHelp[$eachParameter.Key]) {
131+
$parameterHelp[$eachParameter.Key] | embedParameterHelp
132+
}
105133
$parameterAttribute = "[Parameter(ValueFromPipelineByPropertyName)]"
106134
$parameterType
107135
'$' + $parameterName
@@ -144,6 +172,9 @@
144172
# treat it as parameter name
145173
$ParametersToCreate[$Parameter] =
146174
@(
175+
if ($parameterHelp -and $parameterHelp[$Parameter]) {
176+
$parameterHelp[$Parameter] | embedParameterHelp
177+
}
147178
"[Parameter(ValueFromPipelineByPropertyName)]"
148179
"`$$Parameter"
149180
) -join [Environment]::NewLine
@@ -184,6 +215,9 @@
184215
}
185216
$ParametersToCreate[$prop.Name] =
186217
@(
218+
if ($parameterHelp -and $parameterHelp[$prop.Name]) {
219+
$parameterHelp[$prop.Name] | embedParameterHelp
220+
}
187221
$parameterAttribute = "[Parameter(ValueFromPipelineByPropertyName)]"
188222
$parameterAttribute
189223
if ($paramType -eq [boolean]) {

0 commit comments

Comments
 (0)