| | |
| | | private Map<String, OptionHandler> options; |
| | | |
| | | /** |
| | | * Creates a new command line owner that parses arguments/options and set them |
| | | * into the given object. |
| | | * Creates a new command line owner that parses arguments/options and set |
| | | * them into the given object. |
| | | * |
| | | * @param bean instance of a class annotated by |
| | | * @param bean |
| | | * instance of a class annotated by |
| | | * {@link org.kohsuke.args4j.Option} and |
| | | * {@link org.kohsuke.args4j.Argument}. this object will receive |
| | | * values. |
| | | * |
| | | * @throws IllegalAnnotationError if the option bean class is using args4j |
| | | * annotations incorrectly. |
| | | * @throws IllegalAnnotationError |
| | | * if the option bean class is using args4j annotations |
| | | * incorrectly. |
| | | */ |
| | | public CmdLineParser(Object bean) |
| | | throws IllegalAnnotationError { |
| | | public CmdLineParser(Object bean) throws IllegalAnnotationError { |
| | | this.parser = new MyParser(bean); |
| | | } |
| | | |
| | |
| | | |
| | | char next = '?'; |
| | | List<NamedOptionDef> booleans = new ArrayList<NamedOptionDef>(); |
| | | for (@SuppressWarnings("rawtypes") OptionHandler handler : parser.options) { |
| | | for (@SuppressWarnings("rawtypes") |
| | | OptionHandler handler : parser.options) { |
| | | if (handler.option instanceof NamedOptionDef) { |
| | | NamedOptionDef n = (NamedOptionDef) handler.option; |
| | | |
| | |
| | | parser.parseArgument(tmp.toArray(new String[tmp.size()])); |
| | | } |
| | | |
| | | public void parseOptionMap(Map<String, String[]> parameters) |
| | | throws CmdLineException { |
| | | public void parseOptionMap(Map<String, String[]> parameters) throws CmdLineException { |
| | | Multimap<String, String> map = LinkedHashMultimap.create(); |
| | | for (Map.Entry<String, String[]> ent : parameters.entrySet()) { |
| | | for (String val : ent.getValue()) { |
| | |
| | | parseOptionMap(map); |
| | | } |
| | | |
| | | public void parseOptionMap(Multimap<String, String> params) |
| | | throws CmdLineException { |
| | | public void parseOptionMap(Multimap<String, String> params) throws CmdLineException { |
| | | List<String> tmp = Lists.newArrayListWithCapacity(2 * params.size()); |
| | | for (final String key : params.keySet()) { |
| | | String name = makeOption(key); |
| | |
| | | } |
| | | |
| | | 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)) { |
| | | 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)) { |
| | | 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)); |
| | | throw new CmdLineException(parser, String.format("invalid boolean \"%s=%s\"", name, value)); |
| | | } |
| | | |
| | | private class MyParser extends org.kohsuke.args4j.CmdLineParser { |
| | |
| | | |
| | | @SuppressWarnings({"unchecked", "rawtypes"}) |
| | | @Override |
| | | protected OptionHandler createOptionHandler(final OptionDef option, |
| | | final Setter setter) { |
| | | protected OptionHandler createOptionHandler(final OptionDef option, final Setter setter) { |
| | | if (isHandlerSpecified(option) || isEnum(setter) || isPrimitive(setter)) { |
| | | return add(super.createOptionHandler(option, setter)); |
| | | } |