-
-ARGPAR_HIDDEN
-struct argpar_parse_ret argpar_parse(const unsigned int argc,
- const char * const * const argv,
- const struct argpar_opt_descr * const descrs,
- const bool fail_on_unknown_opt)
-{
- struct argpar_parse_ret parse_ret = { 0 };
- const struct argpar_item *item = NULL;
- struct argpar_iter *iter = NULL;
-
- parse_ret.items = new_item_array();
- if (!parse_ret.items) {
- parse_ret.error = strdup("Failed to create items array.");
- ARGPAR_ASSERT(parse_ret.error);
- goto error;
- }
-
- iter = argpar_iter_create(argc, argv, descrs);
- if (!iter) {
- parse_ret.error = strdup("Failed to create argpar iter.");
- ARGPAR_ASSERT(parse_ret.error);
- goto error;
- }
-
- while (true) {
- enum argpar_iter_parse_next_status status;
-
- status = argpar_iter_parse_next(iter, &item, &parse_ret.error);
- if (status == ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR) {
- goto error;
- } else if (status == ARGPAR_ITER_PARSE_NEXT_STATUS_END) {
- break;
- } else if (status == ARGPAR_ITER_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT) {
- if (fail_on_unknown_opt) {
- parse_ret.ingested_orig_args =
- argpar_iter_get_ingested_orig_args(iter);
- goto error;
- }
-
- free(parse_ret.error);
- parse_ret.error = NULL;
- break;
- }
-
- ARGPAR_ASSERT(status == ARGPAR_ITER_PARSE_NEXT_STATUS_OK);
-
- if (!push_item(parse_ret.items, (void *) item)) {
- goto error;
- }
-
- item = NULL;
- }
-
- ARGPAR_ASSERT(!parse_ret.error);
- parse_ret.ingested_orig_args =
- argpar_iter_get_ingested_orig_args(iter);
- goto end;
-
-error:
- ARGPAR_ASSERT(parse_ret.error);
-
- /* That's how we indicate that an error occurred */
- destroy_item_array(parse_ret.items);
- parse_ret.items = NULL;
-
-end:
- argpar_iter_destroy(iter);
- argpar_item_destroy(item);
- return parse_ret;
-}
-
-ARGPAR_HIDDEN
-void argpar_parse_ret_fini(struct argpar_parse_ret *ret)
-{
- ARGPAR_ASSERT(ret);
-
- destroy_item_array(ret->items);
- ret->items = NULL;
-
- free(ret->error);
- ret->error = NULL;
-}