diff options
author | David Sommerseth <dazo@users.sourceforge.net> | 2008-12-04 11:08:01 +0100 |
---|---|---|
committer | David Sommerseth <dazo@users.sourceforge.net> | 2008-12-04 11:08:01 +0100 |
commit | 68372c68e04d0bf0f98eb9ec1e52da2bf669b381 (patch) | |
tree | 8a515c8c65effd08ea08d403c58de6f483c179c8 /eurephiadm | |
parent | 6880a718670b2090bfcc5e82f2e9a9e7278ce9c9 (diff) | |
download | eurephia-68372c68e04d0bf0f98eb9ec1e52da2bf669b381.tar.gz eurephia-68372c68e04d0bf0f98eb9ec1e52da2bf669b381.tar.xz eurephia-68372c68e04d0bf0f98eb9ec1e52da2bf669b381.zip |
Enhanced the argument parser to handle mulitple options to arguments better
The char *optarg is removed and replaced with char *optargs[MAX_ARGUMENTS]
This will contain all extra options given to an argument.
In addtions, MODULE must be defined with a string, which will be used
when printing argument parsing errors
Diffstat (limited to 'eurephiadm')
-rw-r--r-- | eurephiadm/argparser.c | 17 | ||||
-rw-r--r-- | eurephiadm/argparser.h | 13 |
2 files changed, 21 insertions, 9 deletions
diff --git a/eurephiadm/argparser.c b/eurephiadm/argparser.c index 1453b0e..6272c77 100644 --- a/eurephiadm/argparser.c +++ b/eurephiadm/argparser.c @@ -21,12 +21,12 @@ #include <stdio.h> #include <string.h> +#define ARGPARSER_C #include "argparser.h" +char *optargs[MAX_ARGUMENTS]; -char *optarg = NULL; - -int eurephia_getopt(int curarg, int argc, char **argv, e_options *argopts) { +int _eurephia_getopt(const char *module, int curarg, int argc, char **argv, e_options *argopts) { int i = 0; char *arg = argv[curarg]; @@ -34,15 +34,18 @@ int eurephia_getopt(int curarg, int argc, char **argv, e_options *argopts) { if( (strcmp(argopts[i].longarg, arg) == 0) || (strcmp(argopts[i].shortarg, arg) == 0) ) { - if( (argopts[i].param > 0) && (argc > curarg+1) ) { - optarg = argv[curarg+1]; + if( (argopts[i].param > 0) && (argc > curarg+argopts[i].param) ) { + int j = 0; + for( j = 0; j < argopts[i].param; j++ ) { + optargs[j] = argv[curarg+j+1]; + } } else if( (argopts[i].param > 0) ) { - fprintf(stderr, "eurephiadm: ERROR :: Missing argument to '%s'\n", arg); + fprintf(stderr, "%s: ERROR :: Missing argument(s) to '%s'\n", module, arg); return -1; } return (int) *(&(*argopts[i].shortarg)+1); } } - fprintf(stderr, "eurephiadm: ERROR :: Unknown argument '%s'\n", arg); + fprintf(stderr, "%s: ERROR :: Unknown argument '%s'\n", module, arg); return -1; } diff --git a/eurephiadm/argparser.h b/eurephiadm/argparser.h index 56ef01e..d0076f8 100644 --- a/eurephiadm/argparser.h +++ b/eurephiadm/argparser.h @@ -21,14 +21,23 @@ #ifndef EUREPHIA__ARGPARSE_H #define EUREPHIA__ARGPARSE_H +#ifndef ARGPARSER_C +# ifndef MODULE +# error To use the argparser, you must define MODULE (string) +# endif +#endif + +#define MAX_ARGUMENTS 64 + typedef struct { char *longarg; char *shortarg; int param; } e_options; -extern char *optarg; +extern char *optargs[MAX_ARGUMENTS]; -int eurephia_getopt(int curarg, int argc, char **argv, e_options *argopts); +int _eurephia_getopt(const char *module, int curarg, int argc, char **argv, e_options *argopts); +#define eurephia_getopt(ca, ac, av, opts) _eurephia_getopt(MODULE, ca, ac, av, opts) #endif |