private static void iterateCommands(CommandManager mgr) throws IOException {
    Map<String, CommandTarget> commandMap = mgr.getCommands();
    StringBuilder serviceString = new StringBuilder("include \"");
    serviceString.append(MGMT_STRUCT_THRIFT_FILE).append("\"\n");
    serviceString.append("include \"");
    serviceString.append(MGMT_CMDRESULT_THRIFT_FILE).append("\"\n");
    serviceString.append("namespace java ").append(MGMT_SERVICE_PACKAGE).append("\n");
    serviceString.append(EXCEPTION_DECLARATION);
    serviceString.append("\nservice GeodeCommandService {\n");

    StringBuilder structString = new StringBuilder();
    structString.append("namespace java ").append(MGMT_STRUCT_PACKAGE).append("\n");

    int size = commandMap.size();
    for (String command : commandMap.keySet()) {
      CommandTarget tgr = commandMap.get(command);
      boolean argsStructRequired = argsStructRequired(tgr);
      boolean commandSupported = commandSupported(tgr);
      if (commandSupported) {
        if (argsStructRequired) {
          boolean commandProcessed = processCommand(tgr, structString);
          if (commandProcessed) {
            serviceString
                .append("\n\tcommandResult.CommandResult ")
                .append(getProperName(tgr.getCommandName(), METHOD_NAME));
            serviceString
                .append("(")
                .append("1:structs.")
                .append(getProperName(tgr.getCommandName(), STRUCT_NAME));
            serviceString.append(" arguments)");
            serviceString.append(EXCEPTION_METHOD_SIGNATURE);
            if (size > 1) serviceString.append(",");
          }
        } else {
          serviceString
              .append("\n\tcommandResult.CommandResult ")
              .append(getProperName(tgr.getCommandName(), METHOD_NAME));
          serviceString.append("()");
          serviceString.append(EXCEPTION_METHOD_SIGNATURE);
          if (size > 1) serviceString.append(",");
        }
      }
      size--;
    }
    serviceString.append("\n}");
    writeToFile(structString, MGMT_STRUCT_THRIFT_FILE);
    writeToFile(serviceString, MGMT_SERVICE_THRIFT_FILE);
  }
 /**
  * Some commands can not be supported over wire but only work thorugh shell. Add them here.
  *
  * @param tgr
  * @return
  */
 private static boolean commandSupported(CommandTarget tgr) {
   if (tgr.getCommandName().contains("connect")
       || tgr.getCommandName().startsWith("start")
       || tgr.getCommandName().startsWith("exit")
       || tgr.getCommandName().startsWith("disconnect")
       || tgr.getCommandName().startsWith("debug")
       || tgr.getCommandName()
           .startsWith("deploy") // deploy need byte[] manual changes. will be supported later
       || tgr.getCommandName().startsWith("describe client")
       || tgr.getCommandName().startsWith("echo")
       || tgr.getCommandName().startsWith("import") // import command have file structures again
       || tgr.getCommandName().equals("sh")
       || tgr.getCommandName().equals("sleep")) return false;
   else return true;
 }
  private static boolean processCommand(CommandTarget commandTarget, StringBuilder structFile) {

    StringBuilder structString = new StringBuilder();
    try {
      int paramNumber = 1;
      String command = commandTarget.getCommandName();
      List<Argument> arguments = commandTarget.getOptionParser().getArguments();
      List<Option> options = commandTarget.getOptionParser().getOptions();

      structString.append("\nstruct " + getProperName(command, STRUCT_NAME) + " {\n");

      // This is to ensure at least one non-optional fields
      structString
          .append("\t")
          .append(paramNumber++)
          .append(":")
          .append("i16 argsId=")
          .append(argsCounter.incrementAndGet())
          .append(",\n");

      for (int i = 0; i < arguments.size(); i++) {
        Argument argument = arguments.get(i);
        structString
            .append("\t")
            .append(paramNumber++)
            .append(":")
            .append(" string ")
            .append(getProperName(argument.getArgumentName(), STRUCT_FIELD_NAME));
        if (options.size() > 0) structString.append(",\n");
        else structString.append("\n");
      }

      for (int i = 0; i < options.size(); i++) {
        Option option = options.get(i);
        String thriftType = getThriftDataType(option.getDataType());
        String thriftName = getProperName(option.getLongOption(), STRUCT_FIELD_NAME);
        structString
            .append("\t")
            .append(paramNumber++)
            .append(":")
            .append((option.isRequired() ? "" : "optional "))
            .append(thriftType)
            .append(" ")
            .append(thriftName);

        String defaultValue = option.getUnspecifiedDefaultValue();
        if (defaultValue != null && isNumericStringOrBoolean(option.getDataType())) {
          // Check for numeric or string and boolean dont allow collections
          structString
              .append(" = ")
              .append(quoteStringOptionValue(defaultValue, option.getDataType()));
        }

        if (i < options.size() - 1) structString.append(",\n");
        else structString.append("\n");

        allKindsType.add(option.getDataType().getSimpleName());
      }
      structString.append("\n}\n");
      structFile.append(structString.toString());
      return true;
    } catch (RuntimeException e) {
      System.out.println(
          "Command " + commandTarget.getCommandName() + " not supported bcoz " + e.getMessage());
      e.printStackTrace();
      return false;
    }
  }
 private static boolean argsStructRequired(CommandTarget tgr) {
   if ((tgr.getOptionParser().getArguments().size() + tgr.getOptionParser().getOptions().size())
       > 0) return true;
   else return false;
 }