diff th_args.c @ 137:0f43a94516f4

Improve argument handling module.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 25 Sep 2014 03:37:08 +0300
parents 286b2249c5d2
children dab546dfb9b4
line wrap: on
line diff
--- a/th_args.c	Thu Sep 25 03:18:27 2014 +0300
+++ b/th_args.c	Thu Sep 25 03:37:08 2014 +0300
@@ -62,15 +62,15 @@
 -------
 The return value from handler callbacks affects the return value of
 th_args_process(). Additionally, a failure in callback (returns FALSE)
-effects the argument processing if bailOut argument of th_args_process()
-is TRUE!
+effects the argument processing if OPTH_BAILOUT flag for th_args_process()
+is set.
 
-If bailOut is TRUE, any error/failure in argument processing (including
+If OPTH_BAILOUT is set, any error/failure in argument processing (including
 callbacks) immediately stops the argument processing and FALSE is
 returned from th_args_process().
 
-If bailOut is FALSE, most errors are "ignored", but FALSE is still returned
-if any errors occured.
+If OPTH_BAILOUT is NOT set, most errors are "ignored", but FALSE is still
+returned if any errors occured.
 
 
 NOTICE #2!
@@ -251,11 +251,11 @@
 BOOL th_args_process(int argc, char *argv[],
                      optarg_t optList[], int optListN,
                      BOOL(*handleOpt) (int, char *, char *),
-                     BOOL(*handleNonOption) (char *), BOOL bailOut)
+                     BOOL(*handleNonOption) (char *), int flags)
 {
     BOOL endOptions, optionsOK;
     int argIndex, newArgIndex;
-    char *currArg;
+    int handle = flags & OPTH_ONLY_MASK;
 
     // Parse arguments
     argIndex = 1;
@@ -263,8 +263,8 @@
     endOptions = FALSE;
     while (argIndex < argc)
     {
-        currArg = argv[argIndex];
-        if ((currArg[0] == '-') && !endOptions)
+        char *currArg = argv[argIndex];
+        if (*currArg == '-' && !endOptions && (handle == OPTH_ONLY_OPTS || handle == 0))
         {
             newArgIndex = argIndex;
             currArg++;
@@ -296,6 +296,7 @@
             argIndex = newArgIndex;
         }
         else
+        if (handle == OPTH_ONLY_OTHER || handle == 0)
         {
             // Was not option argument
             if (handleNonOption == NULL
@@ -307,7 +308,7 @@
         }
 
         // Check if we bail out on invalid argument
-        if (!optionsOK && bailOut)
+        if (!optionsOK && (flags & OPTH_BAILOUT))
             return FALSE;
 
         argIndex++;