相关疑难解决方法(0)

使用getopt为命令行选项提供两个参数

是否有另一种方法可以在使用getopt时将两个参数作为单个字符串传递给选项?通常我会做以下事情:

./command -o "key value" [command arguments]
Run Code Online (Sandbox Code Playgroud)

然后我必须明确地拆分参数字符串

 while ((op = getopt(argc, argv, "o:")) != EOF) {
    switch (op) {
             case 'o': 
                 char* result = NULL;
                 result = strtok_r(optarg," ");
                 while(result) {
                     /* DO STUFF */
                     result = strtok(NULL," ");
                 }

                 break;
             default:
                 printUsage()
                 break;
   }
Run Code Online (Sandbox Code Playgroud)

所以,我想知道是否有可能做到以下几点:

./command -o key value [command arguments]
Run Code Online (Sandbox Code Playgroud)

使getopt将"value"视为-o第二个参数而不是命令参数.

c getopt

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

c ×1

getopt ×1