3131
3232package org .scijava .command .console ;
3333
34- import java .io .File ;
35- import java .util .HashMap ;
3634import java .util .LinkedList ;
3735import java .util .Map ;
3836
3937import org .scijava .command .CommandInfo ;
4038import org .scijava .command .CommandService ;
4139import org .scijava .console .AbstractConsoleArgument ;
4240import org .scijava .console .ConsoleArgument ;
41+ import org .scijava .console .ConsoleUtils ;
4342import org .scijava .log .LogService ;
4443import org .scijava .plugin .Parameter ;
4544import 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}
0 commit comments