Skip to content

Commit 402f257

Browse files
author
James Brundage
committed
CommandAst.AsSentence: Expanding array literals (Fixes #291)
1 parent fa14589 commit 402f257

File tree

1 file changed

+39
-26
lines changed

1 file changed

+39
-26
lines changed

Types/CommandAST/AsSentence.ps1

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,31 @@
99
For the purposes of natural language processing ValueFromPipeline will be ignored.
1010
1111
The order the parameters is declared takes precedence over Position attributes.
12+
.NOTES
13+
Each potential command can be thought of as a simple sentence with (mostly) natural syntax
14+
15+
command <parametername> ...<parameterargument> (etc)
16+
17+
either more natural or PowerShell syntax should be allowed, for example:
18+
19+
all functions can Quack {
20+
"quack"
21+
}
22+
23+
would map to the command all and the parameters -Function and -Can (with the arguments Quack and {"quack"})
24+
25+
Assuming -Functions was a `[switch]` or an alias to a `[switch]`, it will match that `[switch]` and only that switch.
26+
27+
If -Functions was not a `[switch]`, it will match values from that point.
28+
29+
If the parameter type is not a list or PSObject, only the next parameter will be matched.
30+
31+
If the parameter type *is* a list or an PSObject,
32+
or ValueFromRemainingArguments is present and no named parameters were found,
33+
then all remaining arguments will be matched until the next named parameter is found.
34+
35+
_Aliasing is important_ when working with a given parameter.
36+
The alias, _not_ the parameter name, will be what is mapped in .Parameters.
1237
#>
1338
param()
1439

@@ -67,6 +92,20 @@ if ($IsRightToLeft) {
6792
}
6893

6994

95+
$commandElements = # Walk thru each command element
96+
@(foreach ($element in $commandElements) {
97+
# If the element is an array literal, expand it
98+
if ($element -is [Management.Automation.Language.ArrayLiteralAst]) {
99+
$element.Elements
100+
} else {
101+
# otherwise, include it as is.
102+
$element
103+
}
104+
})
105+
106+
# Now we have all of the words in a sentence.
107+
# We can still determine if an item in a list was in a list by inspecting it's parent.
108+
70109
$sentences = @()
71110
if ($SpecificCommands) {
72111
$potentialCommands = $SpecificCommands
@@ -117,32 +156,6 @@ $potentialCommandIndex = -1
117156
foreach ($potentialCommand in $potentialCommands) {
118157
$potentialCommandIndex++
119158
$commandName = $potentialCommandName = $potentialCommandNames[$potentialCommandIndex]
120-
<#
121-
Each potential command can be thought of as a simple sentence with (mostly) natural syntax
122-
123-
command <parametername> ...<parameterargument> (etc)
124-
125-
either more natural or PowerShell syntax should be allowed, for example:
126-
127-
all functions can Quack {
128-
"quack"
129-
}
130-
131-
would map to the command all and the parameters -Function and -Can (with the arguments Quack and {"quack"})
132-
133-
Assuming -Functions was a `[switch]` or an alias to a `[switch]`, it will match that `[switch]` and only that switch.
134-
135-
If -Functions was not a `[switch]`, it will match values from that point.
136-
137-
If the parameter type is not a list or PSObject, only the next parameter will be matched.
138-
139-
If the parameter type *is* a list or an PSObject,
140-
or ValueFromRemainingArguments is present and no named parameters were found,
141-
then all remaining arguments will be matched until the next named parameter is found.
142-
143-
_Aliasing is important_ when working with a given parameter. The alias, _not_ the parameter name, will be what is mapped.
144-
145-
#>
146159

147160
# Cache the potential parameters
148161
$potentialParameters = $potentialCommand.Parameters

0 commit comments

Comments
 (0)