add 'arg required' flag to argparse
This commit is contained in:
parent
8c508ad7d1
commit
c539a19833
|
@ -13,8 +13,8 @@ int main(int argc, const char** argv) {
|
||||||
|
|
||||||
struct argparse_option options[] = {
|
struct argparse_option options[] = {
|
||||||
OPT_HELP(),
|
OPT_HELP(),
|
||||||
OPT_INTEGER('p', "port", &port, "server port"),
|
OPT_INTEGER('p', "port", &port, "server port", .flags = OPT_REQUIRED),
|
||||||
OPT_STRING ('u', "url", &url, "elasticsearch url"),
|
OPT_STRING ('u', "url", &url, "elasticsearch url", .flags = OPT_REQUIRED),
|
||||||
OPT_END()
|
OPT_END()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -34,7 +34,7 @@ prefix_cmp(const char *str, const char *prefix)
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
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)
|
const char *reason, int flags)
|
||||||
{
|
{
|
||||||
(void)self;
|
(void)self;
|
||||||
|
@ -47,7 +47,7 @@ argparse_error(struct argparse *self, const struct argparse_option *opt,
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
argparse_getvalue(struct argparse *self, const struct argparse_option *opt,
|
argparse_getvalue(struct argparse *self, struct argparse_option *opt,
|
||||||
int flags)
|
int flags)
|
||||||
{
|
{
|
||||||
const char *s = NULL;
|
const char *s = NULL;
|
||||||
|
@ -118,6 +118,8 @@ argparse_getvalue(struct argparse *self, const struct argparse_option *opt,
|
||||||
assert(0);
|
assert(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
opt->flags &= ~OPT_REQUIRED;
|
||||||
|
|
||||||
skipped:
|
skipped:
|
||||||
if (opt->callback) {
|
if (opt->callback) {
|
||||||
return opt->callback(self, opt);
|
return opt->callback(self, opt);
|
||||||
|
@ -127,7 +129,7 @@ skipped:
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
argparse_options_check(const struct argparse_option *options)
|
argparse_options_check(struct argparse_option *options)
|
||||||
{
|
{
|
||||||
for (; options->type != ARGPARSE_OPT_END; options++) {
|
for (; options->type != ARGPARSE_OPT_END; options++) {
|
||||||
switch (options->type) {
|
switch (options->type) {
|
||||||
|
@ -147,7 +149,7 @@ argparse_options_check(const struct argparse_option *options)
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
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++) {
|
for (; options->type != ARGPARSE_OPT_END; options++) {
|
||||||
if (options->short_name == *self->optvalue) {
|
if (options->short_name == *self->optvalue) {
|
||||||
|
@ -159,7 +161,7 @@ argparse_short_opt(struct argparse *self, const struct argparse_option *options)
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
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++) {
|
for (; options->type != ARGPARSE_OPT_END; options++) {
|
||||||
const char *rest;
|
const char *rest;
|
||||||
|
@ -218,6 +220,17 @@ argparse_describe(struct argparse *self, const char *description,
|
||||||
self->epilog = epilog;
|
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
|
int
|
||||||
argparse_parse(struct argparse *self, int argc, const char **argv)
|
argparse_parse(struct argparse *self, int argc, const char **argv)
|
||||||
{
|
{
|
||||||
|
@ -282,6 +295,8 @@ end:
|
||||||
self->argc * sizeof(*self->out));
|
self->argc * sizeof(*self->out));
|
||||||
self->out[self->cpidx + self->argc] = NULL;
|
self->out[self->cpidx + self->argc] = NULL;
|
||||||
|
|
||||||
|
argparse_check_required(self);
|
||||||
|
|
||||||
return self->cpidx + self->argc;
|
return self->cpidx + self->argc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -302,13 +317,13 @@ argparse_usage(struct argparse *self)
|
||||||
|
|
||||||
fputc('\n', stdout);
|
fputc('\n', stdout);
|
||||||
|
|
||||||
const struct argparse_option *options;
|
struct argparse_option *options;
|
||||||
|
|
||||||
// figure out best width
|
// figure out best width
|
||||||
size_t usage_opts_width = 0;
|
size_t usage_opts_width = 0;
|
||||||
size_t len;
|
|
||||||
options = self->options;
|
options = self->options;
|
||||||
for (; options->type != ARGPARSE_OPT_END; options++) {
|
for (; options->type != ARGPARSE_OPT_END; options++) {
|
||||||
|
size_t len;
|
||||||
len = 0;
|
len = 0;
|
||||||
if ((options)->short_name) {
|
if ((options)->short_name) {
|
||||||
len += 2;
|
len += 2;
|
||||||
|
@ -321,7 +336,7 @@ argparse_usage(struct argparse *self)
|
||||||
}
|
}
|
||||||
if (options->type == ARGPARSE_OPT_INTEGER) {
|
if (options->type == ARGPARSE_OPT_INTEGER) {
|
||||||
len += strlen("=<int>");
|
len += strlen("=<int>");
|
||||||
}
|
}
|
||||||
if (options->type == ARGPARSE_OPT_FLOAT) {
|
if (options->type == ARGPARSE_OPT_FLOAT) {
|
||||||
len += strlen("=<flt>");
|
len += strlen("=<flt>");
|
||||||
} else if (options->type == ARGPARSE_OPT_STRING) {
|
} else if (options->type == ARGPARSE_OPT_STRING) {
|
||||||
|
@ -377,7 +392,7 @@ argparse_usage(struct argparse *self)
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
argparse_help_cb(struct argparse *self, const struct argparse_option *option)
|
argparse_help_cb(struct argparse *self, struct argparse_option *option)
|
||||||
{
|
{
|
||||||
(void)option;
|
(void)option;
|
||||||
argparse_usage(self);
|
argparse_usage(self);
|
||||||
|
|
|
@ -19,7 +19,7 @@ struct argparse;
|
||||||
struct argparse_option;
|
struct argparse_option;
|
||||||
|
|
||||||
typedef int argparse_callback (struct argparse *self,
|
typedef int argparse_callback (struct argparse *self,
|
||||||
const struct argparse_option *option);
|
struct argparse_option *option);
|
||||||
|
|
||||||
enum argparse_flag {
|
enum argparse_flag {
|
||||||
ARGPARSE_STOP_AT_NON_OPTION = 1,
|
ARGPARSE_STOP_AT_NON_OPTION = 1,
|
||||||
|
@ -39,7 +39,8 @@ enum argparse_option_type {
|
||||||
};
|
};
|
||||||
|
|
||||||
enum argparse_option_flags {
|
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 {
|
struct argparse {
|
||||||
// user supplied
|
// user supplied
|
||||||
const struct argparse_option *options;
|
struct argparse_option *options;
|
||||||
const char *const *usages;
|
const char *const *usages;
|
||||||
int flags;
|
int flags;
|
||||||
const char *description; // a description after usage
|
const char *description; // a description after usage
|
||||||
|
@ -102,7 +103,7 @@ struct argparse {
|
||||||
|
|
||||||
// built-in callbacks
|
// built-in callbacks
|
||||||
int argparse_help_cb(struct argparse *self,
|
int argparse_help_cb(struct argparse *self,
|
||||||
const struct argparse_option *option);
|
struct argparse_option *option);
|
||||||
|
|
||||||
// built-in option macros
|
// built-in option macros
|
||||||
#define OPT_END() { ARGPARSE_OPT_END, 0, NULL, NULL, 0, NULL, 0, 0 }
|
#define OPT_END() { ARGPARSE_OPT_END, 0, NULL, NULL, 0, NULL, 0, 0 }
|
||||||
|
|
Loading…
Reference in New Issue