Welcome to little lamb

Code » limb » master » tree

[master] / src / liblimb / parseopt.h / parseopt_warn.c

/* This file is part of limb                           https://lila.oss/limb
 * Copyright (C) 2023 Olivier Brunel                          jjk@jjacky.com */
/* SPDX-License-Identifier: GPL-2.0-only */
#include <errno.h>
#include <limb/bytestr.h>
#include <limb/output.h>
#include <limb/parseopt.h>
#include "parseopt.h"

void
parseopt_warn(const char **argv, const struct option *options,
              const struct parseopt *ctx)
{
    switch (errno) {
        case EINVAL:
            warn("option name missing");
            break;
        case ENOENT:
            if (!strncmp(argv[ctx->cur], "--", 2)) {
                warn("unknown option: ", argv[ctx->cur]);
                /* was there a partial match? */
                if (ctx->idx >= 0) {
                    const char *s = argv[ctx->cur] + ctx->off;
                    size_t l = byte_chr(s, strlen(s), '=');
                    list_matches_full(obuffer_2, OLVL_NORMAL, "did you mean ",
                                      "--", " or ", " ?", s, l, ctx->idx, options,
                                      offsetof(struct option, longopt), sizeof(*options));
                }
            } else {
                char buf[3] = { '-', argv[ctx->cur][ctx->off], 0 };
                warn("unknown option: ", buf);
            }
            break;
        case ENOMSG:
            {
                char buf[2] = { options[ctx->idx].shortopt, 0 };
                warn("option --", options[ctx->idx].longopt,
                     (*buf) ? "/-" : "", buf, " requires an argument");
            }
            break;
    }
}