// Written by retoor@molodetz.nl

// This source code implements a simple argument parsing utility for C programs,
// facilitating the checking or retrieval of command line argument values.

// This file uses the standard C libraries: <stdio.h>, <string.h>, <stdlib.h>, <stdbool.h>

// MIT License

#ifndef RLIB_RARGS_H
#define RLIB_RARGS_H

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>

bool rargs_isset(int argc, char *argv[], char *key) {
    for (int i = 0; i < argc; i++) {
        if (strcmp(argv[i], key) == 0) {
            return true;
        }
    }
    return false;
}

char *rargs_get_option_string(int argc, char *argv[], char *key, const char *def) {
    for (int i = 0; i < argc; i++) {
        if (strcmp(argv[i], key) == 0) {
            if (i < argc - 1) {
                return argv[i + 1];
            }
        }
    }
    return (char *)def;
}

int rargs_get_option_int(int argc, char *argv[], char *key, int def) {
    for (int i = 0; i < argc; i++) {
        if (strcmp(argv[i], key) == 0) {
            if (i < argc - 1) {
                return atoi(argv[i + 1]);
            }
        }
    }
    return def;
}

bool rargs_get_option_bool(int argc, char *argv[], char *key, bool def) {
    for (int i = 0; i < argc; i++) {
        if (strcmp(argv[i], key) == 0) {
            if (i < argc - 1) {
                if (strcmp(argv[i + 1], "false") == 0 || strcmp(argv[i + 1], "0") == 0) {
                    return false;
                }
                return true;
            }
        }
    }
    return def;
}

#endif