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 eb7e7d45d1daddc5ef0d0164556f57a657df3ec4
Author: Paul King <[email protected]>
AuthorDate: Mon Apr 13 05:43:55 2026 +1000

    support groovy-csv in groovy-groovysh if on the classpath
---
 .../groovy/groovysh/jline/GroovyCommands.groovy    | 24 ++++++++++++++--------
 1 file changed, 15 insertions(+), 9 deletions(-)

diff --git 
a/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/jline/GroovyCommands.groovy
 
b/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/jline/GroovyCommands.groovy
index d5e6693c70..1e1cffad3f 100644
--- 
a/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/jline/GroovyCommands.groovy
+++ 
b/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/jline/GroovyCommands.groovy
@@ -413,8 +413,7 @@ class GroovyCommands extends JlineCommandRegistry 
implements CommandRegistry {
                     def parser = getParser(format, slurpers[format])
                     out = parser.parse(source)
                 } else if (format == 'CSV') {
-                    def parser = getCsvParser(format)
-                    out = parser.parse(source).toList()*.toMap()
+                    out = parseCsv(source)
                 } else if (format == 'PROPERTIES') {
                     out = new Properties().tap {load(source) }
                 } else {
@@ -429,8 +428,7 @@ class GroovyCommands extends JlineCommandRegistry 
implements CommandRegistry {
                     def parser = getParser(format, slurpers[format])
                     out = parser.parseText(arg)
                 } else if (format == 'CSV') {
-                    def parser = getCsvParser(format)
-                    out = parser.parse(new StringReader(arg)).toList()*.toMap()
+                    out = parseCsv(new StringReader(arg))
                 } else {
                     out = engine.deserialize(arg, 'AUTO')
                 }
@@ -442,12 +440,20 @@ class GroovyCommands extends JlineCommandRegistry 
implements CommandRegistry {
         out
     }
 
-    private getCsvParser(String format) {
-        def parserName = 'org.apache.commons.csv.CSVFormat'
-        if (!ClassUtils.lookFor(engine, parserName)) {
-            throw new IllegalArgumentException("$format format requires 
$parserName to be available")
+    private static final String GROOVY_CSV_SLURPER = 'groovy.csv.CsvSlurper'
+    private static final String COMMONS_CSV_FORMAT = 
'org.apache.commons.csv.CSVFormat'
+
+    private parseCsv(Reader source) {
+        // Try groovy-csv (CsvSlurper) first, then fall back to Apache Commons 
CSV
+        if (ClassUtils.lookFor(GROOVY_CSV_SLURPER)) {
+            def parser = engine.execute("new ${GROOVY_CSV_SLURPER}()")
+            return parser.parse(source)
+        }
+        if (ClassUtils.lookFor(engine, COMMONS_CSV_FORMAT)) {
+            def parser = 
engine.execute("${COMMONS_CSV_FORMAT}.DEFAULT.builder().setHeader().setSkipHeaderRecord(true).build()")
+            return parser.parse(source).toList()*.toMap()
         }
-        
engine.execute("${parserName}.DEFAULT.builder().setHeader().setSkipHeaderRecord(true).build()")
+        throw new IllegalArgumentException("CSV format requires 
$GROOVY_CSV_SLURPER or $COMMONS_CSV_FORMAT to be available")
     }
 
     Object getParser(String format, String parserName) {

Reply via email to