Skip to content

Commit 6245871

Browse files
StartAutomatingStartAutomating
authored andcommitted
[CommandAST].AsSentence() - Improving argumentation (re #242)
1 parent 96ae17c commit 6245871

File tree

1 file changed

+69
-26
lines changed

1 file changed

+69
-26
lines changed

PipeScript.types.ps1xml

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

297297
# Because we want to have flexible open-ended arguments here, we do not hard-code any arguments.
298298
# we parse them.
299-
$IsRightToLeft = $false
300-
foreach ($arg in $arguments) {
301-
if ($arg -match '^-{0,2}RightToLeft$') {
299+
$IsRightToLeft = $false
300+
$SpecificCommands = @()
301+
$specificCommandNames = @()
302+
for ($argIndex =0 ; $argIndex -lt $args.Count; $argIndex++) {
303+
$arg = $args[$argIndex]
304+
if ($arg -is [Management.Automation.CommandInfo]) {
305+
$SpecificCommands += $arg
306+
$specificCommandNames += $arg.Name
307+
continue
308+
}
309+
if ($arg -match '^[-/]{0,2}(?:Is)?RightToLeft$') {
302310
# If -RightToLeft was passed
303311
$IsRightToLeft = $true
312+
continue
313+
}
314+
315+
$argCommands = @(
316+
$foundTranspiler = Get-Transpiler -TranspilerName $arg
317+
if ($foundTranspiler) {
318+
foreach ($transpiler in $foundTranspiler) {
319+
if ($transpiler.Validate($arg)) {
320+
$transpiler
321+
}
322+
}
323+
} else {
324+
$ExecutionContext.SessionState.InvokeCommand.GetCommands($arg, 'All', $true)
325+
}
326+
)
327+
328+
if ($argCommands) {
329+
$SpecificCommands += $argCommands
330+
continue
304331
}
305332
}
306333

334+
307335
$mappedParameters = [Ordered]@{}
308336
$sentence = [Ordered]@{
309337
PSTypeName='PipeScript.Sentence'
@@ -320,37 +348,52 @@ if ($IsRightToLeft) {
320348
[Array]::Reverse($commandElements)
321349
}
322350

323-
# The first command element should be the name of the command.
324-
$firstCommandElement = $commandElements[0]
325-
$commandName = ''
326-
$potentialCommands =
327-
@(if ($firstCommandElement.Value -and $firstCommandElement.StringConstantType -eq 'BareWord') {
328-
$commandName = $firstCommandElement.Value
329-
$foundTranspiler = Get-Transpiler -TranspilerName $commandName
330-
if ($foundTranspiler) {
331-
foreach ($transpiler in $foundTranspiler) {
332-
if ($transpiler.Validate($commandAst)) {
333-
$transpiler
351+
352+
if ($SpecificCommands) {
353+
$potentialCommandNames = $specificCommandNames
354+
$potentialCommands = $SpecificCommands
355+
} else {
356+
357+
# The first command element should be the name of the command.
358+
$firstCommandElement = $commandElements[0]
359+
$commandName = ''
360+
$potentialCommandNames = @()
361+
$potentialCommands =
362+
@(
363+
if ($firstCommandElement.Value -and $firstCommandElement.StringConstantType -eq 'BareWord') {
364+
$commandName = $firstCommandElement.Value
365+
$foundTranspiler = Get-Transpiler -TranspilerName $commandName
366+
if ($foundTranspiler) {
367+
foreach ($transpiler in $foundTranspiler) {
368+
if ($transpiler.Validate($commandAst)) {
369+
$potentialCommandNames += $commandName
370+
$transpiler
371+
}
334372
}
373+
} else {
374+
foreach ($foundCmd in $ExecutionContext.SessionState.InvokeCommand.GetCommands($commandName, 'All', $true)) {
375+
$foundCmd
376+
$potentialCommandNames += $commandName
377+
}
335378
}
336-
} else {
337-
$ExecutionContext.SessionState.InvokeCommand.GetCommands($commandName, 'All', $true)
379+
})
380+
381+
if (-not $potentialCommands) {
382+
[PSCustomObject][Ordered]@{
383+
PSTypeName = 'PipeScript.Sentence'
384+
Keyword = ''
385+
Command = $null
386+
Arguments = $commandElements[0..$commandElements.Length]
338387
}
339-
})
340-
341-
if (-not $potentialCommands) {
342-
[PSCustomObject][Ordered]@{
343-
PSTypeName = 'PipeScript.Sentence'
344-
Keyword = $commandName
345-
Command = $null
346-
Arguments = $commandElements[1..$commandElements.Length]
347388
}
348389
}
349390

350-
351391
$mappedParameters = [Ordered]@{}
352392

393+
$potentialCommandIndex = -1
353394
foreach ($potentialCommand in $potentialCommands) {
395+
$potentialCommandIndex++
396+
$commandName = $potentialCommandName = $potentialCommandNames[$potentialCommandIndex]
354397
<#
355398
Each potential command can be thought of as a simple sentence with (mostly) natural syntax
356399

@@ -611,7 +654,7 @@ foreach ($potentialCommand in $potentialCommands) {
611654
$sentence =
612655
[PSCustomObject]@{
613656
PSTypeName = 'PipeScript.Sentence'
614-
Keyword = $commandName
657+
Keyword = $potentialCommandName
615658
Command = $potentialCommand
616659
Clauses = $clauses
617660
Parameters = $mappedParameters

0 commit comments

Comments
 (0)