diff options
author | Denys Vlasenko <dvlasenk@redhat.com> | 2010-10-08 15:24:30 +0200 |
---|---|---|
committer | Denys Vlasenko <dvlasenk@redhat.com> | 2010-10-08 15:24:30 +0200 |
commit | 8c7950b3b2da5c7041190626aa5e30e66d9b8f6a (patch) | |
tree | 95daf85b395617c0421ec0dfc15d2e0a92ce8a58 /lib/utils | |
parent | c963dd2b23debcf1d650190717cd89d266acef5c (diff) | |
download | abrt-8c7950b3b2da5c7041190626aa5e30e66d9b8f6a.tar.gz abrt-8c7950b3b2da5c7041190626aa5e30e66d9b8f6a.tar.xz abrt-8c7950b3b2da5c7041190626aa5e30e66d9b8f6a.zip |
add --help option in parse_opts(), not at every call site.
This patch removes the need to add --help option to every program.
It is added (and handled) by parse_opts().
Signed-off-by: Denys Vlasenko <dvlasenk@redhat.com>
Diffstat (limited to 'lib/utils')
-rw-r--r-- | lib/utils/parse_options.c | 41 | ||||
-rw-r--r-- | lib/utils/parse_options.h | 1 | ||||
-rw-r--r-- | lib/utils/xfuncs.c | 2 |
3 files changed, 26 insertions, 18 deletions
diff --git a/lib/utils/parse_options.c b/lib/utils/parse_options.c index e65a1bb9..08fac9ec 100644 --- a/lib/utils/parse_options.c +++ b/lib/utils/parse_options.c @@ -3,15 +3,6 @@ #include "abrtlib.h" #include "parse_options.h" -static int parse_opt_size(const struct options *opt) -{ - unsigned size = 1; - for (; opt->type != OPTION_END; opt++) - size++; - - return size; -} - #define USAGE_OPTS_WIDTH 24 #define USAGE_GAP 2 @@ -64,16 +55,26 @@ void parse_usage_and_die(const char * const * usage, const struct options *opt) exit(1); } +static int parse_opt_size(const struct options *opt) +{ + unsigned size = 0; + for (; opt->type != OPTION_END; opt++) + size++; + + return size; +} + void parse_opts(int argc, char **argv, const struct options *opt, const char * const usage[]) { + int help = 0; int size = parse_opt_size(opt); struct strbuf *shortopts = strbuf_new(); - struct option *longopts = xcalloc(sizeof(struct options), size); + struct option *longopts = xzalloc(sizeof(longopts[0]) * (size+2)); int ii; - for (ii = 0; ii < size - 1; ++ii) + for (ii = 0; ii < size; ++ii) { longopts[ii].name = opt[ii].long_name; @@ -94,14 +95,20 @@ void parse_opts(int argc, char **argv, const struct options *opt, case OPTION_END: break; } - longopts[ii].flag = 0; + /*longopts[ii].flag = 0; - xzalloc did it */ longopts[ii].val = opt[ii].short_name; } - - longopts[ii].name = 0; + longopts[ii].name = "help"; + /*longopts[ii].has_arg = 0; - xzalloc did it */ + longopts[ii].flag = &help; + longopts[ii].val = 1; + /* xzalloc did it already: + ii++; + longopts[ii].name = NULL; longopts[ii].has_arg = 0; - longopts[ii].flag = 0; + longopts[ii].flag = NULL; longopts[ii].val = 0; + */ int option_index = 0; while (1) @@ -111,14 +118,14 @@ void parse_opts(int argc, char **argv, const struct options *opt, if (c == -1) break; - if (c == '?') + if (c == '?' || help) { free(longopts); strbuf_free(shortopts); parse_usage_and_die(usage, opt); } - for (ii = 0; ii < size - 1; ++ii) + for (ii = 0; ii < size; ++ii) { if (opt[ii].short_name == c) { diff --git a/lib/utils/parse_options.h b/lib/utils/parse_options.h index 9f40fc94..9c4758af 100644 --- a/lib/utils/parse_options.h +++ b/lib/utils/parse_options.h @@ -38,7 +38,6 @@ struct options { #define OPT_STRING(s, l, v, a, h) { OPTION_STRING, (s), (l), (v), (a), (h) } #define OPT__VERBOSE(v) OPT_BOOL('v', "verbose", (v), "be verbose") -#define OPT__HELP(v) OPT_BOOL('h', "help", (v), "show this help message") void parse_opts(int argc, char **argv, const struct options *opt, const char * const usage[]); diff --git a/lib/utils/xfuncs.c b/lib/utils/xfuncs.c index 214ce426..d5166037 100644 --- a/lib/utils/xfuncs.c +++ b/lib/utils/xfuncs.c @@ -39,6 +39,7 @@ int close_on_exec_on(int fd) return fcntl(fd, F_SETFD, FD_CLOEXEC); } +#if 0 /* unused */ void *xcalloc(size_t nmemb, size_t size) { void *ptr = calloc(nmemb, size); @@ -46,6 +47,7 @@ void *xcalloc(size_t nmemb, size_t size) die_out_of_memory(); return ptr; } +#endif // Die if we can't allocate size bytes of memory. void* xmalloc(size_t size) |