+enum parse_next_item_status
+{
+ PARSE_NEXT_ITEM_STATUS_OK = 0,
+ PARSE_NEXT_ITEM_STATUS_END = 1,
+ PARSE_NEXT_ITEM_STATUS_ERROR = -1,
+};
+
+/*
+ * Parse the next item using `iter`. Log and append an error if necessary.
+ *
+ * The item in `*item` on entry is freed, and the new item is also
+ * returned in `*item`.
+ */
+static
+enum parse_next_item_status parse_next_item(struct argpar_iter *iter,
+ const struct argpar_item **item, const char **argv,
+ const char *command, unsigned int consumed_args)
+{
+ enum argpar_iter_next_status status;
+ const struct argpar_error *error = NULL;
+ enum parse_next_item_status ret;
+
+ ARGPAR_ITEM_DESTROY_AND_RESET(*item);
+ status = argpar_iter_next(iter, item, &error);
+
+ switch (status) {
+ case ARGPAR_ITER_NEXT_STATUS_ERROR_MEMORY:
+ BT_CLI_LOGE_APPEND_CAUSE_OOM();
+ ret = PARSE_NEXT_ITEM_STATUS_ERROR;
+ break;
+ case ARGPAR_ITER_NEXT_STATUS_ERROR:
+ {
+ GString *err_str = format_arg_error(error, argv,
+ consumed_args,
+ "While parsing `%s` command's command-line arguments",
+ command);
+ BT_CLI_LOGE_APPEND_CAUSE("%s", err_str->str);
+ g_string_free(err_str, TRUE);
+ ret = PARSE_NEXT_ITEM_STATUS_ERROR;
+ break;
+ }
+ case ARGPAR_ITER_NEXT_STATUS_END:
+ ret = PARSE_NEXT_ITEM_STATUS_END;
+ break;
+ case ARGPAR_ITER_NEXT_STATUS_OK:
+ ret = PARSE_NEXT_ITEM_STATUS_OK;
+ break;
+ default:
+ bt_common_abort();
+ }
+
+ argpar_error_destroy(error);
+ return ret;
+}
+
+