C getopt_long 一个选项的两个必需参数

C getopt_long two required arguments for an option

是否可以告诉 getopt_long 如果给出一个选项,我需要两个参数?

例如,如果 -i 存在,则接下来需要两个参数,如果它们不存在,解析将失败。

根据 manual and getopt_long() -- proper way to use it? 每个参数传递一个值,该值通过 optarg 传递。

看来您需要做的是将 optarg 用作第一个参数,将 getopt() 使用的指针用作指针,将 strlen(optarg) 用作指向参数后的指针。然后(当你在 --i 时)测试下一个字符串是参数还是下一个选项。如果是下一个选项,则将错误踢出。如果是第二个参数,就捡起来。

您可能需要使用 optind 变量并指向 argv[optind] 来进行处理。

但是,这可能行不通,因为

By default, getargs() permutes the contents of argv as it scans, so that eventually all the non-options are at the end.

get_opt() 应该跳过第二个参数,但是,我不确定(根据阅读手册)它是跳过它还是保存它以备后用。编写代码时,您必须检查调试输出。我没有时间为此编写测试。