changeset 139:4ca0af6dbcf8

Another fix in the option handling.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 26 Sep 2014 01:52:14 +0300
parents dab546dfb9b4
children f81c6dc7bd0f
files th_args.c
diffstat 1 files changed, 31 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/th_args.c	Thu Sep 25 03:47:48 2014 +0300
+++ b/th_args.c	Fri Sep 26 01:52:14 2014 +0300
@@ -143,7 +143,7 @@
 static BOOL th_args_process_short(char *currArg, int *newArgIndex,
     int argc, char *argv[],
     optarg_t optList[], int optListN,
-    BOOL (*handleOpt)(int, char *, char *))
+    BOOL (*handleOpt)(int, char *, char *), BOOL process)
 {
     char *tmpArg = currArg, *optArg;
     int optN;
@@ -166,6 +166,7 @@
                 if (!th_args_check_arg(&optList[optN], optArg))
                     return FALSE;
                 else
+                if (process)
                 {
                     char tmpStr[2] = { 0, 0 };
 
@@ -198,7 +199,7 @@
 static BOOL th_args_process_long(char *currArg, int *newArgIndex,
     int argc, char *argv[],
     optarg_t optList[], int optListN,
-    BOOL (*handleOpt)(int, char *, char *))
+    BOOL (*handleOpt)(int, char *, char *), BOOL process)
 {
     int optN, optLen, i;
     char *optArg;
@@ -232,7 +233,7 @@
             return FALSE;
         else
         // Option was given succesfully, try to handle it
-        if (!handleOpt(optList[optN].id, optArg, currArg))
+        if (process && !handleOpt(optList[optN].id, optArg, currArg))
             return FALSE;
     }
     else
@@ -253,50 +254,45 @@
                      BOOL(*handleOpt) (int, char *, char *),
                      BOOL(*handleNonOption) (char *), int flags)
 {
-    BOOL endOptions, optionsOK;
-    int argIndex, newArgIndex;
-    int handle = flags & OPTH_ONLY_MASK;
+    int handle = flags & OPTH_ONLY_MASK,
+        argIndex = 1;
+    BOOL optionsOK = TRUE,
+        endOptions = FALSE;
 
-    // Parse arguments
-    argIndex = 1;
-    optionsOK = TRUE;
-    endOptions = FALSE;
     while (argIndex < argc)
     {
         char *currArg = argv[argIndex];
         if (*currArg == '-' && !endOptions)
         {
-            if (handle == OPTH_ONLY_OPTS || handle == 0)
+            BOOL process = (handle & OPTH_ONLY_OPTS) || handle == 0;
+            int newArgIndex = argIndex;
+            currArg++;
+            if (*currArg == '-')
             {
-                newArgIndex = argIndex;
+                // Check for "--", which ends the options-list
                 currArg++;
-                if (*currArg == '-')
+                if (*currArg == 0)
                 {
-                    // Check for "--", which ends the options-list
-                    currArg++;
-                    if (*currArg == 0)
-                    {
-                        endOptions = TRUE;
-                        continue;
-                    }
-
-                    // Long options
-                    if (!th_args_process_long(currArg, &newArgIndex,
-                                              argc, argv, optList,
-                                              optListN, handleOpt))
-                        optionsOK = FALSE;
-                }
-                else
-                {
-                    // Short options
-                    if (!th_args_process_short(currArg, &newArgIndex,
-                                               argc, argv, optList,
-                                               optListN, handleOpt))
-                        optionsOK = FALSE;
+                    endOptions = TRUE;
+                    continue;
                 }
 
-                argIndex = newArgIndex;
+                // Long options
+                if (!th_args_process_long(currArg, &newArgIndex,
+                                          argc, argv, optList,
+                                          optListN, handleOpt, process))
+                    optionsOK = FALSE;
             }
+            else
+            {
+                // Short options
+                if (!th_args_process_short(currArg, &newArgIndex,
+                                           argc, argv, optList,
+                                           optListN, handleOpt, process))
+                    optionsOK = FALSE;
+            }
+
+            argIndex = newArgIndex;
         }
         else
         if (handle == OPTH_ONLY_OTHER || handle == 0)