鍍金池/ 問答/C  C++/ c++如何使用getopt_long解析長(zhǎng)參數(shù)?。?/span>

c++如何使用getopt_long解析長(zhǎng)參數(shù)???

這是別人寫的代碼

#include <getopt.h>
#include <string.h>

struct globalArgs_t {
    int noIndex;                /* -I option */
    char *langCode;             /* -l option */
    const char *outFileName;    /* -o option */
    FILE *outFile;
    int verbosity;              /* -v option */
    char **inputFiles;          /* input files */
    int numInputFiles;          /* # of input files */
    int randomized;             /* --randomize option */
} globalArgs;
 
static const char *optString = "Il:o:vh?";
 
static const struct option longOpts[] = {
    { "no-index", no_argument, NULL, 'I' },
    { "language", required_argument, NULL, 'l' },
    { "output", required_argument, NULL, 'o' },
    { "verbose", no_argument, NULL, 'v' },
    { "randomize", no_argument, NULL, 0 },
    { "help", no_argument, NULL, 'h' },
    { NULL, no_argument, NULL, 0 }
};

opt = getopt_long( argc, argv, optString, longOpts, &longIndex );
    while( opt != -1 ) {
        switch( opt ) {
            case 'I':
                globalArgs.noIndex = 1; /* true */
                break;
                 
            case 'l':
                globalArgs.langCode = optarg;
                break;
                 
            case 'o':
                globalArgs.outFileName = optarg;
                break;
                 
            case 'v':
                globalArgs.verbosity++;
                break;
                 
            case 'h':   /* fall-through is intentional */
            case '?':
                display_usage();
                break;
 
            case 0:     /* long option without a short arg */
                if( strcmp( "randomize", longOpts[longIndex].name ) == 0 ) {
                    globalArgs.randomized = 1;
                }
                break;
                 
            default:
                /* You won't actually get here. */
                break;
        }
         
        opt = getopt_long( argc, argv, optString, longOpts, amp;longIndex );
    }

我一定要給每一個(gè)參數(shù)指定短選項(xiàng)嗎?

我只想使用長(zhǎng)選項(xiàng)

static const char *optString = "Il:o:vh?";

這里面的冒號(hào)有什么講究啊

回答
編輯回答
怪痞
2017年1月22日 03:26