Exemplo n.º 1
0
  public CommandLine(List<String> args, AbstractShellScript ss)
      throws UnsupportedOptionException, InvalidOptionValueException {
    for (int i = 0; i < args.size(); ++i) {
      String arg = args.get(i);

      // if this is a specified option that says that the next element is
      // NOT an option
      // like in "rm -- -f" if you wqnt to erase a file named "-f"
      if (arg.equals("--")) {
        // adds the next positional parameter as an argument
        CommandLineElement parm = new CommandLineElement();
        parm.setText(args.get(++i));
        getPositionalParameters().add(parm);
      } else {
        // this is an option
        if (arg.startsWith("-")) {
          boolean isLong = arg.startsWith("--");
          int pos = arg.indexOf('=');
          String name = isLong ? arg.substring(0, pos > 0 ? pos : arg.length()) : arg;
          OptionSpecification spec = ss.getOptionSpecification(name);

          // no specifications exist for this parameter, which means
          // that the option does not exist
          if (spec == null) {
            throw new UnsupportedOptionException(
                "option \"" + arg + "\" is not supported. Please use \"--help\"");
          } else {
            Option option = new Option();
            option.setSpecification(spec);
            option.setSpecified(true);

            // if the option requires a value
            if (spec.getValueRegexp() != null) {
              if (isLong) {
                if (pos < 0) {
                  throw new InvalidOptionValueException("empty value for option " + name);
                } else {
                  option.setValue(arg.substring(pos + 1));
                }
              } else {
                option.setValue(args.get(++i));
              }
            }

            getPositionalParameters().add(option);
          }
        }
        // this argument is not an option, it's hence a parameter
        else {
          CommandLineElement parm = new CommandLineElement();
          parm.setText(arg);
          getPositionalParameters().add(parm);
        }
      }
    }
  }
Exemplo n.º 2
0
  public List<String> findArguments() {
    List<String> args = new ArrayList<String>();

    for (CommandLineElement p : getPositionalParameters()) {
      if (!(p instanceof Option)) {
        args.add(p.getText());
      }
    }

    return Collections.unmodifiableList(args);
  }