Newer
Older
emul / emul_opt.c
@takayun takayun on 24 May 2016 1 KB add option
#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;
    while (1) {
        c = getopt_long(argc, argv, "f:vh", long_options, &option_index);
        if (c == -1) return 0;
        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;
        }
    }
    return 1;
}