|
| 1 | +package org.hisrc.jsonix.args4j; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.HashSet; |
| 5 | +import java.util.Set; |
| 6 | + |
| 7 | +import org.apache.commons.lang3.Validate; |
| 8 | +import org.kohsuke.args4j.CmdLineException; |
| 9 | +import org.kohsuke.args4j.CmdLineParser; |
| 10 | +import org.kohsuke.args4j.NamedOptionDef; |
| 11 | +import org.kohsuke.args4j.ParserProperties; |
| 12 | +import org.kohsuke.args4j.spi.OptionHandler; |
| 13 | +import org.kohsuke.args4j.spi.Parameters; |
| 14 | + |
| 15 | +public class PartialCmdLineParser extends CmdLineParser { |
| 16 | + |
| 17 | + private OptionHandler<?> currentOptionHandler; |
| 18 | + |
| 19 | + public PartialCmdLineParser(Object bean) { |
| 20 | + super(bean); |
| 21 | + } |
| 22 | + |
| 23 | + public PartialCmdLineParser(Object bean, ParserProperties parserProperties) { |
| 24 | + super(bean, parserProperties); |
| 25 | + } |
| 26 | + |
| 27 | + public int parseArgument(final String[] args, final int position) |
| 28 | + throws CmdLineException { |
| 29 | + Validate.noNullElements(args); |
| 30 | + currentOptionHandler = null; |
| 31 | + |
| 32 | + CmdLineImpl cmdLine = new CmdLineImpl(args, position); |
| 33 | + |
| 34 | + Set<OptionHandler<?>> present = new HashSet<OptionHandler<?>>(); |
| 35 | + int argIndex = position; |
| 36 | + int consumed = 0; |
| 37 | + |
| 38 | + while (cmdLine.hasMore()) { |
| 39 | + String arg = cmdLine.getCurrentToken(); |
| 40 | + if (isOption(arg)) { |
| 41 | + // '=' is for historical compatibility fallback |
| 42 | + boolean isKeyValuePair = arg.contains(getProperties() |
| 43 | + .getOptionValueDelimiter()) || arg.indexOf('=') != -1; |
| 44 | + |
| 45 | + // parse this as an option. |
| 46 | + currentOptionHandler = isKeyValuePair ? findOptionHandler(arg) |
| 47 | + : findOptionByName(arg); |
| 48 | + |
| 49 | + if (currentOptionHandler == null) { |
| 50 | + return consumed; |
| 51 | + } |
| 52 | + |
| 53 | + // known option; skip its name |
| 54 | + if (isKeyValuePair) { |
| 55 | + cmdLine.splitToken(); |
| 56 | + } else { |
| 57 | + cmdLine.proceed(1); |
| 58 | + consumed++; |
| 59 | + } |
| 60 | + } else { |
| 61 | + if (argIndex >= getArguments().size()) { |
| 62 | + return consumed; |
| 63 | + } |
| 64 | + |
| 65 | + // known argument |
| 66 | + currentOptionHandler = getArguments().get(argIndex); |
| 67 | + if (currentOptionHandler == null) // this is a programmer error. |
| 68 | + // arg index should be |
| 69 | + // continuous |
| 70 | + throw new IllegalStateException("@Argument with index=" |
| 71 | + + argIndex + " is undefined"); |
| 72 | + |
| 73 | + if (!currentOptionHandler.option.isMultiValued()) |
| 74 | + argIndex++; |
| 75 | + } |
| 76 | + int diff = currentOptionHandler.parseArguments(cmdLine); |
| 77 | + cmdLine.proceed(diff); |
| 78 | + consumed += diff; |
| 79 | + present.add(currentOptionHandler); |
| 80 | + } |
| 81 | + |
| 82 | + // check whether a help option is set |
| 83 | + boolean helpSet = false; |
| 84 | + for (OptionHandler<?> handler : getOptions()) { |
| 85 | + if (handler.option.help() && present.contains(handler)) { |
| 86 | + helpSet = true; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + if (!helpSet) { |
| 91 | + checkRequiredOptionsAndArguments(present); |
| 92 | + } |
| 93 | + |
| 94 | + return consumed; |
| 95 | + } |
| 96 | + |
| 97 | + private OptionHandler<?> findOptionHandler(String name) { |
| 98 | + // Look for key/value pair first. |
| 99 | + int pos = name.indexOf(getProperties().getOptionValueDelimiter()); |
| 100 | + if (pos < 0) { |
| 101 | + pos = name.indexOf('='); // historical compatibility fallback |
| 102 | + } |
| 103 | + if (pos > 0) { |
| 104 | + name = name.substring(0, pos); |
| 105 | + } |
| 106 | + return findOptionByName(name); |
| 107 | + } |
| 108 | + |
| 109 | + private OptionHandler<?> findOptionByName(String name) { |
| 110 | + for (OptionHandler<?> h : getOptions()) { |
| 111 | + NamedOptionDef option = (NamedOptionDef) h.option; |
| 112 | + if (name.equals(option.name())) { |
| 113 | + return h; |
| 114 | + } |
| 115 | + for (String alias : option.aliases()) { |
| 116 | + if (name.equals(alias)) { |
| 117 | + return h; |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + return null; |
| 122 | + } |
| 123 | + |
| 124 | + private void checkRequiredOptionsAndArguments(Set<OptionHandler<?>> present) |
| 125 | + throws CmdLineException { |
| 126 | + // make sure that all mandatory options are present |
| 127 | + for (OptionHandler<?> handler : getOptions()) { |
| 128 | + if (handler.option.required() && !present.contains(handler)) { |
| 129 | + throw new CmdLineException(this, |
| 130 | + Messages.REQUIRED_OPTION_MISSING, |
| 131 | + handler.option.toString()); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + // make sure that all mandatory arguments are present |
| 136 | + for (OptionHandler<?> handler : getOptions()) { |
| 137 | + if (handler.option.required() && !present.contains(handler)) { |
| 138 | + throw new CmdLineException(this, |
| 139 | + Messages.REQUIRED_ARGUMENT_MISSING, |
| 140 | + handler.option.toString()); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + // make sure that all requires arguments are present |
| 145 | + for (OptionHandler<?> handler : present) { |
| 146 | + if (handler.option instanceof NamedOptionDef |
| 147 | + && !isHandlerHasHisOptions((NamedOptionDef) handler.option, |
| 148 | + present)) { |
| 149 | + throw new CmdLineException(this, |
| 150 | + Messages.REQUIRES_OPTION_MISSING, |
| 151 | + handler.option.toString(), |
| 152 | + Arrays.toString(((NamedOptionDef) handler.option) |
| 153 | + .depends())); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + // make sure that all forbids arguments are not present |
| 158 | + for (OptionHandler<?> handler : present) { |
| 159 | + if (handler.option instanceof NamedOptionDef |
| 160 | + && !isHandlerAllowOtherOptions( |
| 161 | + (NamedOptionDef) handler.option, present)) { |
| 162 | + throw new CmdLineException(this, |
| 163 | + Messages.FORBIDDEN_OPTION_PRESENT, |
| 164 | + handler.option.toString(), |
| 165 | + Arrays.toString(((NamedOptionDef) handler.option) |
| 166 | + .forbids())); |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + private boolean isHandlerHasHisOptions(NamedOptionDef option, |
| 172 | + Set<OptionHandler<?>> present) { |
| 173 | + for (String depend : option.depends()) { |
| 174 | + if (!present.contains(findOptionHandler(depend))) |
| 175 | + return false; |
| 176 | + } |
| 177 | + return true; |
| 178 | + } |
| 179 | + |
| 180 | + private boolean isHandlerAllowOtherOptions(NamedOptionDef option, |
| 181 | + Set<OptionHandler<?>> present) { |
| 182 | + for (String forbid : option.forbids()) { |
| 183 | + if (present.contains(findOptionHandler(forbid))) |
| 184 | + return false; |
| 185 | + } |
| 186 | + return true; |
| 187 | + } |
| 188 | + |
| 189 | + private class CmdLineImpl implements Parameters { |
| 190 | + private final String[] args; |
| 191 | + private int pos; |
| 192 | + |
| 193 | + CmdLineImpl(String[] args, int position) { |
| 194 | + this.args = args; |
| 195 | + pos = position; |
| 196 | + } |
| 197 | + |
| 198 | + protected boolean hasMore() { |
| 199 | + return pos < args.length; |
| 200 | + } |
| 201 | + |
| 202 | + protected String getCurrentToken() { |
| 203 | + return args[pos]; |
| 204 | + } |
| 205 | + |
| 206 | + private void proceed(int n) { |
| 207 | + pos += n; |
| 208 | + } |
| 209 | + |
| 210 | + public String getParameter(int idx) throws CmdLineException { |
| 211 | + if (pos + idx >= args.length || pos + idx < 0) |
| 212 | + throw new CmdLineException(PartialCmdLineParser.this, |
| 213 | + Messages.MISSING_OPERAND, getOptionName()); |
| 214 | + return args[pos + idx]; |
| 215 | + } |
| 216 | + |
| 217 | + public int size() { |
| 218 | + return args.length - pos; |
| 219 | + } |
| 220 | + |
| 221 | + /** |
| 222 | + * Used when the current token is of the form "-option=value", to |
| 223 | + * replace the current token by "value", as if this was given as two |
| 224 | + * tokens "-option value" |
| 225 | + */ |
| 226 | + void splitToken() { |
| 227 | + if (pos < args.length && pos >= 0) { |
| 228 | + int idx = args[pos].indexOf("="); |
| 229 | + if (idx > 0) { |
| 230 | + args[pos] = args[pos].substring(idx + 1); |
| 231 | + } |
| 232 | + } |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + private String getOptionName() { |
| 237 | + return currentOptionHandler.option.toString(); |
| 238 | + } |
| 239 | +} |
0 commit comments