c - 如何在 C 中分配 "optind"?

标签 c posix getopt

我提出这个问题是因为没有太多关于如何为每个循环分配这个 optind。

Man page说:

The variable optind is the index of the next element to be processed in argv. The system initializes this value to 1.

下面,我有一个从 Head First C 获得的简单代码,在代码中我们从“减去optind” >argc”,我们得到剩余参数的数量,然后我们将使用这些数量将剩余参数打印为“成分”。

#include <unistd.h>
#include <stdio.h>
int main(int argc, char* argv[]) {
char* delivery = "";
int thick = 0 ;
int count = 0;

char ch;,

for(int i = 0; i < argc;i++){
//This is , to show the whole array and their indexes.
    printf("Argv[%i] = %s\n", i, argv[i]);
}
while((ch = getopt(argc, argv, "d:t")) != -1 ){
    switch(ch) {
        case 'd':
            printf("Optind in case 'd' : %i\n",optind);
            delivery = optarg;
            break;
        case 't':
            printf("Optind in case 't' : %i\n",optind);
            thick = 1;
            break;
        default:
            fprintf(stderr,"Unknown option: '%s'\n", optarg); // optional argument.
            return 1;
    }
}
    argc -= optind;
    argv += optind;
    printf("Optind : %i and Argc after the subctraction : %i\n",optind,argc);
    if(thick)
        puts("Thick crust");
    if(delivery[0]){
        printf("To be delivered %s\n", delivery);
    }

    puts("Ingredients:");
    for(count = 0; count < argc ; count ++){
        puts(argv[count]);
    }
    return 0;
}

所以在代码的开头,for 循环写入所有数组及其索引以查看差异。

然后我运行代码:

./pizzaCode -d now Anchovies Pineapple -t //-t is intentionally at the end

有人告诉我,如果标志在末尾,它就不会出现在“t”的情况下,但不知何故它在我的 ubuntu 上工作。这是我想知道的另一件事,但不是主要问题。

所以输出如下:

Argv[0] = ./pizzaCode
Argv[1] = -d
Argv[2] = now
Argv[3] = Anchovies
Argv[4] = Pineapple
Argv[5] = -t
Optind in case 'd' : 3
Optind in case 't' : 6
Optind : 4 and Argc after the subctraction : 2
Thick crust
To be delivered now
Ingredients:
Anchovies
Pineapple

1- 到目前为止一切都很好,问题是 argv[0] 和 argv 1 是怎么来的变成了凤尾鱼和菠萝?

2- 另一个问题是在“d”的情况下 optind 是如何变成 3 的?因为'd的索引是1,下一个索引是2。

3-循环后 optind 是如何变成 4 的?在“t”的情况下是 6。

我希望你们都清楚我的问题,我只是想理解逻辑而不是必须记住它。 提前致谢!

最佳答案

manpage for Gnu getopt记录这个非标准的实现:

By default, getopt() permutes the contents of argv as it scans, so that eventually all the nonoptions are at the end.

这实际上并不完全正确;如您在示例中所见,排列发生在扫描最后一个选项之后。但是效果是一样的; argv 被置换,以便非选项位于末尾,并且 optind 被修改为索引第一个非选项。

如果您想避免排列,以便 getopt 的行为与 Posix 一致:

If the first character of optstring is '+' or the environment variable POSIXLY_CORRECT is set, then option processing stops as soon as a nonoption argument is encountered.

在这种情况下,不会进行任何排列,optind 的值将被保留。

设置 POSIXLY_CORRECT 有其他后果,在各种 Gnu 实用程序的联机帮助页中随处记录。我的习惯是使用 + 作为选项字符串的第一个字符(除非我真的想要非 Posix 行为),但可以说设置环境变量更便携。


针对您的具体问题:

  1. 为什么非选项参数在 argv[0]argv[1] 处?

    因为你修改了argv:argv += optind;

  2. 为什么在循环处理选项-d中是optind3?

    因为该选项需要一个参数。所以 next 参数是 now 参数之后的参数,它已经被处理(通过将指向它的指针放入 optarg)。

  3. optind 是如何变成 4 的?

    如上所述,它在 argv vector 被排列后被修改,以便 optind 成为第一个“未处理的”非选项参数的索引。

关于c - 如何在 C 中分配 "optind"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46636641/

相关文章:

python - 将 Waf 目标链接到由外部构建系统 (CMake) 生成的库

bash - "/path/file/.."如何符合 POSIX 标准?

Bash 长选项/标志 - 怎么做?

c - 配置文件 glib 中字符串键的引号

c - C 中的无符号整数

c - 在 SIGTRAP 上是否有任何获取指令指针的不可知方法?

c - 为什么我们在 getopt() 函数中使用 argc 作为参数?

python - 可选的命令行参数

c - 循环计数器和指针

正确或体面的 getopts_long 用法?