Newer
Older
emul / emul_opt.c
#include "emul_opt.h"
#include <getopt.h>
#include <stdio.h>

#define EMUL_APP_VERSION "alpha 1.0.0"

static struct option long_options[] = {
    {"version", no_argument, NULL, 'v'},
    {"help", no_argument, NULL, 'h'},
    {"file", required_argument, NULL, 'f'},
    {0, 0, 0, 0}
};

static void (*event_fa)(char*);

void emul_opt_event_fa(void (*func)(char*)) {
    event_fa = func;
}

int emul_opt(int argc, char *argv[]) {
    int c;
    int digit_optind = 0;
    int this_option_optind = optind ? optind : 1;
    int option_index = 0;
    int option_count = 0;
    while (1) {
        c = getopt_long(argc, argv, "f:vh", long_options, &option_index);
        if (c == -1) break;
        switch (c) {
            case 0:
                printf("option %s", long_options[option_index].name);
                if (optarg) printf(" with arg %s", optarg);
                break;
            case 'f':
                event_fa(optarg);
                break;
            case 'v':
                printf("%s is version %s\n", argv[0], EMUL_APP_VERSION);
                break;
            case 'h':
                printf("usage: %s [option]\n", argv[0]);
                printf("   -f ,\t--file <file>\tLoad and Execute program from input file\n");
                printf("   -v ,\t--version\tDisplay version\n");
                printf("   -h ,\t--help\t\tDisplay help\n");
                break;
        }
        option_count++;
    }
    return option_count;
}