示例#1
0
  /** Initialize any program arguments */
  public Properties initArgs(String[] args) throws Exception {
    int size = args.length;

    int i = 0;

    Properties props = null;

    try {
      // apply arguments
      for (; i < size; i++) {
        if ("-?".equals(args[i]) || "-h".equals(args[i])) {
          // print usage and exit
          usage(props);
        }
        // debug
        else if ("-d".equals(args[i])) {
          GoCoin.DEBUG = true;
        }
        // verbose
        else if ("-v".equals(args[i])) {
          GoCoin.DEBUG = true;
          GoCoin.VERBOSE = true;
        }
        // token
        else if ("--token-file".equals(args[i])) {
          this.tokenFile = args[++i];
        } else if (args[i].startsWith("--")) {
          String arg = args[i].substring(2);
          if (apiMethods.contains(arg)) {
            apiMethod = arg;
            if (API_METHOD_VERSION.equals(arg)) {
              // no params needed
            } else if (API_METHOD_GET_EXCHANGE.equals(arg)) {
              // no params needed
            } else if (API_METHOD_GET_TOKEN.equals(arg)) {
              String json = getFileContents(args[++i]);
              AuthCode ac = new AuthCode(json);
              // potentially use the default client id and secret from the properties file
              if (this.authcode != null) {
                if (!GoCoin.hasValue(ac.getClientId())) {
                  ac.setClientId(this.authcode.getClientId());
                }
                if (!GoCoin.hasValue(ac.getClientSecret())) {
                  ac.setClientSecret(this.authcode.getClientSecret());
                }
              }
              params.put("authcode", ac);
            } else if (API_METHOD_GET_ACCOUNTS.equals(arg)) {
              params.put("merchantId", args[++i]);
            } else if (API_METHOD_GET_MERCHANT.equals(arg)) {
              params.put("merchantId", args[++i]);
            } else if (API_METHOD_GET_MERCHANT_USERS.equals(arg)) {
              params.put("merchantId", args[++i]);
            } else if (API_METHOD_GET_USER.equals(arg)) {
              params.put("userId", args[++i]);
            } else if (API_METHOD_GET_USER_APPS.equals(arg)) {
              params.put("userId", args[++i]);
            } else if (API_METHOD_GET_INVOICE.equals(arg)) {
              params.put("invoiceId", args[++i]);
            } else if (API_METHOD_CREATE_INVOICE.equals(arg)) {
              params.put("merchantId", args[++i]);
              String json = getFileContents(args[++i]);
              params.put("invoice", new Invoice(json));
            } else if (API_METHOD_SEARCH_INVOICES.equals(arg)) {
              SearchCriteria criteria = new SearchCriteria();
              if (i + 1 != size) {
                int j = i + 1;
                while (j < size && args[j].startsWith("-") && searchOptions.contains(args[j])) {
                  String option = args[j];
                  String value = args[++j];
                  // System.out.println("FOUND SEARCH OPTION ["+option+"]=["+value+"]");
                  if ("-merchant".equalsIgnoreCase(option)) {
                    criteria.setMerchantId(value);
                  }
                  if ("-status".equalsIgnoreCase(option)) {
                    criteria.setStatus(value);
                  }
                  if ("-start".equalsIgnoreCase(option)) {
                    criteria.setStartTs(value);
                  }
                  if ("-end".equalsIgnoreCase(option)) {
                    criteria.setEndTs(value);
                  }
                  if ("-page".equalsIgnoreCase(option)) {
                    criteria.setPageNumber(value);
                  }
                  if ("-per".equalsIgnoreCase(option)) {
                    criteria.setPerPage(value);
                  }
                  j++;
                }
                // put our index to i since we've iterated through it
                i = j;
              }
              params.put("criteria", criteria);
            }
          } else {
            if (GoCoin.VERBOSE) {
              System.out.println("[WARNING]: Unknown api method [" + arg + "]");
            }
          }
        }
        // unknown, abort
        else {
          System.err.println("Unknown parameter [" + args[i] + "]");
          // print usage and exit
          usage(props);
        }
      }
    } catch (ArrayIndexOutOfBoundsException aiobe) {
      System.err.printf("\n" + "Invalid program argument %s", args[i - 1]);
      usage(props);
    }

    if (props == null) {
      props = getDefaultProperties();
    }

    return props;
  }