簡體   English   中英

C:getopt():選項似乎無效

[英]C: getopt(): option does not seem to have effect

我正在嘗試以下C代碼(保存在名為testgetopt.c的文件中):

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

void usage(char *s)
{
    fprintf(stderr, "Usage: %s [-d] -i input-TIFF-file -o output-raw-file\n", s);
    fprintf(stderr, "Note: -d indicates debugging option\n");
}

int main(int argc, char **argv) { 
    char c;
    size_t i;
    char *itext = NULL, *otext = NULL;
    FILE *fout;
    short debug = 0;

    if ((c = getopt(argc, argv, "di:o:")) == EOF) {
        usage(argv[0]);
        exit(1);
    }

    while ((c = getopt(argc, argv, "di:o:")) != EOF) {
        switch (c) {
        case 'd':
            debug = 1;
            break;
        case 'i':
            itext = optarg;
            break;
        case 'o':
            otext = optarg;
            break;
        case '?':
            if (optopt == 'i' || optopt == 'o')
                fprintf (stderr, "Option -%c requires an argument.\n", optopt);
            else if (isprint (optopt))
                fprintf (stderr, "Unknown option `-%c'.\n", optopt);
            else
                fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt);
            usage(argv[0]);
            exit(1);
        default:
            fprintf(stderr, "Both -i and -o are needed\n");
            usage(argv[0]);
            exit(1);
        }
    }
    printf("debug = %i\n", debug);
    if (debug) 
        printf ("input file = %s, output file = %s\n", itext, otext);
    return EXIT_SUCCESS;
}

我編譯使用:

gcc testgetopt.c 

但是,無論是否包含-d,我總是將調試設置為0。否則,程序應將-d調試設置為1,否則為0。

做錯了什么?

以下是示例:

讓我們先嘗試一下-d

./a.out -i in.dat -o out.dat
debug = 0

讓我們接下來嘗試使用-d。

./a.out -d -i in.dat -o out.dat
debug = 0

預先感謝您的任何幫助和建議!

問題是您將getopt稱為完整性檢查,然后在成功時不使用其結果。 這意味着第一個選項總是被丟棄。 您可以通過將-d作為非第一選項來輕松驗證這一點:

$ ./a.out -i in.dat -o out.dat -d
debug = 1
input file = (null), output file = out.dat

您可以看到-d這次確實生效了,但是沒有設置第一個參數-i

有幾種方法可以解決此問題。 推薦的方法(IMHO)是刪除第一個getopt調用。 然后更改完整性檢查以驗證實際選項。 也就是說,在getopt循環之后應該有代碼來驗證是否已提供所有必需的選項。

例如:

int main(int argc, char **argv) { 
    char c;
    size_t i;
    char *itext = NULL, *otext = NULL;
    FILE *fout;
    short debug = 0;

    /* REMOVED THIS BLOCK OF CODE
    if ((c = getopt(argc, argv, "di:o:")) == EOF) {
        usage(argv[0]);
        exit(1);
    }
    */

    while ((c = getopt(argc, argv, "di:o:")) != EOF) {
        switch (c) {
        case 'd':
            debug = 1;
            break;
        case 'i':
            itext = optarg;
            break;
        case 'o':
            otext = optarg;
            break;
        case '?':
            if (optopt == 'i' || optopt == 'o')
                fprintf (stderr, "Option -%c requires an argument.\n", optopt);
            else if (isprint (optopt))
                fprintf (stderr, "Unknown option `-%c'.\n", optopt);
            else
                fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt);
            usage(argv[0]);
            exit(1);
        default:
            fprintf(stderr, "Both -i and -o are needed\n");
            usage(argv[0]);
            exit(1);
        }
    }

    /* ADD SOME ARGUMENT SANITY CHECKS */
    if (!itext || !otext) {
        printf("Missing mandatory arguments\n");
        exit(1);
    }

    printf("debug = %i\n", debug);
    if (debug) 
        printf ("input file = %s, output file = %s\n", itext, otext);
    return EXIT_SUCCESS;
}

由於您兩次調用getopt ,因此需要將optind = 1;重置optind = 1; ,請重新設置optind = 1; 在通話之間。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM