Skip to content

Commit a318dab

Browse files
StartAutomatingStartAutomating
authored andcommitted
CommandAst.AsSentence: Expanding array literals (Fixes #291)
1 parent 402f257 commit a318dab

File tree

1 file changed

+39
-26
lines changed

1 file changed

+39
-26
lines changed

PipeScript.types.ps1xml

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,31 @@ $decamelCase = [Regex]::new('(?<=[a-z])(?=[A-Z])')
291291
For the purposes of natural language processing ValueFromPipeline will be ignored.
292292

293293
The order the parameters is declared takes precedence over Position attributes.
294+
.NOTES
295+
Each potential command can be thought of as a simple sentence with (mostly) natural syntax
296+
297+
command <parametername> ...<parameterargument> (etc)
298+
299+
either more natural or PowerShell syntax should be allowed, for example:
300+
301+
all functions can Quack {
302+
"quack"
303+
}
304+
305+
would map to the command all and the parameters -Function and -Can (with the arguments Quack and {"quack"})
306+
307+
Assuming -Functions was a `[switch]` or an alias to a `[switch]`, it will match that `[switch]` and only that switch.
308+
309+
If -Functions was not a `[switch]`, it will match values from that point.
310+
311+
If the parameter type is not a list or PSObject, only the next parameter will be matched.
312+
313+
If the parameter type *is* a list or an PSObject,
314+
or ValueFromRemainingArguments is present and no named parameters were found,
315+
then all remaining arguments will be matched until the next named parameter is found.
316+
317+
_Aliasing is important_ when working with a given parameter.
318+
The alias, _not_ the parameter name, will be what is mapped in .Parameters.
294319
#>
295320
param()
296321

@@ -349,6 +374,20 @@ if ($IsRightToLeft) {
349374
}
350375

351376

377+
$commandElements = # Walk thru each command element
378+
@(foreach ($element in $commandElements) {
379+
# If the element is an array literal, expand it
380+
if ($element -is [Management.Automation.Language.ArrayLiteralAst]) {
381+
$element.Elements
382+
} else {
383+
# otherwise, include it as is.
384+
$element
385+
}
386+
})
387+
388+
# Now we have all of the words in a sentence.
389+
# We can still determine if an item in a list was in a list by inspecting it's parent.
390+
352391
$sentences = @()
353392
if ($SpecificCommands) {
354393
$potentialCommands = $SpecificCommands
@@ -399,32 +438,6 @@ $potentialCommandIndex = -1
399438
foreach ($potentialCommand in $potentialCommands) {
400439
$potentialCommandIndex++
401440
$commandName = $potentialCommandName = $potentialCommandNames[$potentialCommandIndex]
402-
<#
403-
Each potential command can be thought of as a simple sentence with (mostly) natural syntax
404-
405-
command <parametername> ...<parameterargument> (etc)
406-
407-
either more natural or PowerShell syntax should be allowed, for example:
408-
409-
all functions can Quack {
410-
"quack"
411-
}
412-
413-
would map to the command all and the parameters -Function and -Can (with the arguments Quack and {"quack"})
414-
415-
Assuming -Functions was a `[switch]` or an alias to a `[switch]`, it will match that `[switch]` and only that switch.
416-
417-
If -Functions was not a `[switch]`, it will match values from that point.
418-
419-
If the parameter type is not a list or PSObject, only the next parameter will be matched.
420-
421-
If the parameter type *is* a list or an PSObject,
422-
or ValueFromRemainingArguments is present and no named parameters were found,
423-
then all remaining arguments will be matched until the next named parameter is found.
424-
425-
_Aliasing is important_ when working with a given parameter. The alias, _not_ the parameter name, will be what is mapped.
426-
427-
#>
428441

429442
# Cache the potential parameters
430443
$potentialParameters = $potentialCommand.Parameters

0 commit comments

Comments
 (0)