1

kindly help. I've gone all over online manuals... yet, no hint whats wrong.

The problem is that option_index does not get updated by getopt_long(), thus I'm unable to access proper struct members in form of long_options[option_index].name etc.

#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>

int main(int argc, char** argv) {

    int opt=0;
    int option_index=0;
    struct option long_options[]={
        {"first",   required_argument,  NULL,   'f'},
        {"second",  no_argument,        NULL,   's'},
        {"third",   required_argument,  NULL,   't'},
        {NULL,      0,                  NULL,   0}
    };

    while (1){
        option_index=0;
        opt=getopt_long (argc,argv, "f:st:", long_options, &option_index);
        if ( opt == -1 )
            break;

        printf("option %s", long_options[option_index].name);
        if (optarg)
            printf(" with arg %s", optarg);
        printf("\n");
    }

    return EXIT_SUCCESS;
}

Output :

# ~/workspace/Test/Debug/Test -f 1 -s -t third
option first with arg 1
option first
option first with arg third
  • Verified with debugger that option_index left unchanged during entire execution.

Thanks in advance, any idea / lead will be appreciated !

4

0 回答 0