@Override protected GrooveCmdLineParser createParser(String appName) { GrooveCmdLineParser result = new GrooveCmdLineParser(appName, this); // move -g to the final position @SuppressWarnings("rawtypes") List<OptionHandler> handlers = result.getOptions(); OptionHandler<?> genHandler = null; for (OptionHandler<?> handler : handlers) { if (handler instanceof GeneratorHandler) { genHandler = handler; } } handlers.remove(genHandler); handlers.add(genHandler); return result; }
/** Modified from superclass to add parameter controlling * closing bracket printing. */ protected void printSingleLineOption(PrintWriter pw, OptionHandler<?> h, ResourceBundle rb, boolean closeOpt) { pw.print(' '); boolean multiOccurrences = (h instanceof MapOptionHandler) || h.option.isMultiValued(); boolean brackets = !h.option.required() || multiOccurrences; if (brackets) { pw.print('['); } pw.print(h.getNameAndMeta(rb)); if (h.option.isArgument() && h.option.isMultiValued()) { pw.print(" ..."); } if (brackets && closeOpt) { pw.print(']'); } if (!h.option.isArgument() && multiOccurrences) { pw.print(h.option.required() ? '+' : '*'); } }
private Set<OptionHandler> parseArgumentProps(Set<OptionHandler> present) throws CmdLineException { if (popsFile != null) { Properties properties = new Properties(); try { properties.load(new FileInputStream(popsFile)); for (String s : properties.stringPropertyNames()) { currentOptionHandler = findOptionByAliasName(s); if (currentOptionHandler == null) { System.err.println("Ignoring " + s + " = " + properties.getProperty(s)); } else if (!present.contains(currentOptionHandler)) { CmdLineImpl line = new CmdLineImpl(properties.getProperty(s)); currentOptionHandler.parseArguments(line); present.add(currentOptionHandler); } } } catch (IOException e) { throw new CmdLineException(this, "Error reading propreties"); } return present; } else { return present; } }
@SuppressWarnings("rawtypes") private static Map<String, OptionHandler> index(List<OptionHandler> in) { Map<String, OptionHandler> m = new HashMap<>(); for (OptionHandler handler : in) { if (handler.option instanceof NamedOptionDef) { NamedOptionDef def = (NamedOptionDef) handler.option; if (!def.isArgument()) { m.put(def.name(), handler); for (String alias : def.aliases()) { m.put(alias, handler); } } } } return m; }
private int getPrefixLen(OptionHandler<?> h, ResourceBundle rb) { if (h.option.usage().length() == 0) { return 0; } return h.getNameAndMeta(rb).length(); }
@Override protected GrooveCmdLineParser createParser(String appName) { GrooveCmdLineParser result = new GrooveCmdLineParser(appName, this) { @Override public void printSingleLineUsage(Writer w, ResourceBundle rb) { int optionCount = getOptions().size(); PrintWriter pw = new PrintWriter(w); for (int ix = 0; ix < optionCount - 1; ix++) { printSingleLineOption(pw, getOptions().get(ix), rb, true); } pw.print(" ["); pw.print(getOptions().get(optionCount - 1) .getNameAndMeta(rb)); pw.print(" | "); pw.print(getArguments().get(0) .getNameAndMeta(rb)); pw.print(']'); pw.flush(); } }; // move -g to the final position @SuppressWarnings("rawtypes") List<OptionHandler> handlers = result.getOptions(); OptionHandler<?> genHandler = null; for (OptionHandler<?> handler : handlers) { if (handler instanceof GeneratorHandler) { genHandler = handler; } } handlers.remove(genHandler); handlers.add(genHandler); return result; }
@Override public void parseArgument(String... args) throws CmdLineException { Set<OptionHandler> present = parseArgumentCmd(args); boolean helpSet = false; for (OptionHandler handler : getOptions()) { if (handler.option.help() && present.contains(handler)) { helpSet = true; } } if (!helpSet) { checkRequiredOptionsAndArguments(present); } }
/** * @return {@code true} if all options required by {@code option} are present, {@code false} otherwise */ private boolean isHandlerHasHisOptions(NamedOptionDef option, Set<OptionHandler> present) { for (String depend : option.depends()) { if (!present.contains(findOptionHandler(depend))) { return false; } } return true; }
/** * @return {@code true} if all options forbid by {@code option} are not present, {@code false} otherwise */ private boolean isHandlerAllowOtherOptions(NamedOptionDef option, Set<OptionHandler> present) { for (String forbid : option.forbids()) { if (present.contains(findOptionHandler(forbid))) return false; } return true; }
private OptionHandler findOptionHandler(String name) { // Look for key/value pair first. int pos = name.indexOf(getProperties().getOptionValueDelimiter()); if (pos < 0) { pos = name.indexOf('='); // historical compatibility fallback } if (pos > 0) { name = name.substring(0, pos); } return findOptionByName(name); }
/** * Finds a registered {@code OptionHandler} by its name or its alias. * * @param name name * @return the {@code OptionHandler} or {@code null} */ private OptionHandler findOptionByName(String name) { for (OptionHandler h : getOptions()) { NamedOptionDef option = (NamedOptionDef) h.option; if (name.equals(option.name())) { return h; } for (String alias : option.aliases()) { if (name.equals(alias)) { return h; } } } return null; }
protected OptionHandler findOptionByAliasName(String name) { for (OptionHandler h : getOptions()) { NamedOptionDef option = (NamedOptionDef) h.option; for (String alias : option.aliases()) { if (name.equals(alias)) { return h; } } } return null; }
private static String metaVar(OptionHandler<?> handler, NamedOptionDef n) { String var = n.metaVar(); if (Strings.isNullOrEmpty(var)) { var = handler.getDefaultMetaVariable(); if (handler instanceof EnumOptionHandler) { var = var.substring(1, var.length() - 1).replace(" ", ""); } } return var; }
@SuppressWarnings("rawtypes") private OptionHandler findHandler(String name) { if (options == null) { options = index(parser.optionsList); } return options.get(name); }
@SuppressWarnings({"unchecked", "rawtypes"}) @Override protected OptionHandler createOptionHandler(OptionDef option, Setter setter) { if (isHandlerSpecified(option) || isEnum(setter) || isPrimitive(setter)) { return add(super.createOptionHandler(option, setter)); } OptionHandlerFactory<?> factory = handlers.get(setter.getType()); if (factory != null) { return factory.create(this, option, setter); } return add(super.createOptionHandler(option, setter)); }
public void runExperiment(String[] args) throws StorageInitializtionException, StorageException, IOException, ClassNotFoundException, InterruptedException { CmdLineParser parser = new CmdLineParser(this); parser.setUsageWidth(80); try { // parse the arguments. parser.parseArgument(args); OptionHandler hd; } catch (CmdLineException e) { System.err.println(e.getMessage()); parser.printUsage(System.err); System.err.println(); return; } init(); writeStartMessages(args); startProcesses(); waitAllJoin(); waitForTheSystemToStabilize(); killLeaders(); writeResults(); tearDown(); }
/** * Default constructor. * @param parser the parser * @param option the option definition * @param setter the setter * @throws CmdLineException */ public ProxyOptionHandler(CmdLineParser parser, OptionDef option, Setter<? super Object> setter) throws CmdLineException { super(parser, option, setter); OptionDef proxyOption = new OptionDef(option.usage(), option.metaVar(), option.required(), OptionHandler.class, option.isMultiValued()); proxy = parser.createOptionHandler(proxyOption, setter); if (!option.required() && CmdLineOptionsProvider.class.isAssignableFrom(this.setter.getType())) { handleExtraArgs(); } }
@Override protected boolean isOption(String arg) { final boolean isKeyValuePair = arg.indexOf('=') != -1; final OptionHandler<?> handler = isKeyValuePair ? findHandler(arg) : findByName(arg); if (handler == null) { return false; } return super.isOption(arg); }
private OptionHandler<?> findByName(String name) { Method m; try { m = CmdLineParser.class.getDeclaredMethod("findOptionByName", String.class); m.setAccessible(true); return (OptionHandler<?>) m.invoke(this, name); } catch (final Exception e) { throw new RuntimeException(e); } }
private OptionHandler<?> findHandler(String name) { try { final Method m = CmdLineParser.class.getDeclaredMethod("findOptionHandler", String.class); m.setAccessible(true); return (OptionHandler<?>) m.invoke(this, name); } catch (final Exception e) { throw new RuntimeException(e); } }
private void printCategoryUsage( String categoryName, final Collection<String> options, OutputStreamWriter outputStream, String prefix, String suffix) { try { if (prefix != null) { printStringLineWrapped(prefix, outputStream); } outputStream.write(boldPrefix + categoryName + ":\n" + normalPrefix); parser.printUsage( outputStream, null, new OptionHandlerFilter() { @Override public boolean select(OptionHandler optionHandler) { if (optionHandler.option instanceof NamedOptionDef) { return !optionHandler.option.hidden() && options.contains( ((NamedOptionDef) optionHandler.option).name().replaceFirst("^--", "")); } return false; } }); if (suffix != null) { printStringLineWrapped(suffix, outputStream); } } catch (IOException e) { // Ignore. } }
@SuppressWarnings("rawtypes") private void removeHiddenOption(CmdLineParser parser) { Iterator<OptionHandler> iterator = parser.getOptions().iterator(); while (iterator.hasNext()) { OptionHandler next = iterator.next(); if (next.option.hidden()) { iterator.remove(); } } }
private OptionHandler<?> findOptionHandler(String name) { // Look for key/value pair first. int pos = name.indexOf(getProperties().getOptionValueDelimiter()); if (pos < 0) { pos = name.indexOf('='); // historical compatibility fallback } if (pos > 0) { name = name.substring(0, pos); } return findOptionByName(name); }
private OptionHandler<?> findOptionByName(String name) { for (OptionHandler<?> h : getOptions()) { NamedOptionDef option = (NamedOptionDef) h.option; if (name.equals(option.name())) { return h; } for (String alias : option.aliases()) { if (name.equals(alias)) { return h; } } } return null; }
private boolean isHandlerHasHisOptions(NamedOptionDef option, Set<OptionHandler<?>> present) { for (String depend : option.depends()) { if (!present.contains(findOptionHandler(depend))) return false; } return true; }
private boolean isHandlerAllowOtherOptions(NamedOptionDef option, Set<OptionHandler<?>> present) { for (String forbid : option.forbids()) { if (present.contains(findOptionHandler(forbid))) return false; } return true; }
private Set<OptionHandler> parseArgumentCmd(String... args) throws CmdLineException { Utilities.checkNonNull(args, "args"); String expandedArgs[] = args; if (super.getProperties().getAtSyntax()) { expandedArgs = expandAtFiles(args); } CmdLineImpl cmdLine = new CmdLineImpl(expandedArgs); Set<OptionHandler> present = new HashSet<>(); int argIndex = 0; while (cmdLine.hasMore()) { String arg = cmdLine.getCurrentToken(); if (isOption(arg)) { // '=' is for historical compatibility fallback boolean isKeyValuePair = arg.contains(getProperties().getOptionValueDelimiter()) || arg.indexOf('=') != -1; // parse this as an option. currentOptionHandler = isKeyValuePair ? findOptionHandler(arg) : findOptionByName(arg); if (currentOptionHandler == null) { throw new CmdLineException(this, Messages.UNDEFINED_OPTION, arg); } // known option; skip its name if (isKeyValuePair) { cmdLine.splitToken(); } else { cmdLine.proceed(1); } } else { if (argIndex >= getArguments().size()) { Messages msg = getArguments().size() == 0 ? Messages.NO_ARGUMENT_ALLOWED : Messages.TOO_MANY_ARGUMENTS; throw new CmdLineException(this, msg, arg); } // known argument currentOptionHandler = getArguments().get(argIndex); if (currentOptionHandler == null) { // this is a programmer error. arg index should be continuous throw new IllegalStateException("@Argument with index=" + argIndex + " is undefined"); } if (!currentOptionHandler.option.isMultiValued()) { argIndex++; } } int diff = currentOptionHandler.parseArguments(cmdLine); cmdLine.proceed(diff); present.add(currentOptionHandler); } parseArgumentProps(present); return present; }
@Override public Class<? extends OptionHandler<Short>> handler() { return Handler.class; }
private <T> void registerOptionHandler(Class<T> type, Class<? extends OptionHandler<T>> impl) { install(OptionHandlerUtil.moduleFor(type, impl)); }
OptionHandler<T> create( org.kohsuke.args4j.CmdLineParser cmdLineParser, OptionDef optionDef, Setter<T> setter);
@SuppressWarnings("unchecked") private static <T> Key<OptionHandler<T>> handlerOf(Class<T> type) { final Type handlerType = Types.newParameterizedTypeWithOwner(null, OptionHandler.class, type); return (Key<OptionHandler<T>>) Key.get(handlerType); }
public static <T> Module moduleFor(Class<T> type, Class<? extends OptionHandler<T>> impl) { return new FactoryModuleBuilder().implement(handlerOf(type), impl).build(keyFor(type)); }
@SuppressWarnings("rawtypes") @Override public Class<? extends OptionHandler> handler() { return o.handler(); }
@SuppressWarnings("rawtypes") private OptionHandler add(OptionHandler handler) { ensureOptionsInitialized(); optionsList.add(handler); return handler; }
private boolean isHandlerSpecified(OptionDef option) { return option.handler() != OptionHandler.class; }
@Override public Class<? extends OptionHandler<Boolean>> handler() { return BooleanOptionHandler.class; }
public int parseArgument(final String[] args, final int position) throws CmdLineException { Validate.noNullElements(args); currentOptionHandler = null; CmdLineImpl cmdLine = new CmdLineImpl(args, position); Set<OptionHandler<?>> present = new HashSet<OptionHandler<?>>(); int argIndex = position; int consumed = 0; while (cmdLine.hasMore()) { String arg = cmdLine.getCurrentToken(); if (isOption(arg)) { // '=' is for historical compatibility fallback boolean isKeyValuePair = arg.contains(getProperties() .getOptionValueDelimiter()) || arg.indexOf('=') != -1; // parse this as an option. currentOptionHandler = isKeyValuePair ? findOptionHandler(arg) : findOptionByName(arg); if (currentOptionHandler == null) { return consumed; } // known option; skip its name if (isKeyValuePair) { cmdLine.splitToken(); } else { cmdLine.proceed(1); consumed++; } } else { if (argIndex >= getArguments().size()) { return consumed; } // known argument currentOptionHandler = getArguments().get(argIndex); if (currentOptionHandler == null) // this is a programmer error. // arg index should be // continuous throw new IllegalStateException("@Argument with index=" + argIndex + " is undefined"); if (!currentOptionHandler.option.isMultiValued()) argIndex++; } int diff = currentOptionHandler.parseArguments(cmdLine); cmdLine.proceed(diff); consumed += diff; present.add(currentOptionHandler); } // check whether a help option is set boolean helpSet = false; for (OptionHandler<?> handler : getOptions()) { if (handler.option.help() && present.contains(handler)) { helpSet = true; } } if (!helpSet) { checkRequiredOptionsAndArguments(present); } return consumed; }