Skip to content

Commit 5715522

Browse files
committed
Merge branch 'console-refactor'
Consolidates logic for ConsoleArgument implementations, facilitating the split of the RunArgument command to a dedicated command and script version.
2 parents 6969130 + ca551ba commit 5715522

File tree

12 files changed

+373
-69
lines changed

12 files changed

+373
-69
lines changed

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

Lines changed: 33 additions & 39 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,62 +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

62+
// -- Constructor --
63+
64+
public RunArgument() {
65+
super(2, "--run", "--class");
66+
}
67+
6768
// -- ConsoleArgument methods --
6869

6970
@Override
7071
public void handle(final LinkedList<String> args) {
71-
if (!supports(args)) return;
72+
if (!supports(args))
73+
return;
7274

7375
args.removeFirst(); // --run
7476
final String commandToRun = args.removeFirst();
75-
final String optionString = args.isEmpty() ? "" : args.removeFirst();
77+
final String paramString = args.isEmpty() ? "" : args.removeFirst();
7678

77-
run(commandToRun, optionString);
79+
run(commandToRun, paramString);
7880
}
7981

8082
// -- Typed methods --
8183

8284
@Override
8385
public boolean supports(final LinkedList<String> args) {
84-
return args != null && args.size() >= 2 && args.getFirst().equals("--run");
86+
if (!super.supports(args))
87+
return false;
88+
return getInfo(args.get(1)) != null;
8589
}
8690

8791
// -- Helper methods --
8892

8993
/** Implements the {@code --run} command line argument. */
9094
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-
}
95+
// get the command info
96+
final CommandInfo info = getInfo(commandToRun);
10497

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-
}
98+
// couldn't find anything to run
99+
if (info == null)
113100
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);
114109
}
110+
}
115111

116-
// 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) {
117116
CommandInfo info = commandService.getCommand(commandToRun);
118117
if (info == null) {
119118
// command was not a class name; search for command by title instead
@@ -125,11 +124,6 @@ private void run(final String commandToRun, final String optionString) {
125124
}
126125
}
127126
}
128-
129-
// couldn't find anything to run
130-
if (info == null) return;
131-
// TODO: parse the optionString a la ImageJ1
132-
commandService.run(info, true, inputMap);
127+
return info;
133128
}
134-
135129
}

src/main/java/org/scijava/console/AbstractConsoleArgument.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131

3232
package org.scijava.console;
3333

34+
import java.util.HashSet;
3435
import java.util.LinkedList;
36+
import java.util.Set;
3537

3638
import org.scijava.plugin.AbstractHandlerPlugin;
3739

@@ -43,13 +45,43 @@
4345
public abstract class AbstractConsoleArgument extends
4446
AbstractHandlerPlugin<LinkedList<String>> implements ConsoleArgument
4547
{
48+
private int numArgs;
49+
private Set<String> aliasFlags;
50+
51+
public AbstractConsoleArgument() {
52+
this(1, new String[0]);
53+
}
54+
55+
public AbstractConsoleArgument(final String... aliases) {
56+
this(1, aliases);
57+
}
58+
59+
public AbstractConsoleArgument(final int requiredArgs, final String... aliases) {
60+
numArgs = requiredArgs;
61+
aliasFlags = new HashSet<String>();
62+
for (final String s : aliases) aliasFlags.add(s);
63+
}
4664

4765
// -- Typed methods --
4866

67+
@Override
68+
public boolean supports(final LinkedList<String> args) {
69+
if (args == null || args.size() < numArgs) return false;
70+
return isAlias(args);
71+
}
72+
4973
@Override
5074
@SuppressWarnings({ "rawtypes", "unchecked" })
5175
public Class<LinkedList<String>> getType() {
5276
return (Class) String.class;
5377
}
5478

79+
/**
80+
* @return true if there are no aliases for this {@code ConsoleArgument}, or
81+
* at least one alias matches the first argument in the provided
82+
* list
83+
*/
84+
protected boolean isAlias(final LinkedList<String> args) {
85+
return aliasFlags.isEmpty() || aliasFlags.contains(args.getFirst());
86+
}
5587
}
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: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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.LinkedList;
34+
35+
import org.scijava.Context;
36+
import org.scijava.plugin.Parameter;
37+
import org.scijava.plugin.Plugin;
38+
import org.scijava.ui.UIService;
39+
40+
/**
41+
* Handles the {@code --headless} argument to signal that no UI will be opened
42+
* and the enclosing {@link Context} will not be used after the
43+
* {@link ConsoleService} argument processing is complete.
44+
*
45+
* @author Mark Hiner hinerm at gmail.com
46+
*/
47+
@Plugin(type = ConsoleArgument.class)
48+
public class HeadlessArgument extends AbstractConsoleArgument {
49+
50+
@Parameter(required = false)
51+
private UIService uiService;
52+
53+
// -- Constructor --
54+
55+
public HeadlessArgument() {
56+
super(1, "--headless");
57+
}
58+
59+
// -- ConsoleArgument methods --
60+
61+
@Override
62+
public void handle(final LinkedList<String> args) {
63+
if (!supports(args)) return;
64+
65+
args.removeFirst(); // --headless
66+
67+
uiService.setHeadless(true);
68+
}
69+
// -- Typed methods --
70+
71+
@Override
72+
public boolean supports(final LinkedList<String> args) {
73+
return uiService != null && super.supports(args);
74+
}
75+
76+
}

src/main/java/org/scijava/console/SystemPropertyArgument.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ public class SystemPropertyArgument extends AbstractConsoleArgument {
4949
private static final String SYS_PROP_REGEX = "-D([\\w\\._-]+)(=(.*))?";
5050
private static final Pattern SYS_PROP_PAT = Pattern.compile(SYS_PROP_REGEX);
5151

52+
// -- Constructor --
53+
54+
public SystemPropertyArgument() {
55+
super(1);
56+
}
57+
5258
// -- ConsoleArgument methods --
5359

5460
@Override
@@ -69,7 +75,7 @@ public void handle(final LinkedList<String> args) {
6975

7076
@Override
7177
public boolean supports(final LinkedList<String> args) {
72-
if (args == null || args.isEmpty()) return false;
78+
if (!super.supports(args)) return false;
7379
final String arg = args.getFirst();
7480
if (!arg.startsWith("-D")) return false;
7581
return SYS_PROP_PAT.matcher(arg).matches();

src/main/java/org/scijava/io/console/OpenArgument.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ public class OpenArgument extends AbstractConsoleArgument {
5959
@Parameter
6060
private LogService log;
6161

62+
// -- Constructor --
63+
64+
public OpenArgument() {
65+
super(2, "--open");
66+
}
67+
6268
// -- ConsoleArgument methods --
6369

6470
@Override
@@ -76,12 +82,4 @@ public void handle(final LinkedList<String> args) {
7682
log.error(exc);
7783
}
7884
}
79-
80-
// -- Typed methods --
81-
82-
@Override
83-
public boolean supports(final LinkedList<String> args) {
84-
return args != null && args.size() >= 2 && args.getFirst().equals("--open");
85-
}
86-
8785
}

0 commit comments

Comments
 (0)