# HG changeset patch # User Matti Hamalainen # Date 1411685534 -10800 # Node ID 4ca0af6dbcf825cedb6c5906c593ec68c12e202c # Parent dab546dfb9b40d63ee80596811b7c025a75c4eab Another fix in the option handling. diff -r dab546dfb9b4 -r 4ca0af6dbcf8 th_args.c --- 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)