# HG changeset patch # User Matti Hamalainen # Date 1411604307 -10800 # Node ID 286b2249c5d253dd0500a639302d70132b8e5ea8 # Parent 1b39682de64f3e0b7edef69bd9682c414b5e5e9d Clean up th_args API. diff -r 1b39682de64f -r 286b2249c5d2 th_args.c --- a/th_args.c Wed Sep 24 23:45:39 2014 +0300 +++ b/th_args.c Thu Sep 25 03:18:27 2014 +0300 @@ -91,18 +91,6 @@ // Option with a required argument { 1, 'o', "output", "Output file name", OPT_ARGREQ }, - - // Option with optional argument - { 2, 'f', "foobar", "Use foobar, with optional string", OPT_ARGOPT }, - - // This option is required to be given, though without other flags - // it may not make much sense. - { 4, 'S', "stupid", "You must give this option", OPT_REQUIRED }, - - // The flags can be combined with OR operator: this option is both - // required to be specified, and also requires argument (the filename) - { 5, 'i', "input", "Input file name", OPT_REQUIRED | OPT_ARGREQ }, - // Option with only long form { 0, 0, "only-long", "Long option", OPT_NONE }, @@ -153,7 +141,7 @@ /* Handle short options */ static BOOL th_args_process_short(char *currArg, int *newArgIndex, - BOOL *wasGiven, int argc, char *argv[], + int argc, char *argv[], optarg_t optList[], int optListN, BOOL (*handleOpt)(int, char *, char *)) { @@ -182,8 +170,6 @@ char tmpStr[2] = { 0, 0 }; // Option was given succesfully, try to handle it - wasGiven[optN] = TRUE; - tmpStr[0] = *tmpArg; if (!handleOpt(optList[optN].id, optArg, tmpStr)) @@ -210,7 +196,7 @@ /* Handle long options */ static BOOL th_args_process_long(char *currArg, int *newArgIndex, - BOOL *wasGiven, int argc, char *argv[], + int argc, char *argv[], optarg_t optList[], int optListN, BOOL (*handleOpt)(int, char *, char *)) { @@ -223,12 +209,15 @@ // Long option for (optN = -1, optLen = i = 0; i < optListN && optN < 0; i++) - if (optList[i].optLong) + { + optarg_t *opt = &optList[i]; + if (opt->optLong) { - optLen = strlen(optList[i].optLong); - if (strncmp(currArg, optList[i].optLong, optLen) == 0) + optLen = strlen(opt->optLong); + if (strncmp(currArg, opt->optLong, optLen) == 0) optN = i; } + } // Get possible option argument, if needed if (optN >= 0) @@ -242,12 +231,9 @@ if (!th_args_check_arg(&optList[optN], optArg)) return FALSE; else - { - // Option was given succesfully, try to handle it - wasGiven[optN] = TRUE; - if (!handleOpt(optList[optN].id, optArg, currArg)) - return FALSE; - } + // Option was given succesfully, try to handle it + if (!handleOpt(optList[optN].id, optArg, currArg)) + return FALSE; } else { @@ -268,17 +254,8 @@ BOOL(*handleNonOption) (char *), BOOL bailOut) { BOOL endOptions, optionsOK; - int argIndex, newArgIndex, i; + int argIndex, newArgIndex; char *currArg; - BOOL *wasGiven; - - // Allocate wasGiven - wasGiven = (BOOL *) th_calloc(optListN, sizeof(BOOL)); - if (!wasGiven) - { - THERR("FATAL ERROR! Could not allocate wasGiven in th_args_process()!\n"); - exit(128); - } // Parse arguments argIndex = 1; @@ -303,7 +280,7 @@ // Long options if (!th_args_process_long(currArg, &newArgIndex, - wasGiven, argc, argv, optList, + argc, argv, optList, optListN, handleOpt)) optionsOK = FALSE; } @@ -311,7 +288,7 @@ { // Short options if (!th_args_process_short(currArg, &newArgIndex, - wasGiven, argc, argv, optList, + argc, argv, optList, optListN, handleOpt)) optionsOK = FALSE; } @@ -331,27 +308,11 @@ // Check if we bail out on invalid argument if (!optionsOK && bailOut) - { - th_free(wasGiven); return FALSE; - } argIndex++; } - // Check wasGiven by isRequired - for (i = 0; i < optListN; i++) - if ((optList[i].flags & OPT_REQUIRED) != 0 && !wasGiven[i]) - { - THERR("Option -%s (--%s) is required.\n", - optList[i].optShort, optList[i].optLong); - - optionsOK = FALSE; - if (bailOut) - break; - } - - th_free(wasGiven); return optionsOK; } @@ -360,54 +321,37 @@ */ void th_args_help(FILE *outFile, optarg_t optList[], int optListN) { - int i, nrequired; + int index; - for (i = nrequired = 0; i < optListN; i++) + for (index = 0; index < optListN; index++) { - optarg_t *o = &optList[i]; + optarg_t *opt = &optList[index]; // Print short option - if (o->optShort != 0) - fprintf(outFile, " -%c, ", o->optShort); + if (opt->optShort != 0) + fprintf(outFile, " -%c, ", opt->optShort); else fprintf(outFile, " "); // Print long option - if (o->optLong) + if (opt->optLong) { char tmpStr[64], *p; - if ((o->flags & OPT_ARGMASK) == OPT_ARGOPT) - { - snprintf(tmpStr, sizeof(tmpStr), "%s[=ARG]", - optList[i].optLong); - p = tmpStr; - } - else if ((o->flags & OPT_ARGMASK) == OPT_ARGREQ) + if ((opt->flags & OPT_ARGMASK) == OPT_ARGREQ) { snprintf(tmpStr, sizeof(tmpStr), "%s=ARG", - optList[i].optLong); + opt->optLong); p = tmpStr; } else - p = o->optLong; + p = opt->optLong; fprintf(outFile, "--%-15s", p); } else fprintf(outFile, " "); - fprintf(outFile, " %s.", optList[i].desc); - - if (o->flags & OPT_REQUIRED) - { - fprintf(outFile, " [*]\n"); - nrequired++; - } - else - fprintf(outFile, "\n"); + fprintf(outFile, " %s.\n", opt->desc); } - - if (nrequired > 0) - fprintf(outFile, "(Options marked with [*] are required)\n"); } diff -r 1b39682de64f -r 286b2249c5d2 th_args.h --- a/th_args.h Wed Sep 24 23:45:39 2014 +0300 +++ b/th_args.h Thu Sep 25 03:18:27 2014 +0300 @@ -18,13 +18,12 @@ /* Option flags */ #define OPT_NONE (0) // Simple option with no arguments -#define OPT_ARGREQ (1) // Option's argument is required -#define OPT_ARGOPT (3) // Option's argument is optional +#define OPT_ARGREQ (1) // Option requires an argument #define OPT_ARGMASK (3) // Mask for option argument flags -#define OPT_REQUIRED (4) // This option is required to be given -typedef struct { +typedef struct +{ int id; char optShort; char *optLong;