changeset 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
files th_args.c th_args.h
diffstat 2 files changed, 17 insertions(+), 11 deletions(-) [+]
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++;
--- a/th_args.h	Thu Sep 25 03:18:27 2014 +0300
+++ b/th_args.h	Thu Sep 25 03:37:08 2014 +0300
@@ -21,6 +21,11 @@
 #define OPT_ARGREQ     (1)    // Option requires an argument
 #define OPT_ARGMASK    (3)    // Mask for option argument flags
 
+#define OPTH_BAILOUT       0x0001 // Bail out on errors
+#define OPTH_ONLY_OPTS     0x0010 // Handle only options
+#define OPTH_ONLY_OTHER    0x0020 // Handle only "non-options"
+#define OPTH_ONLY_MASK     0x00f0 // Mask
+
 
 typedef struct
 {
@@ -35,7 +40,7 @@
 BOOL th_args_process(int argc, char *argv[],
      optarg_t argList[], int argListN,
      BOOL (*handleOpt)(int, char *, char *),
-     BOOL (*handleFile)(char *), BOOL);
+     BOOL (*handleFile)(char *), int flags);
 
 void th_args_help(FILE *, optarg_t optList[], int optListN);