From c539a19833ba93df88f67a878b1d68fc468fb88d Mon Sep 17 00:00:00 2001 From: dave Date: Sun, 22 Jul 2018 15:14:07 -0700 Subject: [PATCH] add 'arg required' flag to argparse --- src/main.c | 4 ++-- src/vendor/argparse.c | 33 ++++++++++++++++++++++++--------- src/vendor/argparse.h | 9 +++++---- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/main.c b/src/main.c index 8534f7a..9ee0158 100644 --- a/src/main.c +++ b/src/main.c @@ -13,8 +13,8 @@ int main(int argc, const char** argv) { struct argparse_option options[] = { OPT_HELP(), - OPT_INTEGER('p', "port", &port, "server port"), - OPT_STRING ('u', "url", &url, "elasticsearch url"), + OPT_INTEGER('p', "port", &port, "server port", .flags = OPT_REQUIRED), + OPT_STRING ('u', "url", &url, "elasticsearch url", .flags = OPT_REQUIRED), OPT_END() }; diff --git a/src/vendor/argparse.c b/src/vendor/argparse.c index 92ec391..1f8da93 100644 --- a/src/vendor/argparse.c +++ b/src/vendor/argparse.c @@ -34,7 +34,7 @@ prefix_cmp(const char *str, const char *prefix) } static void -argparse_error(struct argparse *self, const struct argparse_option *opt, +argparse_error(struct argparse *self, struct argparse_option *opt, const char *reason, int flags) { (void)self; @@ -47,7 +47,7 @@ argparse_error(struct argparse *self, const struct argparse_option *opt, } static int -argparse_getvalue(struct argparse *self, const struct argparse_option *opt, +argparse_getvalue(struct argparse *self, struct argparse_option *opt, int flags) { const char *s = NULL; @@ -118,6 +118,8 @@ argparse_getvalue(struct argparse *self, const struct argparse_option *opt, assert(0); } + opt->flags &= ~OPT_REQUIRED; + skipped: if (opt->callback) { return opt->callback(self, opt); @@ -127,7 +129,7 @@ skipped: } static void -argparse_options_check(const struct argparse_option *options) +argparse_options_check(struct argparse_option *options) { for (; options->type != ARGPARSE_OPT_END; options++) { switch (options->type) { @@ -147,7 +149,7 @@ argparse_options_check(const struct argparse_option *options) } static int -argparse_short_opt(struct argparse *self, const struct argparse_option *options) +argparse_short_opt(struct argparse *self, struct argparse_option *options) { for (; options->type != ARGPARSE_OPT_END; options++) { if (options->short_name == *self->optvalue) { @@ -159,7 +161,7 @@ argparse_short_opt(struct argparse *self, const struct argparse_option *options) } static int -argparse_long_opt(struct argparse *self, const struct argparse_option *options) +argparse_long_opt(struct argparse *self, struct argparse_option *options) { for (; options->type != ARGPARSE_OPT_END; options++) { const char *rest; @@ -218,6 +220,17 @@ argparse_describe(struct argparse *self, const char *description, self->epilog = epilog; } +void +argparse_check_required(struct argparse* self) { + struct argparse_option *opt; + opt = self->options; + for (; opt->type != ARGPARSE_OPT_END; opt++) { + if(opt->flags & OPT_REQUIRED) { + argparse_error(self, opt, "is required", OPT_LONG); + } + } +} + int argparse_parse(struct argparse *self, int argc, const char **argv) { @@ -282,6 +295,8 @@ end: self->argc * sizeof(*self->out)); self->out[self->cpidx + self->argc] = NULL; + argparse_check_required(self); + return self->cpidx + self->argc; } @@ -302,13 +317,13 @@ argparse_usage(struct argparse *self) fputc('\n', stdout); - const struct argparse_option *options; + struct argparse_option *options; // figure out best width size_t usage_opts_width = 0; - size_t len; options = self->options; for (; options->type != ARGPARSE_OPT_END; options++) { + size_t len; len = 0; if ((options)->short_name) { len += 2; @@ -321,7 +336,7 @@ argparse_usage(struct argparse *self) } if (options->type == ARGPARSE_OPT_INTEGER) { len += strlen("="); - } + } if (options->type == ARGPARSE_OPT_FLOAT) { len += strlen("="); } else if (options->type == ARGPARSE_OPT_STRING) { @@ -377,7 +392,7 @@ argparse_usage(struct argparse *self) } int -argparse_help_cb(struct argparse *self, const struct argparse_option *option) +argparse_help_cb(struct argparse *self, struct argparse_option *option) { (void)option; argparse_usage(self); diff --git a/src/vendor/argparse.h b/src/vendor/argparse.h index 7fff31d..dfad4b0 100644 --- a/src/vendor/argparse.h +++ b/src/vendor/argparse.h @@ -19,7 +19,7 @@ struct argparse; struct argparse_option; typedef int argparse_callback (struct argparse *self, - const struct argparse_option *option); + struct argparse_option *option); enum argparse_flag { ARGPARSE_STOP_AT_NON_OPTION = 1, @@ -39,7 +39,8 @@ enum argparse_option_type { }; enum argparse_option_flags { - OPT_NONEG = 1, /* disable negation */ + OPT_NONEG = 0x01, /* disable negation */ + OPT_REQUIRED = 0x02, /* opt must be passed */ }; /** @@ -87,7 +88,7 @@ struct argparse_option { */ struct argparse { // user supplied - const struct argparse_option *options; + struct argparse_option *options; const char *const *usages; int flags; const char *description; // a description after usage @@ -102,7 +103,7 @@ struct argparse { // built-in callbacks int argparse_help_cb(struct argparse *self, - const struct argparse_option *option); + struct argparse_option *option); // built-in option macros #define OPT_END() { ARGPARSE_OPT_END, 0, NULL, NULL, 0, NULL, 0, 0 }