VirtualBox

Ignore:
Timestamp:
Jul 31, 2024 12:36:41 AM (7 months ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
164218
Message:

FE/Qt: Introduced some macro-magic to parse arguments with values so we can handle the --option=value and --option:value variants as well. Updated the --help to use '=' instead of ' ' a option/value separator, just like we do for generated --help text in VBoxManage and elsewhere.

Location:
trunk/src/VBox/Frontends/VirtualBox/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VirtualBox/src/globals/UICommon.cpp

    r105546 r105547  
    315315        const char *arg = argBytes.constData();
    316316        enum { OptType_Unknown, OptType_VMRunner, OptType_VMSelector, OptType_MaybeBoth } enmOptType = OptType_Unknown;
     317
     318        const char *pszSep = NULL;
     319#define MATCH_OPT_WITH_VALUE(a_szOption) \
     320            (    !::strncmp(arg,RT_STR_TUPLE(a_szOption)) \
     321              && (   *(pszSep = &arg[sizeof(a_szOption) - 1]) == '\0' \
     322                  || *pszSep == '=' \
     323                  || *pszSep == ':') )
     324#define ASSIGN_OPT_VALUE_TO_QSTRING(a_strDst) do { \
     325                if (*pszSep != '\0') \
     326                    (a_strDst) = &pszSep[1]; \
     327                else if (++i < argc) \
     328                    (a_strDst) = arguments.at(i); \
     329                else \
     330                    (a_strDst).clear(); \
     331            } while (0)
     332#define ASSIGN_OPT_VALUE_TO_QUUID(a_uuidDst) do { \
     333                if (*pszSep != '\0') \
     334                    (a_uuidDst) = QUuid(&pszSep[1]); \
     335                else if (++i < argc) \
     336                    (a_uuidDst) = QUuid(arguments.at(i)); \
     337                else \
     338                    (a_uuidDst) = QUuid(); /* null UUID */ \
     339            } while (0)
     340
    317341        /* NOTE: the check here must match the corresponding check for the
    318342         * options to start a VM in main.cpp and hardenedmain.cpp exactly,
    319343         * otherwise there will be weird error messages. */
    320         if (   !::strcmp(arg, "--startvm")
    321             || !::strcmp(arg, "-startvm"))
    322         {
    323             enmOptType = OptType_VMRunner;
    324             if (++i < argc)
    325             {
    326                 vmNameOrUuid = arguments.at(i);
    327                 startVM = true;
    328             }
     344        if (   MATCH_OPT_WITH_VALUE("--startvm")
     345            || MATCH_OPT_WITH_VALUE("-startvm") /* legacy */)
     346        {
     347            enmOptType = OptType_VMRunner;
     348            ASSIGN_OPT_VALUE_TO_QSTRING(vmNameOrUuid);
     349            startVM = true;
    329350        }
    330351        else if (!::strcmp(arg, "-separate") || !::strcmp(arg, "--separate"))
     
    334355        }
    335356#ifdef VBOX_GUI_WITH_PIDFILE
    336         else if (!::strcmp(arg, "-pidfile") || !::strcmp(arg, "--pidfile"))
     357        else if (MATCH_OPT_WITH_VALUE("--pidfile") || MATCH_OPT_WITH_VALUE("-pidfile"))
    337358        {
    338359            enmOptType = OptType_MaybeBoth;
    339             if (++i < argc)
    340                 m_strPidFile = arguments.at(i);
     360            ASSIGN_OPT_VALUE_TO_QSTRING(m_strPidFile);
    341361        }
    342362#endif /* VBOX_GUI_WITH_PIDFILE */
     
    363383        }
    364384        /* Passwords: */
    365         else if (!::strcmp(arg, "--settingspw"))
     385        else if (MATCH_OPT_WITH_VALUE("--settingspw") || MATCH_OPT_WITH_VALUE("--settings-pw"))
    366386        {
    367387            enmOptType = OptType_MaybeBoth;
    368             if (++i < argc)
     388            if (*pszSep == '\0')
     389                RTStrCopy(m_astrSettingsPw, sizeof(m_astrSettingsPw), &pszSep[1]);
     390            else if (++i < argc)
     391                RTStrCopy(m_astrSettingsPw, sizeof(m_astrSettingsPw), arguments.at(i).toLocal8Bit().constData());
     392            else
     393                m_astrSettingsPw[0] = '\0';
     394            m_fSettingsPwSet = m_astrSettingsPw[0] != '\0';
     395        }
     396        else if (MATCH_OPT_WITH_VALUE("--settingspwfile") || MATCH_OPT_WITH_VALUE("--settings-pw-file"))
     397        {
     398            enmOptType = OptType_MaybeBoth;
     399            QString strFilename;
     400            ASSIGN_OPT_VALUE_TO_QSTRING(strFilename);
     401            if (!strFilename.isEmpty())
    369402            {
    370                 RTStrCopy(m_astrSettingsPw, sizeof(m_astrSettingsPw), arguments.at(i).toLocal8Bit().constData());
    371                 m_fSettingsPwSet = true;
    372             }
    373         }
    374         else if (!::strcmp(arg, "--settingspwfile"))
    375         {
    376             enmOptType = OptType_MaybeBoth;
    377             if (++i < argc)
    378             {
    379                 const QByteArray &argFileBytes = arguments.at(i).toLocal8Bit();
     403                const QByteArray &argFileBytes = strFilename.toLocal8Bit();
    380404                const char *pszFile = argFileBytes.constData();
    381405                bool fStdIn = !::strcmp(pszFile, "stdin");
     
    406430        }
    407431        /* Misc options: */
    408         else if (!::strcmp(arg, "-comment") || !::strcmp(arg, "--comment"))
     432        else if (MATCH_OPT_WITH_VALUE("--comment") || MATCH_OPT_WITH_VALUE("--comment"))
    409433        {
    410434            enmOptType = OptType_MaybeBoth;
    411             ++i;
     435            if (*pszSep == '\0')
     436                ++i; /* we completely ignore the option value here, it's here only for 'ps' listing. */
    412437        }
    413438        else if (!::strcmp(arg, "--no-startvm-errormsgbox"))
     
    432457            m_strSnapshotToRestore.clear();
    433458        }
    434         else if (   !::strcmp(arg, "--restore-snapshot")
    435                  || !::strncmp(arg, RT_STR_TUPLE("--restore-snapshot="))
    436                  || !::strncmp(arg, RT_STR_TUPLE("--restore-snapshot:")))
    437         {
    438             enmOptType = OptType_VMRunner;
    439             const char * const pszSep = &arg[sizeof("--restore-snapshot") - 1];
    440             if (*pszSep != '\0')
    441                 m_strSnapshotToRestore = &pszSep[1];
    442             else if (++i < argc)
    443                 m_strSnapshotToRestore = arguments.at(i);
    444             else
    445                 m_strSnapshotToRestore.clear();
     459        else if (MATCH_OPT_WITH_VALUE("--restore-snapshot"))
     460        {
     461            enmOptType = OptType_VMRunner;
     462            ASSIGN_OPT_VALUE_TO_QSTRING(m_strSnapshotToRestore);
    446463            m_fRestoreCurrentSnapshot = false;
    447464        }
     
    452469        }
    453470        /* Ad hoc VM reconfig options: */
    454         else if (!::strcmp(arg, "--fda"))
    455         {
    456             enmOptType = OptType_VMRunner;
    457             if (++i < argc)
    458                 m_uFloppyImage = QUuid(arguments.at(i));
    459         }
    460         else if (!::strcmp(arg, "--dvd") || !::strcmp(arg, "--cdrom"))
    461         {
    462             enmOptType = OptType_VMRunner;
    463             if (++i < argc)
    464                 m_uDvdImage = QUuid(arguments.at(i));
     471        else if (MATCH_OPT_WITH_VALUE("--fda"))
     472        {
     473            enmOptType = OptType_VMRunner;
     474/** @todo r=bird: this isn't correct. Must use QString instead of QUuid to store this! */
     475            ASSIGN_OPT_VALUE_TO_QUUID(m_uFloppyImage);
     476        }
     477        else if (MATCH_OPT_WITH_VALUE("--dvd") || MATCH_OPT_WITH_VALUE("--cdrom"))
     478        {
     479            enmOptType = OptType_VMRunner;
     480/** @todo r=bird: this isn't correct. Must use QString instead of QUuid to store this! */
     481            ASSIGN_OPT_VALUE_TO_QUUID(m_uDvdImage);
    465482        }
    466483        /* VMM Options: */
     
    472489        else if (!::strcmp(arg, "--driverless"))
    473490            enmOptType = OptType_VMRunner;
    474         else if (!::strcmp(arg, "--warp-pct"))
    475         {
    476             enmOptType = OptType_VMRunner;
    477             if (++i < argc)
    478                 m_uWarpPct = RTStrToUInt32(arguments.at(i).toLocal8Bit().constData());
     491        else if (MATCH_OPT_WITH_VALUE("--warp-pct"))
     492        {
     493            enmOptType = OptType_VMRunner;
     494            QString strValue;
     495            ASSIGN_OPT_VALUE_TO_QSTRING(strValue);
     496            m_uWarpPct = strValue.isEmpty() ? 100 : RTStrToUInt32(strValue.toLocal8Bit().constData());
    479497        }
    480498#ifdef VBOX_WITH_DEBUGGER_GUI
     
    507525            setDebuggerVar(&m_fDbgAutoShowStatistics, true);
    508526        }
    509         else if (!::strcmp(arg, "--statistics-expand") || !::strcmp(arg, "--stats-expand"))
    510         {
    511             enmOptType = OptType_VMRunner;
    512             if (++i < argc)
     527        else if (MATCH_OPT_WITH_VALUE("--statistics-expand") || MATCH_OPT_WITH_VALUE("--stats-expand"))
     528        {
     529            enmOptType = OptType_VMRunner;
     530            QString strValue;
     531            ASSIGN_OPT_VALUE_TO_QSTRING(strValue);
     532            if (!strValue.isEmpty())
    513533            {
    514534                if (!m_strDbgStatisticsExpand.isEmpty())
    515535                    m_strDbgStatisticsExpand.append('|');
    516                 m_strDbgStatisticsExpand.append(arguments.at(i));
     536                m_strDbgStatisticsExpand.append(strValue);
    517537            }
    518538        }
    519         else if (!::strncmp(arg, RT_STR_TUPLE("--statistics-expand=")) || !::strncmp(arg, RT_STR_TUPLE("--stats-expand=")))
    520         {
    521             enmOptType = OptType_VMRunner;
    522             if (!m_strDbgStatisticsExpand.isEmpty())
    523                 m_strDbgStatisticsExpand.append('|');
    524             m_strDbgStatisticsExpand.append(arguments.at(i).section('=', 1));
    525         }
    526         else if (!::strcmp(arg, "--statistics-filter") || !::strcmp(arg, "--stats-filter"))
    527         {
    528             enmOptType = OptType_VMRunner;
    529             if (++i < argc)
    530                 m_strDbgStatisticsFilter = arguments.at(i);
    531         }
    532         else if (!::strncmp(arg, RT_STR_TUPLE("--statistics-filter=")) || !::strncmp(arg, RT_STR_TUPLE("--stats-filter=")))
    533         {
    534             enmOptType = OptType_VMRunner;
    535             m_strDbgStatisticsFilter = arguments.at(i).section('=', 1);
    536         }
    537         else if (!::strcmp(arg, "--statistics-config") || !::strcmp(arg, "--stats-config"))
    538         {
    539             enmOptType = OptType_VMRunner;
    540             if (++i < argc)
    541                 m_strDbgStatisticsConfig = arguments.at(i);
    542         }
    543         else if (!::strncmp(arg, RT_STR_TUPLE("--statistics-config=")) || !::strncmp(arg, RT_STR_TUPLE("--stats-config=")))
    544         {
    545             enmOptType = OptType_VMRunner;
    546             m_strDbgStatisticsConfig = arguments.at(i).section('=', 1);
     539        else if (MATCH_OPT_WITH_VALUE("--statistics-filter") || MATCH_OPT_WITH_VALUE("--stats-filter"))
     540        {
     541            enmOptType = OptType_VMRunner;
     542            ASSIGN_OPT_VALUE_TO_QSTRING(m_strDbgStatisticsFilter);
     543        }
     544        else if (MATCH_OPT_WITH_VALUE("--statistics-config") || MATCH_OPT_WITH_VALUE("--stats-config"))
     545        {
     546            enmOptType = OptType_VMRunner;
     547            ASSIGN_OPT_VALUE_TO_QSTRING(m_strDbgStatisticsConfig);
    547548        }
    548549        else if (!::strcmp(arg, "-no-debug") || !::strcmp(arg, "--no-debug"))
     
    571572        i++;
    572573    }
     574#undef MATCH_OPT_WITH_VALUE
     575#undef ASSIGN_OPT_VALUE_TO_QSTRING
     576#undef ASSIGN_OPT_VALUE_TO_QUUID
    573577
    574578    if (uiType() == UIType_RuntimeUI && startVM)
  • trunk/src/VBox/Frontends/VirtualBox/src/main.cpp

    r105546 r105547  
    273273#ifdef VBOX_RUNTIME_UI
    274274        "Options:\n"
    275         "  --startvm <vmname|UUID>    start a VM by specifying its UUID or name\n"
     275        "  --startvm=<vmname|UUID>    start a VM by specifying its UUID or name\n"
    276276        "  --separate                 start a separate VM process\n"
    277277        "  --normal                   keep normal (windowed) mode during startup\n"
     
    283283        "  --restore-snapshot=<name>  restore the named snapshot before starting\n"
    284284        "  --no-aggressive-caching    delays caching media info in VM processes\n"
    285         "  --fda <image|none>         Mount the specified floppy image\n"
    286         "  --dvd <image|none>         Mount the specified DVD image\n"
     285        "  --fda=<image|none>         Mount the specified floppy image\n"
     286        "  --dvd=<image|none>         Mount the specified DVD image\n"
    287287# ifdef VBOX_GUI_WITH_PIDFILE
    288         "  --pidfile <file>           create a pidfile file when a VM is up and running\n"
     288        "  --pidfile=<file>           create a pidfile file when a VM is up and running\n"
    289289# endif /* VBOX_GUI_WITH_PIDFILE */
    290290# ifdef VBOX_WITH_DEBUGGER_GUI
     
    293293        "  --debug-command-line       like --dbg and show command line window at VM startup\n"
    294294        "  --debug-statistics         like --dbg and show statistics window at VM startup\n"
    295         "  --statistics-expand <pat>  expand the matching statistics (can be repeated)\n"
    296         "  --statistics-filter <pat>  statistics filter\n"
    297         "  --statistics-config <str>  statistics configuration\n"
     295        "  --statistics-expand=<pat>  expand the matching statistics (can be repeated)\n"
     296        "  --statistics-filter=<pat>  statistics filter\n"
     297        "  --statistics-config=<str>  statistics configuration\n"
    298298        "  --no-debug                 disable the GUI debug menu and debug windows\n"
    299299        "  --start-paused             start the VM in the paused state\n"
     
    304304        "  --execute-all-in-iem       For debugging the interpreted execution mode.\n"
    305305        "  --driverless               Do not open the support driver (NEM or IEM mode).\n"
    306         "  --warp-pct <pct>           time warp factor, 100%% (= 1.0) = normal speed\n"
     306        "  --warp-pct=<pct>           time warp factor, 100%% (= 1.0) = normal speed\n"
    307307        "  --no-keyboard-grabbing     Don't grab the keyboard on windows (for debugging).\n"
    308308        "\n"
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette