c++如何使用getopt_long解析长参数啊?

这是别人写的代码

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

我一定要给每一个参数指定短选项吗?

我只想使用长选项

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

这里面的冒号有什么讲究啊

阅读 4.1k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题