Allow `*.groovy` files as argument value for `--format`, `--filter` and `--file-filter` CLI options (e.g. `--format /path/to/MyFormat.groovy`)

This commit is contained in:
Reinhard Pointner 2019-05-19 18:41:46 +07:00
parent d9ab6f7acb
commit e3ff4525c0
3 changed files with 52 additions and 3 deletions

View File

@ -6,6 +6,7 @@ Next Release (4.8.6)
* Ensure that `ActionPopup` is always displayed on top of the Windows Task Bar
* Improved `-mediainfo -exec` pipeline
* Added `-no-history` CLI option
* Allow `*.groovy` files as argument value for `--format`, `--filter` and `--file-filter` CLI options (e.g. `--format /path/to/MyFormat.groovy`)
FileBot 4.8.5

View File

@ -71,10 +71,10 @@ public class ArgumentBean {
@Option(name = "--conflict", usage = "Conflict resolution", metaVar = "[skip, override, auto, index, fail]")
public String conflict = "skip";
@Option(name = "--filter", usage = "Match filter expression", metaVar = "{expression}")
@Option(name = "--filter", usage = "Match filter expression", handler = GroovyExpressionHandler.class)
public String filter = null;
@Option(name = "--format", usage = "Format expression", metaVar = "{expression}")
@Option(name = "--format", usage = "Format expression", handler = GroovyExpressionHandler.class)
public String format;
@Option(name = "-non-strict", usage = "Enable advanced matching and more aggressive guessing")
@ -119,7 +119,7 @@ public class ArgumentBean {
@Option(name = "-r", usage = "Recursively process folders")
public boolean recursive = false;
@Option(name = "--file-filter", usage = "Input file filter expression", metaVar = "{expression}")
@Option(name = "--file-filter", usage = "Input file filter expression", handler = GroovyExpressionHandler.class)
public String inputFileFilter = null;
@Option(name = "-exec", usage = "Execute command", metaVar = "echo {f} [+]", handler = RestOfArgumentsHandler.class)

View File

@ -0,0 +1,48 @@
package net.filebot.cli;
import static net.filebot.util.FileUtilities.*;
import java.io.File;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.OptionDef;
import org.kohsuke.args4j.spi.Parameters;
import org.kohsuke.args4j.spi.Setter;
import org.kohsuke.args4j.spi.StringOptionHandler;
public class GroovyExpressionHandler extends StringOptionHandler {
public GroovyExpressionHandler(CmdLineParser parser, OptionDef option, Setter<? super String> setter) {
super(parser, option, setter);
}
@Override
public int parseArguments(Parameters params) throws CmdLineException {
setter.addValue(getStringValue(params.getParameter(0)));
return 1;
}
private String getStringValue(String s) throws CmdLineException {
// try as file path
if (s.endsWith(".groovy")) {
File f = new File(s);
if (f.isFile()) {
try {
return readTextFile(f);
} catch (Exception e) {
throw new CmdLineException(owner, "Failed to read argument value from text file: " + f, e);
}
}
}
// or default to literal value
return s;
}
@Override
public String getDefaultMetaVariable() {
return "{expression}";
}
}