コード例 #1
0
void CommandLineUtility::parseArguments(QStringList& arguments)
{
    QStringListIterator iter(arguments);
    //QTextStream out(stdout);
    //QTextStream errorsteam(stderr);

    while (iter.hasNext() && !encounteredError)
    {
        QString temp = iter.next();
        if (helpRegexp.exactMatch(temp))
        {
            helpRequest = true;
        }
        else if (versionRegexp.exactMatch(temp))
        {
            versionRequest = true;
        }
        else if (trayRegexp.exactMatch(temp))
        {
            launchInTray = true;
            hideTrayIcon = false;
        }
        else if (noTrayRegexp.exactMatch(temp))
        {
            hideTrayIcon = true;
            launchInTray = false;
        }
        else if (loadProfileRegexp.exactMatch(temp))
        {
            if (iter.hasNext())
            {
                temp = iter.next();
                QFileInfo fileInfo(temp);
                if (fileInfo.exists())
                {
                    if (fileInfo.suffix() != "amgp" && fileInfo.suffix() != "xml")
                    {
                        setErrorMessage(tr("Profile location %1 is not an XML file.").arg(temp));
                        //errorsteam << tr("Profile location %1 is not an XML file.").arg(temp) << endl;
                        //encounteredError = true;
                    }
                    else
                    {
                        profileLocation = fileInfo.absoluteFilePath();
                    }
                }
                else
                {
                    setErrorMessage(tr("Profile location %1 does not exist.").arg(temp));
                    //errorsteam << tr("Profile location %1 does not exist.").arg(temp) << endl;
                    //encounteredError = true;
                }
            }
        }
        else if (loadProfileForControllerRegexp.exactMatch(temp))
        {
            if (iter.hasNext())
            {
                temp = iter.next();

                bool validNumber = false;
                int tempNumber = temp.toInt(&validNumber);
                if (validNumber)
                {
                    controllerNumber = tempNumber;
                }
                else if (!temp.isEmpty())
                {
                    controllerIDString = temp;
                }
                else
                {
                    setErrorMessage(tr("Controller identifier is not a valid value."));
                    //errorsteam << tr("Controller identifier is not a valid value.") << endl;
                    //encounteredError = true;
                }
            }
        }
        else if (hiddenRegexp.exactMatch(temp))
        {
            hiddenRequest = true;
        }
        else if (unloadRegexp.exactMatch(temp))
        {
            unloadProfile = true;
            profileLocation = "";

            if (iter.hasNext())
            {
                temp = iter.next();

                if (!isPossibleCommand(temp))
                {
                    // A value has been passed. Attempt
                    // to validate the value.
                    bool validNumber = false;
                    int tempNumber = temp.toInt(&validNumber);
                    if (validNumber)
                    {
                        controllerNumber = tempNumber;
                    }
                    else if (!temp.isEmpty())
                    {
                        controllerIDString = temp;
                    }
                    else
                    {
                        setErrorMessage(tr("Controller identifier is not a valid value."));
                        //errorsteam << tr("Controller identifier is not a valid value.") << endl;
                        //encounteredError = true;
                    }
                }
                else
                {
                    // Grabbed a possible command-line option.
                    // Move iterator back to previous item.
                    iter.previous();
                }
            }
        }
        else if (startSetRegexp.exactMatch(temp))
        {
            if (iter.hasNext())
            {
                temp = iter.next();

                bool validNumber = false;
                int tempNumber = temp.toInt(&validNumber);
                if (validNumber && tempNumber >= 1 && tempNumber <= 8)
                {
                    startSetNumber = tempNumber;
                }
                else if (validNumber)
                {
                    setErrorMessage(tr("An invalid set number was specified."));
                    //errorsteam << tr("An invalid set number was specified.") << endl;
                    //encounteredError = true;
                }

                if (iter.hasNext())
                {
                    temp = iter.next();

                    if (!isPossibleCommand(temp))
                    {
                        if (validNumber)
                        {
                            controllerNumber = tempNumber;
                        }
                        else if (!temp.isEmpty())
                        {
                            controllerIDString = temp;
                        }
                        else
                        {
                            setErrorMessage(tr("Controller identifier is not a valid value."));
                            //errorsteam << tr("Controller identifier is not a valid value.") << endl;
                            //encounteredError = true;
                        }
                    }
                    else
                    {
                        // Grabbed a possible command-line option.
                        // Move iterator back to previous item.
                        iter.previous();
                    }
                }
            }
            else
            {
                setErrorMessage(tr("No set number was specified."));
                //errorsteam << tr("No set number was specified.") << endl;
                //encounteredError = true;
            }
        }
#ifdef USE_SDL_2
        else if (gamepadListRegexp.exactMatch(temp))
        {
            listControllers = true;
        }
        else if (mappingRegexp.exactMatch(temp))
        {
            if (iter.hasNext())
            {
                temp = iter.next();

                bool validNumber = false;
                int tempNumber = temp.toInt(&validNumber);
                if (validNumber)
                {
                    controllerNumber = tempNumber;
                    mappingController = true;
                }
                else if (!temp.isEmpty())
                {
                    controllerIDString = temp;
                    mappingController = true;
                }
                else
                {
                    setErrorMessage(tr("Controller identifier is not a valid value."));
                    //errorsteam << tr("Controller identifier is not a valid value.") << endl;
                    //encounteredError = true;
                }
            }
            else
            {
                setErrorMessage(tr("No controller was specified."));
                //errorsteam << tr("No controller was specified.") << endl;
                //encounteredError = true;
            }
        }
#endif

#ifdef Q_OS_UNIX
        else if (daemonRegexp.exactMatch(temp))
        {
            daemonMode = true;
        }
  #ifdef WITH_X11
        else if (displayRegexp.exactMatch(temp))
        {
            if (iter.hasNext())
            {
                displayString = iter.next();
            }
            else
            {
                setErrorMessage(tr("No display string was specified."));
                //errorsteam << tr("No display string was specified.") << endl;
                //encounteredError = true;
            }
        }
  #endif
#endif

#if (defined (Q_OS_UNIX) && defined(WITH_UINPUT) && defined(WITH_XTEST)) \
     || (defined(Q_OS_WIN) && defined(WITH_VMULTI))
        else if (eventgenRegexp.exactMatch(temp))
        {
            if (iter.hasNext())
            {
                QString temp = iter.next();

                if (!eventGeneratorsList.contains(temp))
                {
                    eventGenerator = "";
                    setErrorMessage(tr("An invalid event generator was specified."));
                    //errorsteam << tr("An invalid event generator was specified.") << endl;
                    //encounteredError = true;
                }
                else
                {
                    eventGenerator = temp;
                }
            }
            else
            {
                setErrorMessage(tr("No event generator string was specified."));
                //errorsteam << tr("No event generator string was specified.") << endl;
                //encounteredError = true;
            }
        }
#endif

        else if (qtStyleRegexp.exactMatch(temp))
        {
            if (iter.hasNext())
            {
                // Skip over argument
                iter.next();
            }
            else
            {
                setErrorMessage(tr("Qt style flag was detected but no style was specified."));
                //errorsteam << tr("Qt style flag was detected but no style was specified.") << endl;
                //encounteredError = true;
            }
        }
        else if (logLevelRegexp.exactMatch(temp))
        {
            if (iter.hasNext())
            {
                QString temp = iter.next();
                if (temp == "debug")
                {
                    currentLogLevel = Logger::LOG_DEBUG;
                }
                else if (temp == "info")
                {
                    currentLogLevel = Logger::LOG_INFO;
                }
                /*else if (temp == "warn")
                {
                    currentLogLevel = Logger::LOG_WARNING;
                }
                else if (temp == "error")
                {
                    currentLogLevel = Logger::LOG_ERROR;
                }
                */
            }
            else
            {
                setErrorMessage(tr("No log level specified."));
                //errorsteam << tr("No log level specified.") << endl;
                //encounteredError = true;
            }

        }
        else if (isPossibleCommand(temp))
        {
            // Flag is unrecognized. Assume that it is a Qt option.
            if (iter.hasNext())
            {
                // Check next argument
                QString nextarg = iter.next();
                if (isPossibleCommand(nextarg))
                {
                    // Flag likely didn't take an argument. Move iterator
                    // back.
                    iter.previous();
                }
            }
        }
        // Check if this is the last argument. If it is and no command line flag
        // is active, the final argument is likely a profile that has
        // been specified.
        else if (!temp.isEmpty() && !iter.hasNext())
        {
            // If the file exists and it is an xml file, assume that it is a
            // profile.
            QFileInfo fileInfo(temp);
            if (fileInfo.exists())
            {
                if (fileInfo.suffix() != "amgp" && fileInfo.suffix() != "xml")
                {
                    setErrorMessage(tr("Profile location %1 is not an XML file.").arg(temp));
                    //errorsteam << tr("Profile location %1 is not an XML file.").arg(temp) << endl;
                    //encounteredError = true;
                }
                else
                {
                    profileLocation = fileInfo.absoluteFilePath();
                }
            }
            else
            {
                setErrorMessage(tr("Profile location %1 does not exist.").arg(temp));
                //errorsteam << tr("Profile location %1 does not exist.").arg(temp) << endl;
                //encounteredError = true;
            }
        }
    }
}
コード例 #2
0
void CommandLineUtility::parseArguments(QStringList& arguments)
{
    QStringListIterator iter(arguments);
    //QTextStream out(stdout);
    QTextStream errorsteam(stderr);

    while (iter.hasNext())
    {
        QString temp = iter.next();
        if (helpRegexp.exactMatch(temp))
        {
            helpRequest = true;
        }
        else if (versionRegexp.exactMatch(temp))
        {
            versionRequest = true;
        }
        else if (trayRegexp.exactMatch(temp))
        {
            launchInTray = true;
            hideTrayIcon = false;
        }
        else if (noTrayRegexp.exactMatch(temp))
        {
            hideTrayIcon = true;
            launchInTray = false;
        }
        else if (loadProfileRegexp.exactMatch(temp))
        {
            if (iter.hasNext())
            {
                temp = iter.next();
                QFileInfo fileInfo(temp);
                if (fileInfo.exists())
                {
                    if (fileInfo.suffix() != "amgp" && fileInfo.suffix() != "xml")
                    {
                        errorsteam << tr("Profile location %1 is not an XML file.").arg(temp) << endl;
                        encounteredError = true;
                    }
                    else
                    {
                        profileLocation = fileInfo.absoluteFilePath();
                    }
                }
                else
                {
                    errorsteam << tr("Profile location %1 does not exist.").arg(temp) << endl;
                    encounteredError = true;
                }
            }
        }
        else if (loadProfileForControllerRegexp.exactMatch(temp))
        {
            if (iter.hasNext())
            {
                temp = iter.next();

                bool validNumber = false;
                int tempNumber = temp.toInt(&validNumber);
                if (validNumber)
                {
                    controllerNumber = tempNumber;
                }
                else if (!temp.isEmpty())
                {
                    controllerIDString = temp;
                }
                else
                {
                    errorsteam << tr("Controller identifier is not a valid value.") << endl;
                    encounteredError = true;
                }
            }
        }
        else if (hiddenRegexp.exactMatch(temp))
        {
            hiddenRequest = true;
        }
        else if (unloadRegexp.exactMatch(temp))
        {
            unloadProfile = true;
            profileLocation = "";

            if (iter.hasNext())
            {
                temp = iter.next();

                if (!isPossibleCommand(temp))
                {
                    // A value has been passed. Attempt
                    // to validate the value.
                    bool validNumber = false;
                    int tempNumber = temp.toInt(&validNumber);
                    if (validNumber)
                    {
                        controllerNumber = tempNumber;
                    }
                    else if (!temp.isEmpty())
                    {
                        controllerIDString = temp;
                    }
                    else
                    {
                        errorsteam << tr("Controller identifier is not a valid value.") << endl;
                        encounteredError = true;
                    }
                }
                else
                {
                    // Grabbed a possible command-line option.
                    // Move iterator back to previous item.
                    iter.previous();
                }
            }
        }
        else if (startSetRegexp.exactMatch(temp))
        {
            if (iter.hasNext())
            {
                temp = iter.next();

                bool validNumber = false;
                int tempNumber = temp.toInt(&validNumber);
                if (validNumber && tempNumber >= 1 && tempNumber <= 8)
                {
                    startSetNumber = tempNumber;
                }
                else if (validNumber)
                {
                    errorsteam << tr("An invalid set number was specified.") << endl;
                    encounteredError = true;
                }

                if (iter.hasNext())
                {
                    temp = iter.next();

                    if (!isPossibleCommand(temp))
                    {
                        if (validNumber)
                        {
                            controllerNumber = tempNumber;
                        }
                        else if (!temp.isEmpty())
                        {
                            controllerIDString = temp;
                        }
                        else
                        {
                            errorsteam << tr("Controller identifier is not a valid value.") << endl;
                            encounteredError = true;
                        }
                    }
                    else
                    {
                        // Grabbed a possible command-line option.
                        // Move iterator back to previous item.
                        iter.previous();
                    }
                }
            }
            else
            {
                errorsteam << tr("No set number was specified.") << endl;
                encounteredError = true;
            }
        }
#ifdef USE_SDL_2
        else if (gamepadListRegexp.exactMatch(temp))
        {
            listControllers = true;
        }
        else if (mappingRegexp.exactMatch(temp))
        {
            if (iter.hasNext())
            {
                temp = iter.next();

                bool validNumber = false;
                int tempNumber = temp.toInt(&validNumber);
                if (validNumber)
                {
                    controllerNumber = tempNumber;
                    mappingController = true;
                }
                else if (!temp.isEmpty())
                {
                    controllerIDString = temp;
                    mappingController = true;
                }
                else
                {
                    errorsteam << tr("Controller identifier is not a valid value.") << endl;
                    encounteredError = true;
                }
            }
            else
            {
                errorsteam << tr("No controller was specified.") << endl;
                encounteredError = true;
            }
        }
#endif

#ifdef Q_OS_UNIX
        else if (daemonRegexp.exactMatch(temp))
        {
            daemonMode = true;
        }
    #ifdef WITH_X11
        else if (displayRegexp.exactMatch(temp))
        {
            if (iter.hasNext())
            {
                displayString = iter.next();
            }
            else
            {
                errorsteam << tr("No display string was specified.") << endl;
                encounteredError = true;
            }
        }
    #endif
#endif
        // Check if this is the last argument. If it is and no command line flag
        // is active, the final argument is likely a profile that has
        // been specified.
        else if (!temp.isEmpty() && !iter.hasNext())
        {
            // If the file exists and it is an xml file, assume that it is a
            // profile.
            QFileInfo fileInfo(temp);
            if (fileInfo.exists())
            {
                if (fileInfo.suffix() != "amgp" && fileInfo.suffix() != "xml")
                {
                    errorsteam << tr("Profile location %1 is not an XML file.").arg(temp) << endl;
                    encounteredError = true;
                }
                else
                {
                    profileLocation = fileInfo.absoluteFilePath();
                }
            }
            else
            {
                errorsteam << tr("Profile location %1 does not exist.").arg(temp) << endl;
                encounteredError = true;
            }
        }
    }
}