Skip to content

Commit b02aee7

Browse files
committed
CommandCodeRunner: allow running commands by title
The soon-to-be-deprecated org.scijava.command.console.RunArgument supports that, so we need to keep supporting it here, too.
1 parent fdbfce3 commit b02aee7

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

src/main/java/org/scijava/command/run/CommandCodeRunner.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.Map;
3636

3737
import org.scijava.command.Command;
38+
import org.scijava.command.CommandInfo;
3839
import org.scijava.command.CommandService;
3940
import org.scijava.plugin.Parameter;
4041
import org.scijava.plugin.Plugin;
@@ -58,21 +59,29 @@ public class CommandCodeRunner extends AbstractCodeRunner {
5859
public void run(final Object code, final Object... args)
5960
throws InvocationTargetException
6061
{
61-
waitFor(commandService.run(getCommandClass(code), true, args));
62+
final Class<? extends Command> c = getCommandClass(code);
63+
if (c != null) waitFor(commandService.run(c, true, args));
64+
65+
final CommandInfo info = getCommandInfo(code);
66+
if (info != null) waitFor(commandService.run(info, true, args));
6267
}
6368

6469
@Override
6570
public void run(final Object code, final Map<String, Object> inputMap)
6671
throws InvocationTargetException
6772
{
68-
waitFor(commandService.run(getCommandClass(code), true, inputMap));
73+
final Class<? extends Command> c = getCommandClass(code);
74+
if (c != null) waitFor(commandService.run(c, true, inputMap));
75+
76+
final CommandInfo info = getCommandInfo(code);
77+
if (info != null) waitFor(commandService.run(info, true, inputMap));
6978
}
7079

7180
// -- Typed methods --
7281

7382
@Override
7483
public boolean supports(final Object code) {
75-
return getCommandClass(code) != null;
84+
return getCommandClass(code) != null || getCommandInfo(code) != null;
7685
}
7786

7887
// -- Helper methods --
@@ -86,4 +95,19 @@ private Class<? extends Command> getCommandClass(final Object code) {
8695
return commandClass;
8796
}
8897

98+
private CommandInfo getCommandInfo(final Object code) {
99+
if (!(code instanceof String)) return null;
100+
final String command = (String) code;
101+
102+
final CommandInfo info = commandService.getCommand(command);
103+
if (info != null) return info;
104+
105+
// command was not a class name; search for command by title instead
106+
for (final CommandInfo ci : commandService.getCommands()) {
107+
if (command.equals(ci.getTitle())) return ci;
108+
}
109+
110+
return null;
111+
}
112+
89113
}

0 commit comments

Comments
 (0)