Skip to content

Commit ca551ba

Browse files
committed
Split RunArgument script logic to new class
RunArgument now handles commands exclusively, while RunScriptArgument handles scripts. Shared logic between these classes (input mapping) is moved to a ConsoleUtils utility class.
1 parent 17ccc39 commit ca551ba

File tree

3 files changed

+218
-42
lines changed

3 files changed

+218
-42
lines changed

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

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,21 @@
3131

3232
package org.scijava.command.console;
3333

34-
import java.io.File;
35-
import java.util.HashMap;
3634
import java.util.LinkedList;
3735
import java.util.Map;
3836

3937
import org.scijava.command.CommandInfo;
4038
import org.scijava.command.CommandService;
4139
import org.scijava.console.AbstractConsoleArgument;
4240
import org.scijava.console.ConsoleArgument;
41+
import org.scijava.console.ConsoleUtils;
4342
import org.scijava.log.LogService;
4443
import org.scijava.plugin.Parameter;
4544
import org.scijava.plugin.Plugin;
46-
import org.scijava.script.ScriptService;
4745

4846
/**
4947
* Handles the {@code --run} command line argument.
50-
*
48+
*
5149
* @author Curtis Rueden
5250
* @author Johannes Schindelin
5351
* @author Mark Hiner hinerm at gmail.com
@@ -58,61 +56,63 @@ public class RunArgument extends AbstractConsoleArgument {
5856
@Parameter
5957
private CommandService commandService;
6058

61-
@Parameter
62-
private ScriptService scriptService;
63-
6459
@Parameter
6560
private LogService logService;
6661

6762
// -- Constructor --
6863

6964
public RunArgument() {
70-
super(2, "--run");
65+
super(2, "--run", "--class");
7166
}
7267

7368
// -- ConsoleArgument methods --
7469

7570
@Override
7671
public void handle(final LinkedList<String> args) {
77-
if (!supports(args)) return;
72+
if (!supports(args))
73+
return;
7874

7975
args.removeFirst(); // --run
8076
final String commandToRun = args.removeFirst();
81-
final String optionString = args.isEmpty() ? "" : args.removeFirst();
77+
final String paramString = args.isEmpty() ? "" : args.removeFirst();
8278

83-
run(commandToRun, optionString);
79+
run(commandToRun, paramString);
80+
}
81+
82+
// -- Typed methods --
83+
84+
@Override
85+
public boolean supports(final LinkedList<String> args) {
86+
if (!super.supports(args))
87+
return false;
88+
return getInfo(args.get(1)) != null;
8489
}
8590

8691
// -- Helper methods --
8792

8893
/** Implements the {@code --run} command line argument. */
8994
private void run(final String commandToRun, final String optionString) {
90-
final Map<String, Object> inputMap = new HashMap<String, Object>();
91-
92-
if (!optionString.isEmpty()) {
93-
final String[] pairs = optionString.split(",");
94-
for (final String pair : pairs) {
95-
final String[] split = pair.split("=");
96-
if (split.length != 2) {
97-
logService.error("Parameters must be formatted as a comma-separated list of key=value pairs");
98-
return;
99-
}
100-
inputMap.put(split[0], split[1]);
101-
}
102-
}
95+
// get the command info
96+
final CommandInfo info = getInfo(commandToRun);
10397

104-
// first check if this is a script
105-
final File scriptFile = new File(commandToRun);
106-
if (scriptFile.exists() && scriptService.canHandleFile(commandToRun)) {
107-
try {
108-
scriptService.run(scriptFile, true, inputMap).get();
109-
} catch (final Exception exc) {
110-
logService.error(exc);
111-
}
98+
// couldn't find anything to run
99+
if (info == null)
112100
return;
101+
102+
// TODO: parse the optionString a la ImageJ1
103+
final Map<String, Object> inputMap = ConsoleUtils.parseParameterString(optionString, logService);
104+
105+
try {
106+
commandService.run(info, true, inputMap).get();
107+
} catch (final Exception exc) {
108+
logService.error(exc);
113109
}
110+
}
114111

115-
// Not a script, check if it's a command class
112+
/**
113+
* Try to convert the given string to a {@link CommandInfo}
114+
*/
115+
private CommandInfo getInfo(final String commandToRun) {
116116
CommandInfo info = commandService.getCommand(commandToRun);
117117
if (info == null) {
118118
// command was not a class name; search for command by title instead
@@ -124,14 +124,6 @@ private void run(final String commandToRun, final String optionString) {
124124
}
125125
}
126126
}
127-
128-
// couldn't find anything to run
129-
if (info == null) return;
130-
// TODO: parse the optionString a la ImageJ1
131-
try {
132-
commandService.run(info, true, inputMap).get();
133-
} catch (final Exception exc) {
134-
logService.error(exc);
135-
}
127+
return info;
136128
}
137129
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2016 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
package org.scijava.console;
32+
33+
import java.util.HashMap;
34+
import java.util.Map;
35+
36+
import org.scijava.log.LogService;
37+
38+
/**
39+
* Helper class for {@link ConsoleArgument}s.
40+
*
41+
* @author Mark Hiner hinerm at gmail.com
42+
*/
43+
public final class ConsoleUtils {
44+
45+
public static Map<String, Object> parseParameterString(final String parameterString) {
46+
return parseParameterString(parameterString, null);
47+
}
48+
49+
public static Map<String, Object> parseParameterString(final String parameterString, final LogService logService) {
50+
final Map<String, Object> inputMap = new HashMap<String, Object>();
51+
52+
if (!parameterString.isEmpty()) {
53+
final String[] pairs = parameterString.split(",");
54+
for (final String pair : pairs) {
55+
final String[] split = pair.split("=");
56+
if (split.length == 2)
57+
inputMap.put(split[0], split[1]);
58+
else if (logService != null)
59+
logService.error("Parameters must be formatted as a comma-separated list of key=value pairs");
60+
61+
}
62+
}
63+
64+
return inputMap;
65+
}
66+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2016 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
package org.scijava.script.console;
32+
33+
import java.io.File;
34+
import java.util.LinkedList;
35+
import java.util.Map;
36+
37+
import org.scijava.console.AbstractConsoleArgument;
38+
import org.scijava.console.ConsoleArgument;
39+
import org.scijava.console.ConsoleUtils;
40+
import org.scijava.log.LogService;
41+
import org.scijava.plugin.Parameter;
42+
import org.scijava.plugin.Plugin;
43+
import org.scijava.script.ScriptService;
44+
45+
/**
46+
* {@link ConsoleArgument} for executing scripts directly.
47+
*
48+
* @author Mark Hiner hinerm at gmail.com
49+
*/
50+
@Plugin(type = ConsoleArgument.class)
51+
public class RunScriptArgument extends AbstractConsoleArgument {
52+
53+
@Parameter
54+
private ScriptService scriptService;
55+
56+
@Parameter
57+
private LogService logService;
58+
59+
// -- Constructor --
60+
61+
public RunScriptArgument() {
62+
super(2, "--run", "--script");
63+
}
64+
65+
// -- ConsoleArgument methods --
66+
67+
@Override
68+
public void handle(final LinkedList<String> args) {
69+
if (!supports(args))
70+
return;
71+
72+
args.removeFirst(); // --run
73+
final String scriptToRun = args.removeFirst();
74+
final String paramString = args.isEmpty() ? "" : args.removeFirst();
75+
76+
run(scriptToRun, paramString);
77+
}
78+
79+
// -- Typed methods --
80+
81+
@Override
82+
public boolean supports(final LinkedList<String> args) {
83+
if (!super.supports(args))
84+
return false;
85+
return getScript(args.get(1)) != null;
86+
}
87+
88+
// -- Helper methods --
89+
90+
/**
91+
* Run the script
92+
*/
93+
private void run(final String scriptToRun, final String paramString) {
94+
final File script = getScript(scriptToRun);
95+
96+
// couldn't find anything to run
97+
if (script == null)
98+
return;
99+
100+
// TODO: parse the optionString a la ImageJ1
101+
final Map<String, Object> inputMap = ConsoleUtils.parseParameterString(paramString, logService);
102+
103+
try {
104+
scriptService.run(script, true, inputMap).get();
105+
} catch (final Exception exc) {
106+
logService.error(exc);
107+
}
108+
}
109+
110+
/**
111+
* Try to convert the given string to a {@link File} representing a
112+
* supported script type.
113+
*/
114+
private File getScript(final String string) {
115+
final File scriptFile = new File(string);
116+
return scriptFile.exists() && scriptService.canHandleFile(scriptFile) ? scriptFile : null;
117+
}
118+
}

0 commit comments

Comments
 (0)