Skip to content

Commit e759dd8

Browse files
committed
RunArgument: support scripts
When parsing arguments to the --run command line option, check if we were given a valid script name. If so, run the script with the given arguments.
1 parent 4679622 commit e759dd8

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

src/main/java/org/scijava/command/console/RunArgument.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,39 @@
3131

3232
package org.scijava.command.console;
3333

34+
import java.io.File;
35+
import java.util.HashMap;
3436
import java.util.LinkedList;
37+
import java.util.Map;
3538

3639
import org.scijava.command.CommandInfo;
3740
import org.scijava.command.CommandService;
3841
import org.scijava.console.AbstractConsoleArgument;
3942
import org.scijava.console.ConsoleArgument;
43+
import org.scijava.log.LogService;
4044
import org.scijava.plugin.Parameter;
4145
import org.scijava.plugin.Plugin;
46+
import org.scijava.script.ScriptService;
4247

4348
/**
4449
* Handles the {@code --run} command line argument.
4550
*
4651
* @author Curtis Rueden
4752
* @author Johannes Schindelin
53+
* @author Mark Hiner hinerm at gmail.com
4854
*/
4955
@Plugin(type = ConsoleArgument.class)
5056
public class RunArgument extends AbstractConsoleArgument {
5157

5258
@Parameter
5359
private CommandService commandService;
5460

61+
@Parameter
62+
private ScriptService scriptService;
63+
64+
@Parameter
65+
private LogService logService;
66+
5567
// -- ConsoleArgument methods --
5668

5769
@Override
@@ -76,6 +88,32 @@ public boolean supports(final LinkedList<String> args) {
7688

7789
/** Implements the {@code --run} command line argument. */
7890
private void run(final String commandToRun, final String optionString) {
91+
final Map<String, Object> inputMap = new HashMap<String, Object>();
92+
93+
if (!optionString.isEmpty()) {
94+
final String[] pairs = optionString.split(",");
95+
for (final String pair : pairs) {
96+
final String[] split = pair.split("=");
97+
if (split.length != 2) {
98+
logService.error("Parameters must be formatted as a comma-separated list of key=value pairs");
99+
return;
100+
}
101+
inputMap.put(split[0], split[1]);
102+
}
103+
}
104+
105+
// first check if this is a script
106+
final File scriptFile = new File(commandToRun);
107+
if (scriptFile.exists() && scriptService.canHandleFile(commandToRun)) {
108+
try {
109+
scriptService.run(scriptFile, true, inputMap);
110+
} catch (final Exception exc) {
111+
logService.error(exc);
112+
}
113+
return;
114+
}
115+
116+
// Not a script, check if it's a command class
79117
CommandInfo info = commandService.getCommand(commandToRun);
80118
if (info == null) {
81119
// command was not a class name; search for command by title instead
@@ -87,9 +125,11 @@ private void run(final String commandToRun, final String optionString) {
87125
}
88126
}
89127
}
128+
129+
// couldn't find anything to run
90130
if (info == null) return;
91131
// TODO: parse the optionString a la ImageJ1
92-
commandService.run(info, true);
132+
commandService.run(info, true, inputMap);
93133
}
94134

95135
}

0 commit comments

Comments
 (0)