1

親切に助けてください。私はオンラインマニュアルを隅々まで調べました...それでも、何が悪いのかヒントはありません。

問題は、option_indexがgetopt_long()によって更新されないため、 long_options[option_index].nameなどの形式で適切な構造体メンバーにアクセスできないことです。

#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;
}

出力:

# ~/workspace/Test/Debug/Test -f 1 -s -t third
option first with arg 1
option first
option first with arg third
  • 実行中に option_index が変更されないことをデバッガーで確認しました。

事前に感謝します。アイデア/リードをいただければ幸いです。

4

0 に答える 0