Example #1
0
  private void loadArgs(String[] args) throws ParameterProblem {

    logger.debug("Parsing command line arguments");
    final CommandLineParser parser = new PosixParser();

    final Opts opts = new Opts();
    final CommandLine line;
    try {
      line = parser.parse(opts.getOptions(), args);
    } catch (ParseException e) {
      throw new ParameterProblem(e.getMessage(), e);
    }

    // figure action first
    AdminAction theAction = null;
    for (AdminAction a : AdminAction.values()) {
      if (line.hasOption(a.option())) {
        if (theAction == null) {
          theAction = a;
        } else {
          throw new ParameterProblem("You may only specify a single action");
        }
      }
    }

    if (theAction == null) {
      throw new ParameterProblem("You must specify an action");
    }

    this.action = theAction;
    logger.debug("Action: " + theAction);

    // short circuit for --help arg
    if (theAction == AdminAction.Help) {
      return;
    }

    // then action-specific arguments
    if (theAction == AdminAction.AddNodes || theAction == AdminAction.UpdateNodes) {
      this.hosts = parseHosts(line.getOptionValue(theAction.option()));

      if (line.hasOption(Opts.MEMORY)) {
        final String memString = line.getOptionValue(Opts.MEMORY);
        if (memString == null || memString.trim().length() == 0) {
          throw new ParameterProblem("Node memory value is empty");
        }
        this.nodeMemory = parseMemory(memString);
        this.nodeMemoryConfigured = true;
      }

      if (line.hasOption(Opts.NETWORKS)) {
        this.nodeNetworks = line.getOptionValue(Opts.NETWORKS);
      }

      if (line.hasOption(Opts.POOL)) {
        String pool = line.getOptionValue(Opts.POOL);
        if (pool == null || pool.trim().length() == 0) {
          throw new ParameterProblem("Node pool value is empty");
        }
        this.nodePool = pool.trim();
      }

      final boolean active = line.hasOption(Opts.ACTIVE);
      final boolean inactive = line.hasOption(Opts.INACTIVE);

      if (active && inactive) {
        throw new ParameterProblem(
            "You cannot specify both " + Opts.ACTIVE_LONG + " and " + Opts.INACTIVE_LONG);
      }

      if (active) {
        this.nodeActiveConfigured = true;
      }
      if (inactive) {
        this.nodeActive = false;
        this.nodeActiveConfigured = true;
      }

    } else if (theAction == AdminAction.RemoveNodes) {
      this.hosts = parseHosts(line.getOptionValue(theAction.option()));
    } else if (theAction == AdminAction.ListNodes) {
      final String hostArg = line.getOptionValue(AdminAction.ListNodes.option());
      if (hostArg != null) {
        this.hosts = parseHosts(hostArg);
      }
    } else if (theAction == AdminAction.PoolAvailability) {
      if (line.hasOption(Opts.POOL)) {
        final String pool = line.getOptionValue(Opts.POOL);
        if (pool == null || pool.trim().length() == 0) {
          throw new ParameterProblem("Pool name value is empty");
        }
        this.nodePool = pool;
      }
      if (line.hasOption(Opts.FREE)) {
        this.inUse = RemoteNodeManagement.FREE_ENTRIES;
      }
      if (line.hasOption(Opts.USED)) {
        this.inUse = RemoteNodeManagement.USED_ENTRIES;
      }
    }

    // finally everything else
    if (!line.hasOption(Opts.CONFIG)) {
      throw new ParameterProblem(Opts.CONFIG_LONG + " option is required");
    }
    String config = line.getOptionValue(Opts.CONFIG);
    if (config == null || config.trim().length() == 0) {
      throw new ParameterProblem("Config file path is invalid");
    }
    super.configPath = config.trim();

    final boolean batchMode = line.hasOption(Opts.BATCH);
    final boolean json = line.hasOption(Opts.JSON);

    final Reporter.OutputMode mode;
    if (batchMode && json) {
      throw new ParameterProblem(
          "You cannot specify both " + Opts.BATCH_LONG + " and " + Opts.JSON_LONG);
    } else if (batchMode) {
      mode = Reporter.OutputMode.Batch;
    } else if (json) {
      mode = Reporter.OutputMode.Json;
    } else {
      mode = Reporter.OutputMode.Friendly;
    }

    final String[] fields;
    if (line.hasOption(Opts.REPORT)) {
      fields = parseFields(line.getOptionValue(Opts.REPORT), theAction);
    } else {
      fields = theAction.fields();
    }

    String delimiter = null;
    if (line.hasOption(Opts.DELIMITER)) {
      delimiter = line.getOptionValue(Opts.DELIMITER);
    }

    this.reporter = new Reporter(mode, fields, delimiter);

    if (line.hasOption(Opts.OUTPUT)) {
      final String filename = line.getOptionValue(Opts.OUTPUT);
      final File f = new File(filename);
      try {
        this.outStream = new FileOutputStream(f);
      } catch (FileNotFoundException e) {
        throw new ParameterProblem(
            "Specified output file could not be opened for writing: " + f.getAbsolutePath(), e);
      }
    } else {
      this.outStream = System.out;
    }

    final List leftovers = line.getArgList();
    if (leftovers != null && !leftovers.isEmpty()) {
      throw new ParameterProblem(
          "There are unrecognized arguments, check -h to make "
              + "sure you are doing the intended thing: "
              + leftovers.toString());
    }
  }