From: Jonathan Dieter Date: Fri, 4 May 2018 15:08:45 +0000 (+0300) Subject: Make zck_read_header a proper utility with arguments and help. X-Git-Tag: archive/raspbian/1.1.9+ds1-1+rpi1~1^2~288 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=b02a7de358fd3deefb430789fb0545160589d6fc;p=zchunk.git Make zck_read_header a proper utility with arguments and help. Signed-off-by: Jonathan Dieter --- diff --git a/src/zck_read_header.c b/src/zck_read_header.c index 32f784e..bbc1cf6 100644 --- a/src/zck_read_header.c +++ b/src/zck_read_header.c @@ -31,17 +31,81 @@ #include #include #include +#include #include -int main (int argc, char *argv[]) { - if(argc != 2) { - printf("Usage: %s \n", argv[0]); - exit(1); +#include "util_common.h" + +static char doc[] = "zck_read_header - Read header from a zchunk file"; + +static char args_doc[] = ""; + +static struct argp_option options[] = { + {"verbose", 'v', 0, 0, + "Increase verbosity (can be specified more than once for debugging)"}, + {"quiet", 'q', 0, 0, "Only show errors"}, + {"version", 'V', 0, 0, "Show program version"}, + { 0 } +}; + +struct arguments { + char *args[1]; + zck_log_type log_level; +}; + +static error_t parse_opt (int key, char *arg, struct argp_state *state) { + struct arguments *arguments = state->input; + + switch (key) { + case 'v': + arguments->log_level -= 1; + if(arguments->log_level < ZCK_LOG_DEBUG) + arguments->log_level = ZCK_LOG_DEBUG; + break; + case 'q': + arguments->log_level = ZCK_LOG_ERROR; + break; + case 'V': + version(); + break; + + case ARGP_KEY_ARG: + if (state->arg_num >= 1) { + argp_usage (state); + return EINVAL; + } + arguments->args[state->arg_num] = arg; + + break; + + case ARGP_KEY_END: + if (state->arg_num < 1) { + argp_usage (state); + return EINVAL; + } + break; + + default: + return ARGP_ERR_UNKNOWN; } + return 0; +} + +static struct argp argp = {options, parse_opt, args_doc, doc}; + +int main (int argc, char *argv[]) { + struct arguments arguments = {0}; + + /* Defaults */ + arguments.log_level = ZCK_LOG_WARNING; + + argp_parse (&argp, argc, argv, 0, 0, &arguments); + + zck_set_log_level(arguments.log_level); - int src_fd = open(argv[1], O_RDONLY); + int src_fd = open(arguments.args[0], O_RDONLY); if(src_fd < 0) { - printf("Unable to open %s\n", argv[1]); + printf("Unable to open %s\n", arguments.args[0]); perror(""); exit(1); }