Java 类org.kohsuke.args4j.spi.OptionHandler 实例源码

项目:JavaGraph    文件:PrologChecker.java   
@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;
}
项目:JavaGraph    文件:GrooveCmdLineParser.java   
/** 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() ? '+' : '*');
    }
}
项目:spletne-seje    文件:PropretiesCmdParser.java   
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;
    }
}
项目:gerrit    文件:CmdLineParser.java   
@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;
}
项目:JavaGraph    文件:GrooveCmdLineParser.java   
private int getPrefixLen(OptionHandler<?> h, ResourceBundle rb) {
    if (h.option.usage().length() == 0) {
        return 0;
    }

    return h.getNameAndMeta(rb).length();
}
项目:JavaGraph    文件:CTLModelChecker.java   
@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;
}
项目:spletne-seje    文件:PropretiesCmdParser.java   
@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);
    }
}
项目:spletne-seje    文件:PropretiesCmdParser.java   
/**
 * @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;
}
项目:spletne-seje    文件:PropretiesCmdParser.java   
/**
 * @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;
}
项目:spletne-seje    文件:PropretiesCmdParser.java   
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);
}
项目:spletne-seje    文件:PropretiesCmdParser.java   
/**
 * 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;
}
项目:spletne-seje    文件:PropretiesCmdParser.java   
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;
}
项目:gerrit    文件:CmdLineParser.java   
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;
}
项目:gerrit    文件:CmdLineParser.java   
@SuppressWarnings("rawtypes")
private OptionHandler findHandler(String name) {
  if (options == null) {
    options = index(parser.optionsList);
  }
  return options.get(name);
}
项目:gerrit    文件:CmdLineParser.java   
@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));
}
项目:hops    文件:Experiment1.java   
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();
}
项目:openimaj    文件:ProxyOptionHandler.java   
/**
 * 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();
    }
}
项目:openimaj    文件:ExtendedCmdLineParser.java   
@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);
}
项目:openimaj    文件:ExtendedCmdLineParser.java   
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);
    }
}
项目:openimaj    文件:ExtendedCmdLineParser.java   
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);
    }
}
项目:closure-compiler    文件:CommandLineRunner.java   
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.
  }
}
项目:gaffer    文件:Command.java   
@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();
    }
  }
}
项目:jsonix-schema-compiler    文件:PartialCmdLineParser.java   
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);
}
项目:jsonix-schema-compiler    文件:PartialCmdLineParser.java   
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;
}
项目:jsonix-schema-compiler    文件:PartialCmdLineParser.java   
private boolean isHandlerHasHisOptions(NamedOptionDef option,
        Set<OptionHandler<?>> present) {
    for (String depend : option.depends()) {
        if (!present.contains(findOptionHandler(depend)))
            return false;
    }
    return true;
}
项目:jsonix-schema-compiler    文件:PartialCmdLineParser.java   
private boolean isHandlerAllowOtherOptions(NamedOptionDef option,
        Set<OptionHandler<?>> present) {
    for (String forbid : option.forbids()) {
        if (present.contains(findOptionHandler(forbid)))
            return false;
    }
    return true;
}
项目:spletne-seje    文件:PropretiesCmdParser.java   
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;
}
项目:gerrit    文件:ApproveOption.java   
@Override
public Class<? extends OptionHandler<Short>> handler() {
  return Handler.class;
}
项目:gerrit    文件:CmdLineParserModule.java   
private <T> void registerOptionHandler(Class<T> type, Class<? extends OptionHandler<T>> impl) {
  install(OptionHandlerUtil.moduleFor(type, impl));
}
项目:gerrit    文件:OptionHandlerFactory.java   
OptionHandler<T> create(
org.kohsuke.args4j.CmdLineParser cmdLineParser, OptionDef optionDef, Setter<T> setter);
项目:gerrit    文件:OptionHandlerUtil.java   
@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);
}
项目:gerrit    文件:OptionHandlerUtil.java   
public static <T> Module moduleFor(Class<T> type, Class<? extends OptionHandler<T>> impl) {
  return new FactoryModuleBuilder().implement(handlerOf(type), impl).build(keyFor(type));
}
项目:gerrit    文件:CmdLineParser.java   
@SuppressWarnings("rawtypes")
@Override
public Class<? extends OptionHandler> handler() {
  return o.handler();
}
项目:gerrit    文件:CmdLineParser.java   
@SuppressWarnings("rawtypes")
private OptionHandler add(OptionHandler handler) {
  ensureOptionsInitialized();
  optionsList.add(handler);
  return handler;
}
项目:gerrit    文件:CmdLineParser.java   
private boolean isHandlerSpecified(OptionDef option) {
  return option.handler() != OptionHandler.class;
}
项目:gerrit    文件:CmdLineParser.java   
@Override
public Class<? extends OptionHandler<Boolean>> handler() {
  return BooleanOptionHandler.class;
}
项目:jsonix-schema-compiler    文件:PartialCmdLineParser.java   
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;
}