Skip to content

Commit 96ae17c

Browse files
author
James Brundage
committed
[CommandAST].AsSentence() - Improving argumentation (re #242)
1 parent 435c8c0 commit 96ae17c

File tree

1 file changed

+69
-26
lines changed

1 file changed

+69
-26
lines changed

Types/CommandAST/AsSentence.ps1

Lines changed: 69 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,42 @@ param()
1414

1515
# Because we want to have flexible open-ended arguments here, we do not hard-code any arguments.
1616
# we parse them.
17-
$IsRightToLeft = $false
18-
foreach ($arg in $arguments) {
19-
if ($arg -match '^-{0,2}RightToLeft$') {
17+
$IsRightToLeft = $false
18+
$SpecificCommands = @()
19+
$specificCommandNames = @()
20+
for ($argIndex =0 ; $argIndex -lt $args.Count; $argIndex++) {
21+
$arg = $args[$argIndex]
22+
if ($arg -is [Management.Automation.CommandInfo]) {
23+
$SpecificCommands += $arg
24+
$specificCommandNames += $arg.Name
25+
continue
26+
}
27+
if ($arg -match '^[-/]{0,2}(?:Is)?RightToLeft$') {
2028
# If -RightToLeft was passed
2129
$IsRightToLeft = $true
30+
continue
31+
}
32+
33+
$argCommands = @(
34+
$foundTranspiler = Get-Transpiler -TranspilerName $arg
35+
if ($foundTranspiler) {
36+
foreach ($transpiler in $foundTranspiler) {
37+
if ($transpiler.Validate($arg)) {
38+
$transpiler
39+
}
40+
}
41+
} else {
42+
$ExecutionContext.SessionState.InvokeCommand.GetCommands($arg, 'All', $true)
43+
}
44+
)
45+
46+
if ($argCommands) {
47+
$SpecificCommands += $argCommands
48+
continue
2249
}
2350
}
2451

52+
2553
$mappedParameters = [Ordered]@{}
2654
$sentence = [Ordered]@{
2755
PSTypeName='PipeScript.Sentence'
@@ -38,37 +66,52 @@ if ($IsRightToLeft) {
3866
[Array]::Reverse($commandElements)
3967
}
4068

41-
# The first command element should be the name of the command.
42-
$firstCommandElement = $commandElements[0]
43-
$commandName = ''
44-
$potentialCommands =
45-
@(if ($firstCommandElement.Value -and $firstCommandElement.StringConstantType -eq 'BareWord') {
46-
$commandName = $firstCommandElement.Value
47-
$foundTranspiler = Get-Transpiler -TranspilerName $commandName
48-
if ($foundTranspiler) {
49-
foreach ($transpiler in $foundTranspiler) {
50-
if ($transpiler.Validate($commandAst)) {
51-
$transpiler
69+
70+
if ($SpecificCommands) {
71+
$potentialCommandNames = $specificCommandNames
72+
$potentialCommands = $SpecificCommands
73+
} else {
74+
75+
# The first command element should be the name of the command.
76+
$firstCommandElement = $commandElements[0]
77+
$commandName = ''
78+
$potentialCommandNames = @()
79+
$potentialCommands =
80+
@(
81+
if ($firstCommandElement.Value -and $firstCommandElement.StringConstantType -eq 'BareWord') {
82+
$commandName = $firstCommandElement.Value
83+
$foundTranspiler = Get-Transpiler -TranspilerName $commandName
84+
if ($foundTranspiler) {
85+
foreach ($transpiler in $foundTranspiler) {
86+
if ($transpiler.Validate($commandAst)) {
87+
$potentialCommandNames += $commandName
88+
$transpiler
89+
}
5290
}
91+
} else {
92+
foreach ($foundCmd in $ExecutionContext.SessionState.InvokeCommand.GetCommands($commandName, 'All', $true)) {
93+
$foundCmd
94+
$potentialCommandNames += $commandName
95+
}
5396
}
54-
} else {
55-
$ExecutionContext.SessionState.InvokeCommand.GetCommands($commandName, 'All', $true)
97+
})
98+
99+
if (-not $potentialCommands) {
100+
[PSCustomObject][Ordered]@{
101+
PSTypeName = 'PipeScript.Sentence'
102+
Keyword = ''
103+
Command = $null
104+
Arguments = $commandElements[0..$commandElements.Length]
56105
}
57-
})
58-
59-
if (-not $potentialCommands) {
60-
[PSCustomObject][Ordered]@{
61-
PSTypeName = 'PipeScript.Sentence'
62-
Keyword = $commandName
63-
Command = $null
64-
Arguments = $commandElements[1..$commandElements.Length]
65106
}
66107
}
67108

68-
69109
$mappedParameters = [Ordered]@{}
70110

111+
$potentialCommandIndex = -1
71112
foreach ($potentialCommand in $potentialCommands) {
113+
$potentialCommandIndex++
114+
$commandName = $potentialCommandName = $potentialCommandNames[$potentialCommandIndex]
72115
<#
73116
Each potential command can be thought of as a simple sentence with (mostly) natural syntax
74117
@@ -329,7 +372,7 @@ foreach ($potentialCommand in $potentialCommands) {
329372
$sentence =
330373
[PSCustomObject]@{
331374
PSTypeName = 'PipeScript.Sentence'
332-
Keyword = $commandName
375+
Keyword = $potentialCommandName
333376
Command = $potentialCommand
334377
Clauses = $clauses
335378
Parameters = $mappedParameters

0 commit comments

Comments
 (0)