make option validation optional

This commit is contained in:
Lukas Wurzinger 2022-12-11 14:10:42 +01:00
parent 9a694f3ff5
commit 18fefa2aaa
5 changed files with 80 additions and 63 deletions

122
readarg.h
View file

@ -77,6 +77,10 @@ struct readarg_helpgen_writer {
int readarg_parse(struct readarg_parser *rp);
/* args should always exclude the first element. */
void readarg_parser_init(struct readarg_parser *rp, struct readarg_opt *opts, struct readarg_arg *opers, struct readarg_view_strings args);
/* Assign operands from the operand list to operands defined for the parser. */
void readarg_assign_opers(struct readarg_parser *rp);
/* Validate that all options meet their requirements. */
struct readarg_opt *readarg_validate_opts(struct readarg_parser *rp);
/* Check whether the argument is a valid option. */
int readarg_validate_opt(struct readarg_opt *opt);
/* Check whether the argument is a valid argument. */
@ -109,8 +113,6 @@ static struct readarg_opt *readarg_match_finish(struct readarg_parser *rp, const
static void readarg_update_opt(struct readarg_parser *rp, const char *attach, struct readarg_opt *opt);
static void readarg_update_oper(struct readarg_parser *rp, struct readarg_view_strings val);
static void readarg_assign_opers(struct readarg_parser *rp);
static void readarg_add_val(struct readarg_parser *rp, struct readarg_arg *arg, const char *string, int end);
static const char *readarg_skip_incl(const char *outer, const char *inner);
@ -121,6 +123,65 @@ static void readarg_permute_val(struct readarg_parser *rp, struct readarg_view_s
static void readarg_incr_between(const char **start, const char **stop, struct readarg_view_strings *curr, struct readarg_view_strings *exclude);
static void readarg_permute_rest(const char **target, struct readarg_view_strings start);
struct readarg_opt *readarg_validate_opts(struct readarg_parser *rp) {
for (size_t i = 0; readarg_validate_opt(rp->opts + i); i++) {
if (!readarg_validate_within(&rp->opts[i].arg)) {
rp->error = READARG_ERANGEOPT;
return &rp->opts[i];
}
}
return NULL;
}
void readarg_assign_opers(struct readarg_parser *rp) {
size_t count = rp->state.curr.ioper.len;
size_t nlower = 0;
size_t nupper = 0;
for (size_t i = 0; readarg_validate_arg(rp->opers + i); i++) {
nlower += readarg_select_lower(rp->opers[i].bounds);
nupper += readarg_select_upper(rp->opers[i].bounds);
}
if (count < nlower) {
rp->error = READARG_ERANGEOPER;
return;
}
struct {
size_t extra;
size_t req;
} rest = {
count - nlower,
nlower,
};
for (size_t i = 0; readarg_validate_arg(rp->opers + i); i++) {
if (count == 0 || !rp->opers[i].val.strings) {
size_t off = count - (rest.extra + rest.req);
rp->opers[i].val.strings = rp->state.curr.ioper.strings + off;
}
size_t lower = readarg_select_lower(rp->opers[i].bounds);
size_t upper = readarg_select_upper(rp->opers[i].bounds);
int inf = rp->opers[i].bounds.inf;
size_t add;
/* Add required elements. */
add = rest.req > lower ? lower : rest.req;
rp->opers[i].val.len += add, rest.req -= add;
/* Add optional elements. */
add = inf ? rest.extra : rest.extra > upper ? upper : rest.extra;
rp->opers[i].val.len += add, rest.extra -= add;
}
if (rest.extra || rest.req)
rp->error = READARG_ERANGEOPER;
}
int readarg_parse(struct readarg_parser *rp) {
/* Check whether the current offset is at the end of argv. */
size_t off = rp->state.curr.arg - rp->args.strings;
@ -128,17 +189,8 @@ int readarg_parse(struct readarg_parser *rp) {
if (rp->state.pending) {
/* The last specified option required an argument, but no argument has been provided. */
rp->error = READARG_ENOVAL;
return 0;
}
for (size_t i = 0; readarg_validate_opt(rp->opts + i); i++) {
if (!readarg_validate_within(&rp->opts[i].arg)) {
rp->error = READARG_ERANGEOPT;
return 0;
}
}
readarg_assign_opers(rp);
return 0;
}
@ -331,54 +383,6 @@ static void readarg_update_oper(struct readarg_parser *rp, struct readarg_view_s
}
}
static void readarg_assign_opers(struct readarg_parser *rp) {
size_t count = rp->state.curr.ioper.len;
size_t nlower = 0;
size_t nupper = 0;
for (size_t i = 0; readarg_validate_arg(rp->opers + i); i++) {
nlower += readarg_select_lower(rp->opers[i].bounds);
nupper += readarg_select_upper(rp->opers[i].bounds);
}
if (count < nlower) {
rp->error = READARG_ERANGEOPER;
return;
}
struct {
size_t extra;
size_t req;
} rest = {
count - nlower,
nlower,
};
for (size_t i = 0; readarg_validate_arg(rp->opers + i); i++) {
if (count == 0 || !rp->opers[i].val.strings) {
size_t off = count - (rest.extra + rest.req);
rp->opers[i].val.strings = rp->state.curr.ioper.strings + off;
}
size_t lower = readarg_select_lower(rp->opers[i].bounds);
size_t upper = readarg_select_upper(rp->opers[i].bounds);
int inf = rp->opers[i].bounds.inf;
size_t add;
/* Add required elements. */
add = rest.req > lower ? lower : rest.req;
rp->opers[i].val.len += add, rest.req -= add;
/* Add optional elements. */
add = inf ? rest.extra : rest.extra > upper ? upper : rest.extra;
rp->opers[i].val.len += add, rest.extra -= add;
}
if (rest.extra || rest.req)
rp->error = READARG_ERANGEOPER;
}
static void readarg_add_val(struct readarg_parser *rp, struct readarg_arg *arg, const char *string, int end) {
rp->state.pending = 0;

5
test/help.bash Executable file
View file

@ -0,0 +1,5 @@
#!/usr/bin/env bash
cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null
./test -e a b c --help

View file

@ -34,7 +34,3 @@ cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null
x \
y \
z
./test -e a b c --help
./test -e a b c --version

View file

@ -147,6 +147,13 @@ int main(int argc, char **argv) {
return 1;
}
struct readarg_opt *erropt = readarg_validate_opts(&rp);
if (erropt != NULL) {
fprintf(stderr, "Error: %d\n", rp.error);
readarg_helpgen_put_usage(&rp, &writer, progname, "Usage");
return 1;
}
if (rp.opts[OPT_HELP].arg.val.len >= 1) {
readarg_helpgen_put_usage(&rp, &writer, progname, "Usage");
return 0;

5
test/version.bash Executable file
View file

@ -0,0 +1,5 @@
#!/usr/bin/env bash
cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null
./test -e a b c --version