This is an automated email from the ASF dual-hosted git repository.

paulk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 35a1f0335600bfa460126c55943f8b710ab3b4ac
Author: Paul King <pa...@asert.com.au>
AuthorDate: Sat Aug 2 10:20:33 2025 +1000

    GROOVY-8162: Update Groovysh to JLine3 (allow more Groovy idiomatic 
argument expansion)
---
 .../groovysh/jline/GroovyConsoleEngine.groovy      | 46 ++++++++++++++++++++++
 1 file changed, 46 insertions(+)

diff --git 
a/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/jline/GroovyConsoleEngine.groovy
 
b/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/jline/GroovyConsoleEngine.groovy
index df37e643c7..3b598e5d20 100644
--- 
a/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/jline/GroovyConsoleEngine.groovy
+++ 
b/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/jline/GroovyConsoleEngine.groovy
@@ -25,13 +25,17 @@ import org.jline.console.impl.ConsoleEngineImpl
 
 import java.nio.file.Path
 import java.util.function.Supplier
+import java.util.regex.Matcher
+import java.util.regex.Pattern
 
 class GroovyConsoleEngine extends ConsoleEngineImpl {
     private final Printer printer
+    private final ScriptEngine engine
 
     GroovyConsoleEngine(ScriptEngine engine, Printer printer, Supplier<Path> 
workDir, ConfigurationPath configPath) {
         super(Command.values().toSet() - Command.SLURP, engine, printer, 
workDir, configPath)
         this.printer = printer
+        this.engine = engine
         commandNames().each{ name -> rename(Command."${name.toUpperCase()}", 
"/$name") }
     }
 
@@ -39,6 +43,48 @@ class GroovyConsoleEngine extends ConsoleEngineImpl {
         printer.println(options, object)
     }
 
+    // TODO remove if PR JLine#1371 accepted upstream
+    @Override
+    Object[] expandParameters(String[] args) throws Exception {
+        Object[] out = new Object[args.length]
+        String regexPath = /(.*)\$\{(.*?)}(\/.*)/
+        for (int i = 0; i < args.length; i++) {
+            if (args[i].matches(regexPath)) {
+                Matcher matcher = Pattern.compile(regexPath).matcher(args[i])
+                if (matcher.find()) {
+                    out[i] = matcher.group(1) + engine.get(matcher.group(2)) + 
matcher.group(3)
+                } else {
+                    throw new IllegalArgumentException()
+                }
+            } else if (args[i].startsWith('${')) {
+                String expanded = expandName(args[i])
+                String statement = expanded.startsWith('$') ? args[i][2..-2] : 
expanded
+                out[i] = engine.execute(statement)
+            } else if (args[i].startsWith('$')) {
+                out[i] = engine.get(expandName(args[i]))
+            } else {
+                out[i] = engine.deserialize(args[i])
+            }
+        }
+        out
+    }
+
+    private static String expandName(String name) {
+        String regexVar = "[a-zA-Z_][a-zA-Z0-9_-]*"
+        String out = name
+        if (name.matches('^\\$' + regexVar)) {
+            out = name.substring(1)
+        } else if (name.matches('^\\$\\{' + regexVar + '}.*')) {
+            Matcher matcher = Pattern.compile('^\\$\\{(' + regexVar + 
')}(.*)').matcher(name)
+            if (matcher.find()) {
+                out = matcher.group(1) + matcher.group(2)
+            } else {
+                throw new IllegalArgumentException()
+            }
+        }
+        return out
+    }
+
     @Override
     String name() {
         'Console Commands'

Reply via email to