void ControlCommandLine::parse(const CommandLineArgs *cmdArgs)
{
  CommandLineFormat fmt[] = {
    { PASSWORD_FILE, NEEDS_ARG },
    { CONFIG_RELOAD, NO_ARG },
    { DISCONNECT_ALL, NO_ARG },
    { CONNECT, NEEDS_ARG },
    { SHUTDOWN, NO_ARG },
    { SET_PRIMARY_VNC_PASSWORD, NEEDS_ARG },
    { SET_CONTROL_PASSWORD, NEEDS_ARG },
    { CHECK_SERVICE_PASSWORDS, NO_ARG },
    { SHARE_PRIMARY, NO_ARG },
    { SHARE_RECT, NEEDS_ARG },
    { SHARE_DISPLAY, NEEDS_ARG },
    { SHARE_WINDOW, NEEDS_ARG },
    { SHARE_FULL, NO_ARG },
    { CONTROL_SERVICE, NO_ARG },
    { CONTROL_APPLICATION, NO_ARG },
    { CONFIG_APPLICATION, NO_ARG },
    { CONFIG_SERVICE, NO_ARG },
    { SLAVE_MODE, NO_ARG },
    { DONT_ELEVATE, NO_ARG }
  };

  if (!CommandLine::parse(fmt, sizeof(fmt) / sizeof(CommandLineFormat), cmdArgs)) {
    throw CommandLineFormatException();
  }

  if (hasConfigServiceFlag() && ((int)m_foundKeys.size() > (optionSpecified(DONT_ELEVATE) ? 2 : 1))) {
    throw CommandLineFormatException();
  }

  if (hasConfigAppFlag() && m_foundKeys.size() > 1) {
    throw CommandLineFormatException();
  }

  bool hasPassFile = hasPasswordFile();
  if (hasPassFile) {
    optionSpecified(PASSWORD_FILE, &m_passwordFile);
  }

  if (hasShareRect()) {
    StringStorage strRect;
    optionSpecified(SHARE_RECT, &strRect);
    parseRectCoordinates(&strRect);
  }

  if (hasShareDisplay()) {
    StringStorage strDisplayNumber;
    optionSpecified(SHARE_DISPLAY, &strDisplayNumber);
    parseDisplayNumber(&strDisplayNumber);
  }

  if (hasShareWindow()) {
    optionSpecified(SHARE_WINDOW, &m_windowHeaderName);
  }

  if (hasKillAllFlag() && hasReloadFlag()) {
    throw CommandLineFormatException();
  }

  if (hasConnectFlag()) {
    optionSpecified(CONNECT, &m_connectHostName);
  }

  if ((hasSetVncPasswordFlag() || hasSetControlPasswordFlag()) && m_foundKeys.size() > 1) {
    throw CommandLineFormatException();
  } else {
    optionSpecified(SET_CONTROL_PASSWORD, &m_controlPassword);
    optionSpecified(SET_PRIMARY_VNC_PASSWORD, &m_vncPassword);
  }

  if ((hasControlServiceFlag() || hasControlAppFlag()) && (isSlave()) && (m_foundKeys.size() > 2)) {
    throw CommandLineFormatException();
  }

  bool hasNotSlaveControl = (hasControlServiceFlag() || hasControlAppFlag()) && !isSlave();
  if ((hasNotSlaveControl && !hasPassFile && m_foundKeys.size() > 2) ||
      (hasNotSlaveControl && hasPassFile && m_foundKeys.size() != 3)) {
    throw CommandLineFormatException();
  }

  if (m_foundKeys.size() == 0) {
    throw CommandLineFormatException();
  }
}
void ControlCommandLine::parse(const TCHAR *commandLine)
{
  CommandLineFormat fmt[] = {
    { PASSWORD_FILE, NEEDS_ARG },
    { CONFIG_RELOAD, NO_ARG },
    { DISCONNECT_ALL, NO_ARG },
    { CONNECT, NEEDS_ARG },
    { SHUTDOWN, NO_ARG },
    { SET_PRIMARY_VNC_PASSWORD, NEEDS_ARG },
    { SET_CONTROL_PASSWORD, NEEDS_ARG },
    { CONTROL_SERVICE, NO_ARG },
    { CONTROL_APPLICATION, NO_ARG },
    { CONFIG_APPLICATION, NO_ARG },
    { CONFIG_SERVICE, NO_ARG },
    { SLAVE_MODE, NO_ARG },
    { DONT_ELEVATE, NO_ARG },
    { PORTABLE, NO_ARG },
    { SET_VNC_PORT, NEEDS_ARG },
    { VNC_INI_DIRECTORY, NEEDS_ARG },

  };

  if (!CommandLine::parse(fmt, sizeof(fmt) / sizeof(CommandLineFormat), commandLine)) {
    throw CommandLineFormatException();
  }

  if (hasConfigServiceFlag() && ((int)m_foundKeys.size() > (optionSpecified(DONT_ELEVATE) ? 2 : 1))) {
    throw CommandLineFormatException();
  }

  if ( ( hasConfigAppFlag()  && ! hasPortableFlag() && m_foundKeys.size() > 1  ) ||
       ( hasConfigAppFlag()  &&  hasPortableFlag()  && m_foundKeys.size() > 2 ) ) {
    throw CommandLineFormatException();
  }

  bool hasPassFile = hasPasswordFile();
  if (hasPassFile) {
    optionSpecified(PASSWORD_FILE, &m_passwordFile);
  }

  if (hasKillAllFlag() && hasReloadFlag()) {
    throw CommandLineFormatException();
  }

  if (hasConnectFlag()) {
    optionSpecified(CONNECT, &m_connectHostName);
  }

  if(hasSetVncPortAndPrimaryPasswdFlag()) {
      setVncPortAndPrimaryPassword();
  }

  if ((hasControlServiceFlag() || hasControlAppFlag()) && (isSlave()) && (m_foundKeys.size() > 2)) {
    throw CommandLineFormatException();
  }

  bool hasNotSlaveControl = (hasControlServiceFlag() || hasControlAppFlag()) && !isSlave();
  if ((hasNotSlaveControl && !hasPassFile && m_foundKeys.size() > 2) ||
      (hasNotSlaveControl && hasPassFile && m_foundKeys.size() != 3)) {
    throw CommandLineFormatException();
  }

  if(hasSetVncPortFlag() && ! hasSetVncPasswordFlag()) {
      setVncPort();
  }

  if(! hasSetVncPortFlag() && ( hasSetVncPasswordFlag() || hasSetControlPasswordFlag()) ) {
      setVncOrControlPassword();
  }

  if (m_foundKeys.size() == 0) {
    throw CommandLineFormatException();
  }
}