C : Use getopt with an option which takes an optional parameter

标签 c getopt

我想制作一个带有可选参数 -a 的程序,它可以有一个可选值“文件名”。为此,我想使用 getopt 但我不能让 -a 接受可选参数。

如果我通过 "a" 我可以做 ./my_prog -a

如果我传递 "a:" 我可以做 ./my_prog -a filename//filenamen 在这里不是可选的

如果我通过 "a::" 我可以做 ./my_prog -a[filename]

但是有没有办法获得结果 -a [filename]//filename 是可选的?

最佳答案

我不相信你要求的是可能的。

这可能是一件好事:如果调用为 ./my_prog -a -b,您的程序应该做什么,其中 -b 是它应该采用的另一个选项?它是否应该使用 -b 作为 -a 之后的可选文件名,或者假设没有给出文件名并将其视为一个选项,这并不明显。

因此,我建议重新考虑您要求的行为是否真的是个好主意。

关于C : Use getopt with an option which takes an optional parameter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21908777/

相关文章:

c - 多个逗号分隔值的 getopt 错误处理

c - OpenCV:调整图像大小

perl - 使用 perl Getopt 惯用地处理重复的选项,如 -v -vv -vvv

c - if方法中为什么不清屏(仅限C语言)

c - OpenOffice 断字算法 - 参数是什么意思?

linux - 关于 getopt 语法错误

c++ - getopt/long_getopt 的正确使用方法

python - 可选的命令行参数

c - 在 c 中读入一个 int 并忽略其他任何内容

c - 从 C 中的地址访问值