Skip to content

Commit 459ca79

Browse files
author
James Brundage
committed
Improving Application Inheritance (Fixes #417)
1 parent 7f5114b commit 459ca79

File tree

1 file changed

+62
-11
lines changed

1 file changed

+62
-11
lines changed

Transpilers/Inherit.psx.ps1

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
} | .>PipeScript
4949
#>
5050
param(
51+
# The command that will be inherited.
5152
[Parameter(Mandatory,Position=0)]
5253
[Alias('CommandName')]
5354
[string]
@@ -104,6 +105,37 @@ $IncludeParameter,
104105
[string[]]
105106
$ExcludeParameter,
106107

108+
# The ArgumentList parameter name
109+
# When inheriting an application, a parameter is created to accept any remaining arguments.
110+
# This is the name of that parameter (by default, 'ArgumentList')
111+
# This parameter is ignored when inheriting from anything other than an application.
112+
[Alias('ArgumentListParameter')]
113+
[string]
114+
$ArgumentListParameterName = 'ArgumentList',
115+
116+
# The ArgumentList parameter aliases
117+
# When inheriting an application, a parameter is created to accept any remaining arguments.
118+
# These are the aliases for that parameter (by default, 'Arguments' and 'Args')
119+
# This parameter is ignored when inheriting from anything other than an application.
120+
[Alias('ArgumentListParameters','ArgumentListParameterNames')]
121+
[string[]]
122+
$ArgumentListParameterAlias = @('Arguments', 'Args'),
123+
124+
# The ArgumentList parameter type
125+
# When inheriting an application, a parameter is created to accept any remaining arguments.
126+
# This is the type of that parameter (by default, '[string[]]')
127+
# This parameter is ignored when inheriting from anything other than an application.
128+
[type]
129+
$ArgumentListParameterType = [string[]],
130+
131+
# The help for the argument list parameter.
132+
# When inheriting an application, a parameter is created to accept any remaining arguments.
133+
# This is the help of that parameter (by default, 'Arguments to $($InhertedApplication.Name)')
134+
# This parameter is ignored when inheriting from anything other than an application.
135+
[Alias('ArgumentListParameterDescription')]
136+
[string]
137+
$ArgumentListParameterHelp = 'Arguments to $($InhertedApplication.Name)',
138+
107139
[Parameter(ValueFromPipeline,ParameterSetName='ScriptBlock')]
108140
[scriptblock]
109141
$ScriptBlock = {}
@@ -147,19 +179,38 @@ process {
147179
$Proxy = $true # it implies -Proxy.
148180
}
149181

150-
# In a couple of cases, we're inheriting an application.
151-
# In this scenario, we want to use the same wrapper [ScriptBlock]
152-
$applicationWrapper = {
153-
param(
154-
[Parameter(ValueFromRemainingArguments)]
155-
[string[]]
156-
$ArgumentList
157-
)
158-
159-
process {
182+
$InhertedApplication =
183+
if ($resolvedCommand -is [Management.Automation.ApplicationInfo]) {
184+
$resolvedCommand
185+
}
186+
elseif (
187+
$resolvedCommand -is [Management.Automation.AliasInfo] -and
188+
$resolvedCommand.ResolvedCommand -is [Management.Automation.ApplicationInfo]
189+
) {
190+
$resolvedCommand.ResolvedCommand
191+
}
192+
193+
if ($InhertedApplication) {
194+
# In a couple of cases, we're inheriting an application.
195+
# In this scenario, we want to use the same wrapper [ScriptBlock]
196+
$paramHelp =
197+
foreach ($helpLine in $ExecutionContext.SessionState.InvokeCommand.ExpandString($ArgumentListParameterHelp) -split '(?>\r\n|\n)') {
198+
"# $HelpLine"
199+
}
200+
201+
$applicationWrapper = New-PipeScript -Parameter @{
202+
$ArgumentListParameterName = @(
203+
$paramHelp
204+
"[Parameter(ValueFromRemainingArguments)]"
205+
"[Alias('$($ArgumentListParameterAlias -join "','")')]"
206+
"[$ArgumentListParameterType]"
207+
"`$$ArgumentListParameterName"
208+
)
209+
} -Process {
160210
& $baseCommand @ArgumentList
161-
}
211+
}
162212
}
213+
163214

164215
# Now we get the script block that we're going to inherit.
165216
$resolvedScriptBlock =

0 commit comments

Comments
 (0)