diff options
Diffstat (limited to 'src/command.c')
| -rw-r--r-- | src/command.c | 64 |
1 files changed, 34 insertions, 30 deletions
diff --git a/src/command.c b/src/command.c index 6be2fb5..2f91db0 100644 --- a/src/command.c +++ b/src/command.c @@ -199,41 +199,45 @@ void l2_cmd_free_ctx(struct l2_context_node *ctx) } while (ctx); } -#if 0 -#include "uuid/uuid.h" -#include <inttypes.h> - -int main(int argc, char **argv) +void l2_cmd_collect_args(struct l2_context_node *ctx, unsigned argc, ...) { - char **argvcur = argv + 1; - - if (!*argvcur) { - printf("done\n"); - return 1; + va_list ap; + if (argc == 0) return; + + struct tag_cmd_arg_t { + const char *name; + char **target; + bool optional; + } args[argc]; /* ooooo c99 VLA this is exciting */ + + va_start(ap, argc); + + for (unsigned i = 0; i < argc; ++i) { + args[i].name = va_arg(ap, const char *); + args[i].target = va_arg(ap, char **); + if (*args[i].name == '#') { + ++args[i].name; + args[i].optional = true; + } else { + args[i].optional = false; + } } - ((void)argc); - - struct l2_context_node *cur; - struct l2_parseinfo parseinfo; - memset(&parseinfo, 0, sizeof(struct l2_parseinfo)); - - unsigned res = l2_cmd_parse_command(argv + 1, &parseinfo); - - printf("%p %s\n", (void *)parseinfo.ctx, parseinfo.argv ? *parseinfo.argv : "(null)"); + va_end(ap); - if (res == CMD_PARSE_SUCCESS) { - cur = parseinfo.ctx; - while (cur) { - printf("%s\n", cur->value); - cur = cur->next; + for (struct l2_context_node *cur = ctx; cur; cur = cur->next) { + if (cur->node->type != CMD_NODE_TYPE_ARGUMENT) continue; + for (unsigned i = 0; i < argc; ++i) { + if (!strcmp(cur->node->name, args[i].name)) { + *args[i].target = cur->value; + args[i].optional = true; + } } - - printf("command returned: %u\n", (parseinfo.proc)(parseinfo.ctx, parseinfo.argv)); - - l2_cmd_free_ctx(parseinfo.ctx); } - return 0; + for (unsigned i = 0; i < argc; ++i) { + if (!args[i].optional) { + CMD_FATAL(CMD_MSG_REQ_ARG_UNSPECIFIED, args[i].name); + } + } } -#endif |
