Пример #1
0
  public void registerOption(CmdLineRegisteredOption opt) {
    if (opt == null) {
      // todo
    } else {
      m_registeredOptions.put(opt.getName(), opt);
    }

    if (opt.getAlternativeName() != null) {
      m_registeredOptions.put(opt.getAlternativeName(), opt);
    }
  }
Пример #2
0
  /**
   * If called more than once without calling <code>reset()</code> in between, the options are
   * merged in the style of a <code>Map</code>.
   *
   * @param args
   */
  public void parseArgs(String[] args) {
    if (args == null) {
      return;
    }

    int i = 0;
    while (i < args.length) {
      if (!isKey(args, i)) {
        System.out.println("stray option value " + args[i] + " found outside of any key.");
        // cope with a hererogeneous map for the sake of not losing any arg
        m_options.put(args[i], args[i]);
        i++;
      } else {
        CmdLineOption opt = (CmdLineOption) m_registeredOptions.get(args[i]);
        if (opt == null) {
          opt = new CmdLineOption(args[i]);
        }

        ArrayList values = new ArrayList();

        int lastValIndex = i;
        if (opt instanceof CmdLineRegisteredOption) {
          CmdLineRegisteredOption regOpt = (CmdLineRegisteredOption) opt;
          lastValIndex = i + regOpt.getMinValueCount();
          if (lastValIndex >= args.length) {
            System.out.println(
                "warning: less than the required "
                    + regOpt.getMinValueCount()
                    + " values given for option "
                    + regOpt.getName());
          }
        }
        // collect the values for the current option key
        i++;
        while (i < args.length && (i <= lastValIndex || !isKey(args, i))) {
          values.add(args[i]);
          i++;
        }

        String[] valueArray = (String[]) values.toArray(new String[0]);

        m_options.put(opt, valueArray);
      }
    }
  }