簡體   English   中英

為什么C getopt_long_only()沒有為未知選項設置optopt?

[英]Why does C getopt_long_only() not set optopt for unknown option?

我試圖使用帶有自定義錯誤消息的getopt_long_only() 代碼如下所示。 我嘗試設置opterr = 0並在optstring的開頭使用冒號來禁用內置錯誤消息。 我添加了一個用boolean optoptWorks = true控制的代碼塊來嘗試自定義錯誤消息,例如在使用-z這樣的錯誤選項時打印消息。 但是optopt總是設置為0而且? 我正在使用的錯誤消息沒有意義。 ':'(冒號)錯誤(缺少參數,如-d )對自定義消息確實有效。 關閉內置錯誤信息並處理? 似乎導致optopt始終設置為0,因此我無法打印違規選項( -z is not recognized )。 我在Debian Linux gcc 4.9.4以及Cygwin gcc 7.3.0上編譯,兩者都給出了相同的結果。 似乎getopt_long_only()可能沒有正確設置optopt或者我錯過了什么? Web上的許多示例通過使用內置錯誤消息或僅打印用法而不告訴用戶哪個選項無法識別來解決這個問題。

這是optoptWorks=false輸出:

$ ./testoptget -z
testoptget: unknown option -- z

-d #            Set the debug level.
-h, --help      Print program usage.
-q              Run in quiet mode (log messages to syslog but not console).
-v, --version   Print program version.

$ ./testoptget -d
testoptget: option requires an argument -- d

-d #            Set the debug level.
-h, --help      Print program usage.
-q              Run in quiet mode (log messages to syslog but not console).
-v, --version   Print program version.

這是輸出optoptWorks=true

$ ./testoptget -z
[ERROR] Unknown option character '\x0'.

-d #            Set the debug level.
-h, --help      Print program usage.
-q              Run in quiet mode (log messages to syslog but not console).
-v, --version   Print program version.

$ ./testoptget -d
[ERROR] Option '-d' is missing argument.

-d #            Set the debug level.
-h, --help      Print program usage.
-q              Run in quiet mode (log messages to syslog but not console).
-v, --version   Print program version.

代碼如下:

/*
Test program for getopt_long_only
*/

#include <ctype.h>
#include <getopt.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int debug = 0; /* Default to not debugging */

/**
Print the program usage.
*/
void usage(void)
{
    /* Print the program name and version */
    printf("\n");
    printf("-d #            Set the debug level.\n");
    printf("-h, --help      Print program usage.\n");
    printf("-q              Run in quiet mode (log messages to syslog but not console).\n");
    printf("-v, --version   Print program version.\n\n");
    exit(0);
}

/**
Parse command line parameters and set data for program.
@param argc number of command line parameters
@param argv list of command line parameters
*/
void parseargs(int argc,char **argv)
{   /*
    See:  https://www.gnu.org/software/libc/manual/html_node/Getopt.html#Getopt
    See:  http://man7.org/linux/man-pages/man3/getopt.3.html

    Because legacy -version and --version need to be supported, use getopts_long_only.
    */

    /*
    The meaning of the following is:
    name - the name of the long option
    has_arg - whether the option has an argument like --arg param or --arg=param
    flag - the numeric value to return (set to "opt" below), if NULL or zero, return "val"
    val - the value to return (set to "opt" below) if "flag" not set, use the one-character equivalent
    */
    static struct option long_options[] = {
        { "help",    no_argument, 0, 'h' }, /* returns as if -v, index not needed */
        { "version", no_argument, 0, 'v' }, /* returns as if -h, index not needed */
        { 0,         0,           0, 0 }    /* last element of array must be zeros */
    };
    int long_index = 0;
    int opt;
    int errorCount = 0;
    /* In <unistd.h>:  external int optind, opterr, optopt */
    bool optoptWorks = false;  /* Apparently optopt gets set to 0 for unknown argument so let the getopt_long_only print the error */
    char optstring[32] = "d:hqv";
    if ( optoptWorks ) {
        /*
        If getopt_long_only works as it is supposed to...
        Set opterr to zero so getopt calls won't print an error - check for errors in '?' return value
        Also use : as first character of optstring to cause : to be used for error handling
        */
        opterr = 0;
        /* Do the following because strcat is not safe on overlapping strings */
        char optstring2[32];
        strcpy(optstring2,optstring);
        strcpy(optstring,":");
        strcat(optstring,optstring2);
    }
    while((opt = getopt_long_only(argc, argv, optstring, long_options, &long_index)) != -1) {
        switch (opt) { /* Will match single character option or long_options val or flag */
            case 'd':
                /* -d #, Set the debug level to the argument value */
                debug = atoi(optarg);
                break;
            case 'h':
                /*
                -h, print the usage and exit
                -help
                --help
                */
                usage();
                exit(0);
                break;
            case 'q':
                /* -q, indicate that messages should not be printed to stdout */
                break;
            case 'v':
                /*
                -v, print the version via standard function,
                -version
                --version
                */
                break;
            case ':':
                /*
                This is an error indicator indicated by : at the start of get_opt_long 3rd argument.
                Handle missing argument, such as -d but no argument.
                */
                fprintf(stderr, "[ERROR] Option '-%c' is missing argument.\n", optopt);
                ++errorCount;
                break;
            case '?':
                /*
                Handle unknown parameters as per getopt man page example.
                "optopt" should contain the offending argument, but perhaps matches last long argument (zero record).
                Note that legacy ? command line parameter is no longer supported.
                */
                if (isprint(optopt)) {
                    /* Printable character so print it in the warning. */
                    if ( optoptWorks ) {
                        fprintf(stderr, "[ERROR] Unknown option '-%c'.\n", optopt);
                    }
                    ++errorCount;
                }
                else {
                    /* Nonprintable character so show escape sequence. */
                    if ( optoptWorks ) {
                        fprintf(stderr, "[ERROR] Unknown option character '\\x%x'.\n", optopt);
                    }
                    ++errorCount;
                }
                break;
        } /* end switch */
    } /* end while */
    if ( errorCount > 0 ) {
        usage();
        exit(1);
    }
}

/**
Main program.
@param argc number of command line parameters
@param argv list of command line parameters
@param arge list of environment variables
*/
int main(int argc,char **argv,char **arge)
{
    /* Parse command arguments */
    parseargs(argc,argv);

    /* Normal program termination */
    return(0);
}

當找到未知的長選項時, optopt確實設置為零,請參見此處 但是在我看來你可以使用optind - 1作為argv的索引來打印違規選項,因為在getopt返回'?'之前, optind在此處遞增'?'

據我所知,您的目標是只指定自定義錯誤消息。

另外來自man getopt_long:

如果optstring的第一個字符(在上面描述的任何'+'或' - '之后)是冒號(':'),那么getopt()同樣不會打印錯誤消息。 另外,它返回':'而不是'?' 表示缺少選項參數。 這允許調用者區分兩種不同類型的錯誤。

您引用的文檔是關於getopt而不是getopt_long_only 男人getopt_long_only確實說getopt_long() function works like getopt()但是optopt被設置為“選項字符”。 在長期期權的情況下,沒有“期權字符”,但是“選項字符串”(正如我所說的那樣) - 似乎合乎邏輯地將optopt設置為零。

所以取決於optstring中的初始字符, :? 返回,在此處以及此處此處實現

下面的程序是您與刪除評論,短路使用功能,取代exitreturn ,並添加打印違規選項只printf("%s", argv[opting - 1]);

#include <ctype.h>
#include <getopt.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int debug = 0;

void usage(void) { printf("-- insert usage here --\n"); }

void parseargs(int argc,char **argv)
{
    static struct option long_options[] = {
        { "help",    no_argument, 0, 'h' },
        { "version", no_argument, 0, 'v' },
        { 0,         0,           0, 0 }
    };
    int long_index = 0;
    int opt;
    int errorCount = 0;

    optind = 1;

    while((opt = getopt_long_only(argc, argv, ":d:hqv", long_options, &long_index)) != -1) {
        switch (opt) {
            case 'd':
                debug = atoi(optarg);
                break;
            case 'h':
                usage();
                return;
                break;
            case 'q':
                break;
            case 'v':
                break;
            case ':':
                fprintf(stderr, "[ERROR] Option '-%c' is missing argument.\n", optopt);
                ++errorCount;
                break;
            case '?':
                if (optopt == 0) {
                    fprintf(stderr, "[ERROR] Unknown option '%s'.\n", argv[optind - 1]);
                } else {
                    fprintf(stderr, "[ERROR] Error parsing option '-%c'\n", optopt);
                }
                ++errorCount;
                break;
        }
    }
    if ( errorCount > 0 ) {
        usage();
        return;
    }
}

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

#define SIZE(x) (sizeof(x)/sizeof(*x))
    struct {
        int argc;
        char **argv;
    } tests[] = {
        { 2, (char*[]){ argv[0], (char[]){"-z"}, NULL, } },
        { 2, (char*[]){ argv[0], (char[]){"-d"},  NULL, } },
    };

    for (int i = 0; i < SIZE(tests); ++i) {
        printf("\n## test tests[i].argv[1] = %s\n", tests[i].argv[1]);
        parseargs(tests[i].argc, tests[i].argv);
    }

    return 0;
}

輸出:

## test tests[i].argv[1] = -z
[ERROR] Unknown option '-z'.
-- insert usage here --

## test tests[i].argv[1] = -d
[ERROR] Option '-d' is missing argument.
-- insert usage here --

如果optstring設置為"d:hqv"而沒有前導: ,則它會落入? 案例,即。 然后程序返回:

## test tests[i].argv[1] = -z
./a.out: unrecognized option '-z'
[ERROR] Unknown option '-z'.
-- insert usage here --

## test tests[i].argv[1] = -d
./a.out: option requires an argument -- 'd'
[ERROR] Error parsing option '-d'
-- insert usage here --

暫無
暫無

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

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