Support parsing command line options from a map
Given a map of String to String[], like to what an HttpServletRequest makes available from the query string, feed the values into the parser and update the bean with the parsed state. This makes parsing from a query string rather trivial. Change-Id: I41829d35e59594770c1b6512c241b92182a03afe
This commit is contained in:
@@ -42,6 +42,7 @@ import com.google.inject.assistedinject.Assisted;
|
||||
import org.kohsuke.args4j.Argument;
|
||||
import org.kohsuke.args4j.CmdLineException;
|
||||
import org.kohsuke.args4j.IllegalAnnotationError;
|
||||
import org.kohsuke.args4j.NamedOptionDef;
|
||||
import org.kohsuke.args4j.Option;
|
||||
import org.kohsuke.args4j.OptionDef;
|
||||
import org.kohsuke.args4j.spi.BooleanOptionHandler;
|
||||
@@ -53,6 +54,7 @@ import java.io.Writer;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
/**
|
||||
@@ -64,6 +66,7 @@ import java.util.ResourceBundle;
|
||||
* args4j style format prior to invoking args4j for parsing.
|
||||
*/
|
||||
public class CmdLineParser {
|
||||
|
||||
public interface Factory {
|
||||
CmdLineParser create(Object bean);
|
||||
}
|
||||
@@ -140,10 +143,78 @@ public class CmdLineParser {
|
||||
|
||||
tmp.add(str);
|
||||
}
|
||||
|
||||
parser.parseArgument(tmp.toArray(new String[tmp.size()]));
|
||||
}
|
||||
|
||||
public void parseOptionMap(Map<String, String[]> parameters)
|
||||
throws CmdLineException {
|
||||
ArrayList<String> tmp = new ArrayList<String>();
|
||||
for (Map.Entry<String, String[]> ent : parameters.entrySet()) {
|
||||
String name = ent.getKey();
|
||||
if (!name.startsWith("-")) {
|
||||
if (name.length() == 1) {
|
||||
name = "-" + name;
|
||||
} else {
|
||||
name = "--" + name;
|
||||
}
|
||||
}
|
||||
|
||||
if (findHandler(name) instanceof BooleanOptionHandler) {
|
||||
boolean on = false;
|
||||
for (String value : ent.getValue()) {
|
||||
on = toBoolean(ent.getKey(), value);
|
||||
}
|
||||
if (on) {
|
||||
tmp.add(name);
|
||||
}
|
||||
} else {
|
||||
for (String value : ent.getValue()) {
|
||||
tmp.add(name);
|
||||
tmp.add(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
parser.parseArgument(tmp.toArray(new String[tmp.size()]));
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
private OptionHandler findHandler(String name) {
|
||||
for (OptionHandler handler : parser.options) {
|
||||
if (handler.option instanceof NamedOptionDef) {
|
||||
NamedOptionDef def = (NamedOptionDef) handler.option;
|
||||
if (name.equals(def.name())) {
|
||||
return handler;
|
||||
}
|
||||
for (String alias : def.aliases()) {
|
||||
if (name.equals(alias)) {
|
||||
return handler;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private boolean toBoolean(String name, String value) throws CmdLineException {
|
||||
if ("true".equals(value) || "t".equals(value)
|
||||
|| "yes".equals(value) || "y".equals(value)
|
||||
|| "on".equals(value)
|
||||
|| "1".equals(value)
|
||||
|| value == null || "".equals(value)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ("false".equals(value) || "f".equals(value)
|
||||
|| "no".equals(value) || "n".equals(value)
|
||||
|| "off".equals(value)
|
||||
|| "0".equals(value)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
throw new CmdLineException(parser, String.format(
|
||||
"invalid boolean \"%s=%s\"", name, value));
|
||||
}
|
||||
|
||||
private class MyParser extends org.kohsuke.args4j.CmdLineParser {
|
||||
@SuppressWarnings("rawtypes")
|
||||
private List<OptionHandler> options;
|
||||
|
||||
Reference in New Issue
Block a user