private void readArgs(String[] args) throws Exception {

    args = new GenericOptionsParser(getConf(), args).getRemainingArgs();

    Options options = new Options();
    options.addOption(new Option("confLocation", true, "Location of HCat/Hive Server's hive-site."));
    options.addOption(new Option("delete", false, "Delete delegation token."));
    options.addOption(new Option("list", false, "List delegation tokens."));
    options.addOption(new Option("olderThan", true, "Filter for token's issue-date. (e.g. 3d, 1h or 4m)."));
    options.addOption(new Option("expired", false, "Select expired delegation tokens for listing/deletion."));
    options.addOption(new Option("dryRun", false, "Don't actually delete delegation tokens."));
    options.addOption(new Option("batchSize", true, "Number of tokens to drop between sleep intervals."));
    options.addOption(new Option("sleepTime", true, "Sleep-time in seconds, between batches of dropped delegation tokens."));
    options.addOption(new Option("serverMode", true, "The service from which to read delegation tokens. Should be either of [METASTORE, HIVESERVER2]."));

    CommandLine commandLine = new GnuParser().parse(options,
        args,
        false // Stop on non-existent option.
    );

    if (commandLine.hasOption("confLocation")) {
      confLocation = commandLine.getOptionValue("confLocation");
    }

    if (commandLine.hasOption("list")) {
      opType = OpType.LIST;
    }
    else
    if (commandLine.hasOption("delete")) {
      opType = OpType.DELETE;
    }
    else {
      throw new IllegalArgumentException("Operation must be delete, list or get!");
    }

    isDryRun = (commandLine.hasOption("dryRun"));

    if (commandLine.hasOption("expired")) {
      LOG.info("Working on expired delegation tokens!");
      timeLimitMillis = System.currentTimeMillis();
      selectForDeletion = new Predicate<DelegationTokenIdentifier>() {
        public boolean apply(DelegationTokenIdentifier input) {
          return timeLimitMillis > input.getMaxDate();
        }
      };
    }
    else
    if (commandLine.hasOption("olderThan")) {

      String olderThanLimitString = commandLine.getOptionValue("olderThan");
      switch (olderThanLimitString.charAt(olderThanLimitString.length()-1)) {
        case 'd':
        case 'D':
          timeLimitMillis = System.currentTimeMillis() - 24*60*60*1000*Integer.parseInt(olderThanLimitString.substring(0, olderThanLimitString.length()-1));
          break;
        case 'h':
        case 'H':
          timeLimitMillis = System.currentTimeMillis() - 60*60*1000*Integer.parseInt(olderThanLimitString.substring(0, olderThanLimitString.length()-1));
          break;
        case 'm':
        case 'M':
          timeLimitMillis = System.currentTimeMillis() - 60*1000*Integer.parseInt(olderThanLimitString.substring(0, olderThanLimitString.length()-1));
          break;
        default:
          throw new IllegalArgumentException("Unsupported time-limit: " + olderThanLimitString);
      }

      LOG.info("Working on delegation tokens older than current-time (" + timeLimitMillis + ").");
      selectForDeletion = new Predicate<DelegationTokenIdentifier>() {
        public boolean apply(DelegationTokenIdentifier input) {
          return timeLimitMillis > input.getIssueDate();
        }
      };

    }
    else {
      // Neither "expired" nor "olderThan" criteria selected. This better not be an attempt to delete tokens.
      if (opType == OpType.DELETE) {
        throw new IllegalArgumentException("Attempting to delete tokens. " +
            "Specify deletion criteria (either expired or time-range).");
      }
    }

    if (commandLine.hasOption("batchSize")) {
      String batchSizeString = commandLine.getOptionValue("batchSize");
      batchSize = Integer.parseInt(batchSizeString);
      if (batchSize < 1) {
        LOG.warn("Invalid batch-size! (" + batchSize + ") Resetting to defaults.");
        batchSize = BATCH_SIZE_DEFAULT;
      }
      LOG.info("Batch-size for drop == " + batchSize);
    }

    if (commandLine.hasOption("sleepTime")) {
      String sleepTimeString = commandLine.getOptionValue("sleepTime");
      sleepTimeMillis = 1000 * Integer.parseInt(sleepTimeString);
      if (sleepTimeMillis <= 0) {
        LOG.warn("Invalid sleep-time! (" + sleepTimeMillis + ") Resetting to defaults.");
        sleepTimeMillis = SLEEP_TIME_MILLIS_DEFAULT;
      }
      LOG.info("Sleep between drop-batches: " + sleepTimeMillis + " milliseconds.");
    }

    if (commandLine.hasOption("serverMode")) {
      String serverModeString = commandLine.getOptionValue("serverMode").toLowerCase();
      switch(serverModeString) {
        case "metastore":
          serverMode = HadoopThriftAuthBridge.Server.ServerMode.METASTORE; break;
        case "hiveserver2":
          serverMode = HadoopThriftAuthBridge.Server.ServerMode.HIVESERVER2; break;
        default:
          throw new IllegalArgumentException("Invalid value for for serverMode (" + serverModeString + ")" +
              "Should be either \"METASTORE\", or \"HIVESERVER2\"");
      }
    }
    LOG.info("Running with serverMode == " + serverMode);

  }
Tool to twiddle MetaStore delegation tokens.
standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/security/DelegationTokenTool.java