+int set_error(argpar_error_t ** const error, argpar_error_type_t type,
+ const char * const unknown_opt_name,
+ const argpar_opt_descr_t * const opt_descr, const bool is_short)
+{
+ int ret = 0;
+
+ if (!error) {
+ goto end;
+ }
+
+ *error = ARGPAR_ZALLOC(argpar_error_t);
+ if (!*error) {
+ goto error;
+ }
+
+ (*error)->type = type;
+
+ if (unknown_opt_name) {
+ (*error)->unknown_opt_name = ARGPAR_CALLOC(char,
+ strlen(unknown_opt_name) + 1 + (is_short ? 1 : 2));
+ if (!(*error)->unknown_opt_name) {
+ goto error;
+ }
+
+ if (is_short) {
+ strcpy((*error)->unknown_opt_name, "-");
+ } else {
+ strcpy((*error)->unknown_opt_name, "--");
+ }
+
+ strcat((*error)->unknown_opt_name, unknown_opt_name);
+ }
+
+ (*error)->opt_descr = opt_descr;
+ (*error)->is_short = is_short;
+ goto end;
+
+error:
+ argpar_error_destroy(*error);
+ ret = -1;
+
+end:
+ return ret;
+}
+
+ARGPAR_HIDDEN
+argpar_error_type_t argpar_error_type(
+ const argpar_error_t * const error)
+{
+ ARGPAR_ASSERT(error);
+ return error->type;
+}
+
+ARGPAR_HIDDEN
+unsigned int argpar_error_orig_index(const argpar_error_t * const error)
+{
+ ARGPAR_ASSERT(error);
+ return error->orig_index;
+}
+
+ARGPAR_HIDDEN
+const char *argpar_error_unknown_opt_name(
+ const argpar_error_t * const error)
+{
+ ARGPAR_ASSERT(error);
+ ARGPAR_ASSERT(error->type == ARGPAR_ERROR_TYPE_UNKNOWN_OPT);
+ ARGPAR_ASSERT(error->unknown_opt_name);
+ return error->unknown_opt_name;
+}
+
+ARGPAR_HIDDEN
+const argpar_opt_descr_t *argpar_error_opt_descr(
+ const argpar_error_t * const error, bool * const is_short)
+{
+ ARGPAR_ASSERT(error);
+ ARGPAR_ASSERT(error->type == ARGPAR_ERROR_TYPE_MISSING_OPT_ARG ||
+ error->type == ARGPAR_ERROR_TYPE_UNEXPECTED_OPT_ARG);
+ ARGPAR_ASSERT(error->opt_descr);
+
+ if (is_short) {
+ *is_short = error->is_short;
+ }
+
+ return error->opt_descr;
+}
+
+ARGPAR_HIDDEN
+void argpar_error_destroy(const argpar_error_t * const error)
+{
+ if (error) {
+ free(error->unknown_opt_name);
+ free((void *) error);
+ }
+}
+
+/*
+ * Finds and returns the _first_ descriptor having the short option name
+ * `short_name` or the long option name `long_name` within the option
+ * descriptors `descrs`.
+ *
+ * `short_name` may be `'\0'` to not consider it.
+ *
+ * `long_name` may be `NULL` to not consider it.
+ *
+ * Returns `NULL` if no descriptor is found.
+ */
+static
+const argpar_opt_descr_t *find_descr(const argpar_opt_descr_t * const descrs,