-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathScriptCLI.java
More file actions
169 lines (161 loc) · 5.83 KB
/
Copy pathScriptCLI.java
File metadata and controls
169 lines (161 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* #%L
* SciJava Common shared library for SciJava software.
* %%
* Copyright (C) 2009 - 2026 SciJava developers.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package org.scijava.script;
import java.io.File;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.scijava.Context;
import org.scijava.log.LogLevel;
import org.scijava.log.LogService;
import org.scijava.module.Module;
/**
* A command-line entry point for running SciJava scripts.
*
* @author Curtis Rueden
*/
public class ScriptCLI {
private static final String USAGE = "" + //
"Usage: " + ScriptCLI.class.getSimpleName() + //
" [-d] [-h] [-l language] [-o] [-r] /path/to/script [script-args]\n" + //
"\n" + //
"Options:\n" + //
" -d, --debug : enable debug-level log output\n" + //
" -h, --help : display this help message\n" + //
" -l, --language : specify language of script to execute\n" + //
" otherwise, inferred from script extension\n" + //
" -o, --print-outputs : print output values\n" + //
" -r, --print-return-value : print return value\n" + //
"\n" + //
"To read from stdin, use a dash (-) symbol for the script path.\n" + //
"\n" + //
"For script-args, give space-separated key=value pairs,\n" + //
"while will be passed in as SciJava script arguments.";
public static void main(String... args) throws Exception {
final Map<String, Object> inputs = new HashMap<>();
File file = null;
String language = null;
boolean printOutputs = false;
boolean printReturnValue = false;
boolean parsingOptions = true;
try (final Context context = new Context()) {
// Parse command-line arguments.
if (args.length == 0) args = new String[] {"-h"};
for (int i = 0; i < args.length; i++) {
if (parsingOptions) {
// Parse options and filename.
if (args[i].equals("-d") || args[i].equals("--debug")) {
final LogService log = context.getService(LogService.class);
if (log != null) log.setLevel(LogLevel.DEBUG);
}
else if (args[i].equals("-h") || args[i].equals("--help")) {
System.err.println(USAGE);
System.exit(1);
}
else if (i < args.length - 1 && //
args[i].equals("-l") || args[i].equals("--language"))
{
language = args[++i];
}
else if (args[i].equals("-o") || args[i].equals("--print-outputs")) {
printOutputs = true;
}
else if (args[i].equals("-r") || args[i].equals("--print-return-value")) {
printReturnValue = true;
}
else if (args[i].equals("-")) {
// read from stdin
parsingOptions = false;
}
else if (i < args.length - 1 && args[i].equals("--")) {
// argument after the -- separator must be the filename.
file = new File(args[++i]);
parsingOptions = false;
}
else if (new File(args[i]).exists()) {
file = new File(args[i]);
parsingOptions = false;
}
else {
System.err.println("Invalid argument: " + args[i]);
System.exit(2);
}
}
else {
// Parse script arguments.
final int equals = args[i].indexOf("=");
if (equals < 0) {
System.err.println("Invalid argument: " + args[i]);
System.exit(3);
}
final String key = args[i].substring(0, equals);
final String val = args[i].substring(equals + 1);
inputs.put(key, val);
}
}
final ScriptService ss = context.getService(ScriptService.class);
if (ss == null) {
System.err.println("Error: No script service available.");
System.exit(4);
}
if (file == null && language == null) {
System.err.println("Error: Must specify language when using stdin.");
System.exit(5);
}
final Module m;
if (language == null) {
m = ss.run(file, true, inputs).get();
}
else {
ScriptLanguage lang = ss.getLanguageByName(language);
if (lang == null) lang = ss.getLanguageByExtension(language);
if (lang == null) {
System.err.println("Error: Unsupported language: " + language);
System.exit(6);
}
final Reader reader = file == null ? //
new InputStreamReader(System.in) : //
new FileReader(file);
m = ss.run("." + language, reader, true, inputs).get();
}
if (printOutputs) {
for (Entry<String, Object> output : m.getOutputs().entrySet()) {
System.out.println(output.getKey() + " = " + output.getValue());
}
}
if (printReturnValue && m instanceof ScriptModule) {
System.out.println(((ScriptModule) m).getReturnValue());
}
}
System.exit(0);
}
}