gpt4 book ai didi

c - 使用 getopt 解析命令行参数

转载 作者:行者123 更新时间:2023-11-30 15:24:58 25 4
gpt4 key购买 nike

我正在尝试使用 getopt 来解析我的命令行参数,但我遇到了一个问题,它设置了错误的值,跳过了情况 1,并将情况 2 设置为情况 1。这是 while 循环案例。这些标志工作正常,只是按位置读取,这就是问题所在。

while ((c = getopt (argc, argv, "-l:u:eo")) != -1)
switch (c)
{
case 1:
printf("here\n");
lower = atoi(optarg);
break;
case 2:
upper = atoi(optarg);
break;
}

这是运行程序后终端的输出。

(%) fibon 12 22
here
here
lower = 22, upper = 0, even = 0, odd = 0
Usage: fibon[[lower upper] | [-l lower -u upper]] [-e|-o]

最佳答案

来自getopt(3)手册页:

If the first character of optstring is '-', then each nonoption argv-element is handled as if it were the argument of an option with character code 1.

这意味着两个数字都将从getopt返回为1。您需要跟踪自己拥有哪个选项。

关于c - 使用 getopt 解析命令行参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28140747/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com