Пример #1
0
  /**
   * Parse the arguments according to the specified options and properties.
   *
   * @param options the specified Options
   * @param arguments the command line arguments
   * @param properties command line option name-value pairs
   * @param stopAtNonOption if <tt>true</tt> an unrecognized argument stops the parsing and the
   *     remaining arguments are added to the {@link CommandLine}s args list. If <tt>false</tt> an
   *     unrecognized argument triggers a ParseException.
   * @return the list of atomic option and value tokens
   * @throws ParseException if there are any problems encountered while parsing the command line
   *     tokens.
   */
  public CommandLine parse(
      Options options, String[] arguments, Properties properties, boolean stopAtNonOption)
      throws ParseException {
    this.options = options;
    this.stopAtNonOption = stopAtNonOption;
    skipParsing = false;
    currentOption = null;
    expectedOpts = new ArrayList(options.getRequiredOptions());

    // clear the data from the groups
    for (OptionGroup group : options.getOptionGroups()) {
      group.setSelected(null);
    }

    cmd = new CommandLine();

    if (arguments != null) {
      for (String argument : arguments) {
        handleToken(argument);
      }
    }

    // check the arguments of the last option
    checkRequiredArgs();

    // add the default options
    handleProperties(properties);

    checkRequiredOptions();

    return cmd;
  }
Пример #2
0
  /**
   * Removes the option or its group from the list of expected elements.
   *
   * @param option
   */
  private void updateRequiredOptions(Option option) throws AlreadySelectedException {
    if (option.isRequired()) {
      expectedOpts.remove(option.getKey());
    }

    // if the option is in an OptionGroup make that option the selected option of the group
    if (options.getOptionGroup(option) != null) {
      OptionGroup group = options.getOptionGroup(option);

      if (group.isRequired()) {
        expectedOpts.remove(group);
      }

      group.setSelected(option);
    }
  }