@@ -69,6 +69,8 @@ public class ScriptREPL {
6969
7070 private final PrintStream out ;
7171
72+ private String languagePreference = null ;
73+
7274 /** List of interpreter-friendly script languages. */
7375 private List <ScriptLanguage > languages ;
7476
@@ -88,6 +90,15 @@ public ScriptREPL(final Context context, final OutputStream out) {
8890 (PrintStream ) out : new PrintStream (out );
8991 }
9092
93+ public ScriptREPL (final Context context , final String language ) {
94+ this (context , language , System .out );
95+ }
96+
97+ public ScriptREPL (final Context context , final String language , final OutputStream out ) {
98+ this (context , out );
99+ languagePreference = language ;
100+ }
101+
91102 /**
92103 * Gets the list of languages compatible with the REPL.
93104 * <p>
@@ -162,12 +173,35 @@ public void initialize(final boolean verbose) {
162173 }
163174 out .println ("Have fun!" );
164175 out .println ();
165- lang (langs .get (0 ).getLanguageName ());
176+
177+ if (languagePreference != null ) {
178+ selectPreferredLanguage (langs );
179+ } else {
180+ lang (langs .get (0 ).getLanguageName ());
181+ }
166182 }
167- else if (!langs .isEmpty ()) lang (langs .get (0 ));
183+ else if (!langs .isEmpty ()) {
184+ if (languagePreference != null ) {
185+ selectPreferredLanguage (langs );
186+ } else {
187+ lang (langs .get (0 ));
188+ }
189+ }
190+
168191 populateBindings (interpreter .getBindings ());
169192 }
170193
194+ private void selectPreferredLanguage (List <ScriptLanguage > langs ) {
195+ final ScriptLanguage preference = langs
196+ .stream ().filter (lang -> languagePreference .equals (lang .getLanguageName ()))
197+ .findAny ().orElse (null );
198+ if (preference != null ) {
199+ lang (preference );
200+ } else {
201+ lang (langs .get (0 ).getLanguageName ());
202+ }
203+ }
204+
171205 /** Outputs the prompt. */
172206 public void prompt () {
173207 out .print (interpreter == null || interpreter .isReady () ? "> " : "\\ " );
@@ -310,8 +344,15 @@ public static void main(final String... args) throws Exception {
310344 // make a SciJava application context
311345 final Context context = new Context ();
312346
313- // create the script interpreter
314- final ScriptREPL scriptCLI = new ScriptREPL (context );
347+ // see if we have a preferred language
348+ // and create the script interpreter
349+ final ScriptREPL scriptCLI ;
350+ if (args .length > 0 ) {
351+ final String preference = args [0 ];
352+ scriptCLI = new ScriptREPL (context , preference );
353+ } else {
354+ scriptCLI = new ScriptREPL (context );
355+ }
315356
316357 // start the REPL
317358 scriptCLI .loop ();
0 commit comments