Skip to content

Commit b430008

Browse files
committed
ScriptREPL: fail nicely if there are no languages
1 parent fb31fe6 commit b430008

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/main/java/org/scijava/script/ScriptREPL.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,24 @@ public void initialize() {
122122
out.println("Welcome to the SciJava REPL!");
123123
out.println();
124124
help();
125+
final List<ScriptLanguage> langs = scriptService.getLanguages();
126+
if (langs.isEmpty()) {
127+
out.println("--------------------------------------------------------------");
128+
out.println("Uh oh! There are no SciJava script languages available!");
129+
out.println("Are any on your classpath? E.g.: org.scijava:scripting-groovy?");
130+
out.println("--------------------------------------------------------------");
131+
out.println();
132+
return;
133+
}
125134
out.println("Have fun!");
126135
out.println();
127-
lang(scriptService.getLanguages().get(0).getLanguageName());
136+
lang(langs.get(0).getLanguageName());
128137
populateBindings(interpreter.getBindings());
129138
}
130139

131140
/** Outputs the prompt. */
132141
public void prompt() {
133-
out.print(interpreter.isReady() ? "> " : "\\ ");
142+
out.print(interpreter == null || interpreter.isReady() ? "> " : "\\ ");
134143
}
135144

136145
/**
@@ -148,6 +157,9 @@ public boolean evaluate(final String line) {
148157
else if (tLine.startsWith(":lang ")) lang(line.substring(6).trim());
149158
else if (line.trim().equals(":quit")) return false;
150159
else {
160+
// ensure that a script language is active
161+
if (interpreter == null) return true;
162+
151163
// pass the input to the current interpreter for evaluation
152164
try {
153165
final Object result = interpreter.interpret(line);
@@ -190,6 +202,8 @@ public void help() {
190202

191203
/** Lists variables in the script context. */
192204
public void vars() {
205+
if (interpreter == null) return; // no active script language
206+
193207
final List<String> keys = new ArrayList<String>();
194208
final List<Object> types = new ArrayList<Object>();
195209
final Bindings bindings = interpreter.getBindings();
@@ -213,7 +227,8 @@ public void lang(final String langName) {
213227
// create the new interpreter
214228
final ScriptLanguage language = scriptService.getLanguageByName(langName);
215229
if (language == null) {
216-
throw new IllegalArgumentException("No such language: " + langName);
230+
out.println("No such language: " + langName);
231+
return;
217232
}
218233
final ScriptInterpreter newInterpreter =
219234
new DefaultScriptInterpreter(language);

0 commit comments

Comments
 (0)