diff --git a/lib/args4j.jar b/lib/args4j.jar index e188b3df..09f69b22 100644 Binary files a/lib/args4j.jar and b/lib/args4j.jar differ diff --git a/source/net/filebot/cli/BindingsHandler.java b/source/net/filebot/cli/BindingsHandler.java index 85a66df2..9cc72a31 100644 --- a/source/net/filebot/cli/BindingsHandler.java +++ b/source/net/filebot/cli/BindingsHandler.java @@ -1,63 +1,65 @@ - package net.filebot.cli; - -import java.util.AbstractMap.SimpleEntry; -import java.util.Map.Entry; +import java.util.LinkedHashMap; +import java.util.Map; import org.kohsuke.args4j.CmdLineException; import org.kohsuke.args4j.CmdLineParser; import org.kohsuke.args4j.OptionDef; -import org.kohsuke.args4j.spi.OptionHandler; +import org.kohsuke.args4j.spi.FieldSetter; +import org.kohsuke.args4j.spi.MapOptionHandler; import org.kohsuke.args4j.spi.Parameters; import org.kohsuke.args4j.spi.Setter; +public class BindingsHandler extends MapOptionHandler { -public class BindingsHandler extends OptionHandler> { - - public BindingsHandler(CmdLineParser parser, OptionDef option, Setter> setter) { + public BindingsHandler(CmdLineParser parser, OptionDef option, Setter> setter) { super(parser, option, setter); } - - + @Override public String getDefaultMetaVariable() { return "[name=value]"; } - - + @Override public int parseArguments(Parameters params) throws CmdLineException { + FieldSetter fs = setter.asFieldSetter(); + Map map = (Map) fs.getValue(); + if (map == null) { + map = createNewCollection(fs.getType()); + fs.addValue(map); + } + int pos = 0; while (pos < params.size()) { String[] nv = params.getParameter(pos).split("=", 2); - + if (nv.length < 2 || nv[0].startsWith("-")) { return pos; } - + String n = nv[0].trim(); String v = nv[1].trim(); - + if (!isIdentifier(n)) { throw new CmdLineException(owner, String.format("\"%s\" is not a valid identifier", n)); } - - setter.addValue(new SimpleEntry(n, v)); + + map.put(n, v); pos++; } - + return pos; } - - + public boolean isIdentifier(String n) { if (n.isEmpty()) return false; - + for (int i = 0; i < n.length();) { int c = n.codePointAt(i); - + if (i == 0) { if (!Character.isUnicodeIdentifierStart(c)) return false; @@ -65,10 +67,16 @@ public class BindingsHandler extends OptionHandler> { if (!Character.isUnicodeIdentifierPart(c)) return false; } - + i += Character.charCount(c); } - + return true; } + + @Override + protected Map createNewCollection(Class type) { + return new LinkedHashMap(); + } + }