Index: /trunk/src/VBox/Runtime/common/misc/getopt.cpp
===================================================================
--- /trunk/src/VBox/Runtime/common/misc/getopt.cpp	(revision 17092)
+++ /trunk/src/VBox/Runtime/common/misc/getopt.cpp	(revision 17093)
@@ -40,16 +40,32 @@
 
 
-RTDECL(int) RTGetOpt(int argc, char **argv, PCRTOPTIONDEF paOptions, size_t cOptions, int *piThis, PRTOPTIONUNION pValueUnion)
+RTDECL(int) RTGetOptInit(PRTGETOPTSTATE pState, int argc, char **argv,
+                         PCRTGETOPTDEF paOptions, size_t cOptions,
+                         int iFirst, uint32_t fFlags)
+{
+    AssertReturn(!fFlags, VERR_INVALID_PARAMETER);
+
+    pState->argv        = argv;
+    pState->argc        = argc;
+    pState->paOptions   = paOptions;
+    pState->cOptions    = cOptions;
+    pState->iNext       = iFirst;
+
+    return VINF_SUCCESS;
+}
+
+
+RTDECL(int) RTGetOpt(PRTGETOPTSTATE pState, PRTGETOPTUNION pValueUnion)
 {
     pValueUnion->u64 = 0;
     pValueUnion->pDef = NULL;
 
-    if (    !piThis
-         || *piThis >= argc
-       )
+    if (pState->iNext >= pState->argc)
         return 0;
 
-    int iThis = (*piThis)++;
-    const char *pszArgThis = argv[iThis];
+    int             iThis = pState->iNext++;
+    const char     *pszArgThis = pState->argv[iThis];
+    size_t const    cOptions  = pState->cOptions;
+    PCRTGETOPTDEF   paOptions = pState->paOptions;
 
     for (size_t i = 0; i < cOptions; i++)
@@ -57,7 +73,8 @@
         Assert(!(paOptions[i].fFlags & ~RTGETOPT_VALID_MASK));
         Assert(paOptions[i].iShort > 0);
+        Assert(paOptions[i].iShort != VINF_GETOPT_NOT_OPTION);
 
         bool fShort = *pszArgThis == '-'
-                    && (uint32_t)pszArgThis[1] == paOptions[i].iShort;
+                    && pszArgThis[1] == paOptions[i].iShort;
 
         if ((paOptions[i].fFlags & RTGETOPT_REQ_MASK) != RTGETOPT_REQ_NOTHING)
@@ -91,8 +108,8 @@
                     :   pszArgThis[cchLong] == '\0' || pszArgThis[cchLong + 1] == '\0')
                 {
-                    if (iThis + 1 >= argc)
+                    if (iThis + 1 >= pState->argc)
                         return VERR_GETOPT_REQUIRED_ARGUMENT_MISSING;
-                    pszValue = argv[iThis + 1];
-                    (*piThis)++;
+                    pszValue = pState->argv[iThis + 1];
+                    pState->iNext++;
                 }
                 else /* same argument. */
@@ -196,12 +213,12 @@
         return VERR_GETOPT_UNKNOWN_OPTION;
 
+    /** @todo Handle '--' and possibly implement an RTGetOptInit that lets us
+     *        optionally sort the stuff and set other policeis sorts the result.  */
+
     /*
      * Not an option.
      */
-    (*piThis)--;
-    /** @todo Sort options and arguments (i.e. stuff that doesn't start with '-'), stop when
-     * encountering the first argument. */
-
-    return 0;
+    pValueUnion->psz = pszArgThis;
+    return VINF_GETOPT_NOT_OPTION;
 }
 
Index: /trunk/src/VBox/Runtime/testcase/tstGetOpt.cpp
===================================================================
--- /trunk/src/VBox/Runtime/testcase/tstGetOpt.cpp	(revision 17092)
+++ /trunk/src/VBox/Runtime/testcase/tstGetOpt.cpp	(revision 17093)
@@ -42,14 +42,14 @@
     RTR3Init();
 
-    int i;
-    RTOPTIONUNION Val;
-#define CHECK(expr)  do { if (!(expr)) { RTPrintf("tstGetOpt: error line %d (i=%d): %s\n", __LINE__, i, #expr); cErrors++; } } while (0)
+    RTGETOPTSTATE GetState;
+    RTGETOPTUNION Val;
+#define CHECK(expr)  do { if (!(expr)) { RTPrintf("tstGetOpt: error line %d (iNext=%d): %s\n", __LINE__, GetState.iNext, #expr); cErrors++; } } while (0)
 
 #define CHECK_GETOPT(expr, chRet, iInc) \
     do { \
-        const int iPrev = i; \
+        const int iPrev = GetState.iNext; \
         CHECK((expr) == (chRet)); \
-        CHECK(i == (iInc) + iPrev); \
-        i = (iInc) + iPrev; \
+        CHECK(GetState.iNext == (iInc) + iPrev); \
+        GetState.iNext = (iInc) + iPrev; \
     } while (0)
 
@@ -57,5 +57,5 @@
      * Simple.
      */
-    static const RTOPTIONDEF s_aOpts2[] =
+    static const RTGETOPTDEF s_aOpts2[] =
     {
         { "--optwithstring",    's', RTGETOPT_REQ_STRING },
@@ -64,4 +64,5 @@
         { NULL,                 'q', RTGETOPT_REQ_NOTHING },
         { "--quiet",            384, RTGETOPT_REQ_NOTHING },
+        { "-quiet2",            385, RTGETOPT_REQ_NOTHING },
     };
 
@@ -84,4 +85,5 @@
         "-q",
         "--quiet",
+        "-quiet2",
         /* "filename1", */
         /* "filename2", */
@@ -90,45 +92,48 @@
     int argc2 = (int)RT_ELEMENTS(argv2) - 1;
 
-    i = 0;
-    CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 's', 2);
+    CHECK(RT_SUCCESS(RTGetOptInit(&GetState, argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), 0, 0 /* fFlags */)));
+
+    CHECK_GETOPT(RTGetOpt(&GetState, &Val), 's', 2);
     CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string1"));
-    CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 's', 2);
+    CHECK_GETOPT(RTGetOpt(&GetState, &Val), 's', 2);
     CHECK(VALID_PTR(Val.psz) && !strcmp(Val.psz, "string2"));
 
     /* -i */
-    CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'i', 2);
+    CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
     CHECK(Val.i32 == -42);
-    CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'i', 1);
+    CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 1);
     CHECK(Val.i32 == -42);
-    CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'i', 1);
+    CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 1);
     CHECK(Val.i32 == -42);
-    CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'i', 2);
+    CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
     CHECK(Val.i32 == -42);
-    CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'i', 2);
+    CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
     CHECK(Val.i32 == -42);
 
     /* --optwithint */
-    CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'i', 2);
+    CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
     CHECK(Val.i32 == 42);
-    CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'i', 1);
+    CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 1);
     CHECK(Val.i32 == 42);
-    CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'i', 1);
+    CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 1);
     CHECK(Val.i32 == 42);
-    CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'i', 2);
+    CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
     CHECK(Val.i32 == 42);
-    CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'i', 2);
+    CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'i', 2);
     CHECK(Val.i32 == 42);
 
-    CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'v', 1);
+    CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'v', 1);
     CHECK(Val.pDef == &s_aOpts2[2]);
-    CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'v', 1);
+    CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'v', 1);
     CHECK(Val.pDef == &s_aOpts2[2]);
-    CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 'q', 1);
+    CHECK_GETOPT(RTGetOpt(&GetState, &Val), 'q', 1);
     CHECK(Val.pDef == &s_aOpts2[3]);
-    CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 384, 1);
+    CHECK_GETOPT(RTGetOpt(&GetState, &Val), 384, 1);
     CHECK(Val.pDef == &s_aOpts2[4]);
-    CHECK_GETOPT(RTGetOpt(argc2, argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), &i, &Val), 0, 0);
+    CHECK_GETOPT(RTGetOpt(&GetState, &Val), 385, 1);
+    CHECK(Val.pDef == &s_aOpts2[5]);
+    CHECK_GETOPT(RTGetOpt(&GetState, &Val), 0, 0);
     CHECK(Val.pDef == NULL);
-    CHECK(argc2 == i);
+    CHECK(argc2 == GetState.iNext);
 
 
Index: /trunk/src/VBox/Runtime/testcase/tstHandleTable.cpp
===================================================================
--- /trunk/src/VBox/Runtime/testcase/tstHandleTable.cpp	(revision 17092)
+++ /trunk/src/VBox/Runtime/testcase/tstHandleTable.cpp	(revision 17093)
@@ -455,5 +455,5 @@
     RTR3Init();
 
-    static RTOPTIONDEF const s_aOptions[] =
+    static RTGETOPTDEF const s_aOptions[] =
     {
         { "--base",         'b', RTGETOPT_REQ_UINT32 },
@@ -469,7 +469,8 @@
 
     int ch;
-    int iArg = 1;
-    RTOPTIONUNION Value;
-    while ((ch = RTGetOpt(argc,argv, &s_aOptions[0], RT_ELEMENTS(s_aOptions), &iArg, &Value)))
+    RTGETOPTUNION Value;
+    RTGETOPTSTATE GetState;
+    RTGetOptInit(&GetState, argc, argv, &s_aOptions[0], RT_ELEMENTS(s_aOptions), 1, 0 /* fFlags */);
+    while ((ch = RTGetOpt(&GetState, &Value)))
         switch (ch)
         {
@@ -492,4 +493,8 @@
                 RTPrintf("syntax: tstHandleTable [-b <base>] [-m <max>] [-t <threads>]\n");
                 return 1;
+
+            case VINF_GETOPT_NOT_OPTION:
+                RTPrintf("tstHandleTable: unexpected non-option: %s\n", Value.psz);
+                break;
 
             default:
@@ -500,9 +505,4 @@
                 return 1;
         }
-    if (iArg < argc)
-    {
-        RTPrintf("tstHandleTable: invalid argument: %s\n", argv[iArg]);
-        return 1;
-    }
 
     /*
Index: /trunk/src/VBox/Runtime/testcase/tstTSC.cpp
===================================================================
--- /trunk/src/VBox/Runtime/testcase/tstTSC.cpp	(revision 17092)
+++ /trunk/src/VBox/Runtime/testcase/tstTSC.cpp	(revision 17093)
@@ -416,5 +416,5 @@
     bool fCalcFrequency = false;
     uint32_t cMsDuration = 1000; /* 1 sec */
-    static const RTOPTIONDEF s_aOptions[] =
+    static const RTGETOPTDEF s_aOptions[] =
     {
         { "--duration",         'd', RTGETOPT_REQ_UINT32 },
@@ -424,6 +424,8 @@
     int iArg = 1;
     int ch;
-    RTOPTIONUNION Value;
-    while ((ch = RTGetOpt(argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), &iArg, &Value)))
+    RTGETOPTUNION Value;
+    RTGETOPTSTATE GetState;
+    RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, 0 /* fFlags */);
+    while ((ch = RTGetOpt(&GetState, &Value)))
         switch (ch)
         {
@@ -434,13 +436,13 @@
                          "   or: tstTSC <-f|--calc-frequency> [--duration|-d ms]\n");
                 return 1;
+
+            case VINF_GETOPT_NOT_OPTION:
+                RTStrmPrintf(g_pStdErr, "tstTSC: too many arguments\n");
+                break;
+
             default:
                 RTStrmPrintf(g_pStdErr, "tstTSC: Unknown arg or error (ch=%d)\n", ch);
                 return 1;
         }
-    if (iArg != argc)
-    {
-        RTStrmPrintf(g_pStdErr, "tstTSC: too many arguments\n");
-        return 1;
-    }
 
     if (fCalcFrequency)
