* improved support for running CLI commands with options from Groovy Pad
This commit is contained in:
parent
87d222e81a
commit
3956223761
|
@ -1,6 +1,5 @@
|
||||||
package net.filebot.cli;
|
package net.filebot.cli;
|
||||||
|
|
||||||
import static java.util.Arrays.*;
|
|
||||||
import static java.util.Collections.*;
|
import static java.util.Collections.*;
|
||||||
import static net.filebot.util.FileUtilities.*;
|
import static net.filebot.util.FileUtilities.*;
|
||||||
|
|
||||||
|
@ -193,8 +192,8 @@ public class ArgumentBean {
|
||||||
this.array = array;
|
this.array = array;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> getArray() {
|
public String[] getArray() {
|
||||||
return unmodifiableList(asList(array));
|
return array.clone();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ArgumentBean parse(String[] args) throws CmdLineException {
|
public static ArgumentBean parse(String[] args) throws CmdLineException {
|
||||||
|
|
|
@ -85,6 +85,7 @@ public class ArgumentProcessor {
|
||||||
} else {
|
} else {
|
||||||
// execute user script
|
// execute user script
|
||||||
Bindings bindings = new SimpleBindings();
|
Bindings bindings = new SimpleBindings();
|
||||||
|
bindings.put(ScriptShell.SHELL_ARGV_BINDING_NAME, args.getArray());
|
||||||
bindings.put(ScriptShell.ARGV_BINDING_NAME, args.getFiles(false));
|
bindings.put(ScriptShell.ARGV_BINDING_NAME, args.getFiles(false));
|
||||||
|
|
||||||
DefaultScriptProvider scriptProvider = new DefaultScriptProvider();
|
DefaultScriptProvider scriptProvider = new DefaultScriptProvider();
|
||||||
|
|
|
@ -21,6 +21,7 @@ import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import javax.script.Bindings;
|
||||||
import javax.script.ScriptException;
|
import javax.script.ScriptException;
|
||||||
import javax.script.SimpleBindings;
|
import javax.script.SimpleBindings;
|
||||||
import javax.swing.AbstractAction;
|
import javax.swing.AbstractAction;
|
||||||
|
@ -233,15 +234,19 @@ public class GroovyPad extends JFrame {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
result = shell.evaluate(script, new SimpleBindings());
|
Bindings bindings = new SimpleBindings();
|
||||||
|
bindings.put(ScriptShell.SHELL_ARGV_BINDING_NAME, Settings.getApplicationArguments().getArray());
|
||||||
|
bindings.put(ScriptShell.ARGV_BINDING_NAME, Settings.getApplicationArguments().getFiles(false));
|
||||||
|
|
||||||
|
result = shell.evaluate(script, bindings);
|
||||||
|
|
||||||
// print result and make sure to flush Groovy output
|
// print result and make sure to flush Groovy output
|
||||||
SimpleBindings binding = new SimpleBindings();
|
SimpleBindings resultBindings = new SimpleBindings();
|
||||||
binding.put("result", result);
|
resultBindings.put("result", result);
|
||||||
if (result != null) {
|
if (result != null) {
|
||||||
shell.evaluate("print('Result: '); println(result);", binding);
|
shell.evaluate("print('Result: '); println(result);", resultBindings);
|
||||||
} else {
|
} else {
|
||||||
shell.evaluate("println();", binding);
|
shell.evaluate("println();", resultBindings);
|
||||||
}
|
}
|
||||||
} catch (ScriptException e) {
|
} catch (ScriptException e) {
|
||||||
while (e.getCause() instanceof ScriptException) {
|
while (e.getCause() instanceof ScriptException) {
|
||||||
|
|
|
@ -20,6 +20,7 @@ public class ScriptShell {
|
||||||
|
|
||||||
public static final String ARGV_BINDING_NAME = "args";
|
public static final String ARGV_BINDING_NAME = "args";
|
||||||
public static final String SHELL_BINDING_NAME = "__shell";
|
public static final String SHELL_BINDING_NAME = "__shell";
|
||||||
|
public static final String SHELL_ARGV_BINDING_NAME = "__args";
|
||||||
|
|
||||||
private final ScriptEngine engine;
|
private final ScriptEngine engine;
|
||||||
private final ScriptProvider scriptProvider;
|
private final ScriptProvider scriptProvider;
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package net.filebot.cli;
|
package net.filebot.cli;
|
||||||
|
|
||||||
import static java.util.Collections.*;
|
import static java.util.Collections.*;
|
||||||
import static net.filebot.Settings.*;
|
|
||||||
import static net.filebot.cli.CLILogging.*;
|
import static net.filebot.cli.CLILogging.*;
|
||||||
import static net.filebot.util.StringUtilities.*;
|
import static net.filebot.util.StringUtilities.*;
|
||||||
import groovy.lang.Closure;
|
import groovy.lang.Closure;
|
||||||
|
@ -35,7 +34,6 @@ import javax.script.SimpleBindings;
|
||||||
|
|
||||||
import net.filebot.HistorySpooler;
|
import net.filebot.HistorySpooler;
|
||||||
import net.filebot.RenameAction;
|
import net.filebot.RenameAction;
|
||||||
import net.filebot.Settings;
|
|
||||||
import net.filebot.StandardRenameAction;
|
import net.filebot.StandardRenameAction;
|
||||||
import net.filebot.WebServices;
|
import net.filebot.WebServices;
|
||||||
import net.filebot.format.AssociativeScriptObject;
|
import net.filebot.format.AssociativeScriptObject;
|
||||||
|
@ -78,15 +76,16 @@ public abstract class ScriptShellBaseClass extends Script {
|
||||||
|
|
||||||
public void include(String input) throws Throwable {
|
public void include(String input) throws Throwable {
|
||||||
try {
|
try {
|
||||||
executeScript(input, null);
|
executeScript(input, new String[0], null, null);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
printException(e, true);
|
printException(e, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object runScript(String input) throws Throwable {
|
public Object runScript(String input, String... argv) throws Throwable {
|
||||||
try {
|
try {
|
||||||
return executeScript(input, null);
|
ArgumentBean args = argv == null || argv.length == 0 ? getArgumentBean() : ArgumentBean.parse(argv);
|
||||||
|
return executeScript(input, args.getArray(), args.defines, args.getFiles(false));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
printException(e, true);
|
printException(e, true);
|
||||||
}
|
}
|
||||||
|
@ -94,6 +93,10 @@ public abstract class ScriptShellBaseClass extends Script {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object executeScript(String input, Map<String, ?> bindings, Object... args) throws Throwable {
|
public Object executeScript(String input, Map<String, ?> bindings, Object... args) throws Throwable {
|
||||||
|
return executeScript(input, getArgumentBean().getArray(), bindings, FileUtilities.asFileList(args));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object executeScript(String input, String[] argv, Map<String, ?> bindings, List<File> args) throws Throwable {
|
||||||
// apply parent script defines
|
// apply parent script defines
|
||||||
Bindings parameters = new SimpleBindings();
|
Bindings parameters = new SimpleBindings();
|
||||||
|
|
||||||
|
@ -101,7 +104,9 @@ public abstract class ScriptShellBaseClass extends Script {
|
||||||
if (bindings != null) {
|
if (bindings != null) {
|
||||||
parameters.putAll(bindings);
|
parameters.putAll(bindings);
|
||||||
}
|
}
|
||||||
parameters.put(ScriptShell.ARGV_BINDING_NAME, FileUtilities.asFileList(args));
|
|
||||||
|
parameters.put(ScriptShell.SHELL_ARGV_BINDING_NAME, argv);
|
||||||
|
parameters.put(ScriptShell.ARGV_BINDING_NAME, args);
|
||||||
|
|
||||||
// run given script
|
// run given script
|
||||||
ScriptShell shell = (ScriptShell) getBinding().getVariable(ScriptShell.SHELL_BINDING_NAME);
|
ScriptShell shell = (ScriptShell) getBinding().getVariable(ScriptShell.SHELL_BINDING_NAME);
|
||||||
|
@ -146,12 +151,12 @@ public abstract class ScriptShellBaseClass extends Script {
|
||||||
|
|
||||||
// define global variable: _args
|
// define global variable: _args
|
||||||
public ArgumentBean get_args() {
|
public ArgumentBean get_args() {
|
||||||
return getApplicationArguments();
|
return getArgumentBean();
|
||||||
}
|
}
|
||||||
|
|
||||||
// define global variable: _def
|
// define global variable: _def
|
||||||
public Map<String, String> get_def() {
|
public Map<String, String> get_def() {
|
||||||
return unmodifiableMap(getApplicationArguments().defines);
|
return getArgumentBean().defines;
|
||||||
}
|
}
|
||||||
|
|
||||||
// define global variable: _system
|
// define global variable: _system
|
||||||
|
@ -458,6 +463,14 @@ public abstract class ScriptShellBaseClass extends Script {
|
||||||
return files;
|
return files;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private ArgumentBean getArgumentBean() {
|
||||||
|
try {
|
||||||
|
return ArgumentBean.parse((String[]) getBinding().getVariable(ScriptShell.SHELL_ARGV_BINDING_NAME));
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new IllegalStateException(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private Map<Option, Object> getDefaultOptions(Map<String, ?> parameters) throws Exception {
|
private Map<Option, Object> getDefaultOptions(Map<String, ?> parameters) throws Exception {
|
||||||
Map<Option, Object> options = new EnumMap<Option, Object>(Option.class);
|
Map<Option, Object> options = new EnumMap<Option, Object>(Option.class);
|
||||||
|
|
||||||
|
@ -469,7 +482,7 @@ public abstract class ScriptShellBaseClass extends Script {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ArgumentBean defaultValues = Settings.getApplicationArguments();
|
ArgumentBean args = getArgumentBean();
|
||||||
Set<Option> complement = EnumSet.allOf(Option.class);
|
Set<Option> complement = EnumSet.allOf(Option.class);
|
||||||
complement.removeAll(options.keySet());
|
complement.removeAll(options.keySet());
|
||||||
|
|
||||||
|
@ -479,10 +492,10 @@ public abstract class ScriptShellBaseClass extends Script {
|
||||||
options.put(missing, false);
|
options.put(missing, false);
|
||||||
break;
|
break;
|
||||||
case strict:
|
case strict:
|
||||||
options.put(missing, !defaultValues.nonStrict);
|
options.put(missing, !args.nonStrict);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
options.put(missing, defaultValues.getClass().getField(missing.name()).get(defaultValues));
|
options.put(missing, args.getClass().getField(missing.name()).get(args));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue