[英]How to use getopt when option is a string on C?
I'm working/developing a C program for my university, that can have 3 options when invoked ( -h
for help, -o <argument>
(with or without it) and last option can be a string like(test-in-1): 我正在为我的大学工作/开发一个C程序,该程序在被调用时可以有3个选项(
-h
寻求帮助, -o <argument>
(带有或不带有它),最后一个选项可以是类似(test-in- 1):
./myprogram test-in-1
I have to process these options on my main, and do what's required when they are invoked. 我必须在主机上处理这些选项,并执行调用它们时所需的操作。 None of them are mandatory.
它们都不是强制性的。
I was thinking in using getopt
to parse the options however one of these options is actually a string(char *)
and is with this one that I'm kinda lost since getopt
is not able to read strings, only char or a char with an argument(for example, -h
, -o <argument>
, as far as I understood). 我在考虑使用
getopt
解析选项,但是这些选项之一实际上是一个string(char *)
,由于getopt
无法读取字符串,只能读取char或带有参数(据我了解,例如-h
, -o <argument>
)。 Any idea of how can I do this? 关于如何执行此操作的任何想法?
I can't really post any code besides main line,since I'm at stuck at start of it (int main(int argc, char *argv[]))
. 除了主行之外,我真的不能发布任何代码,因为我一开始就陷入了困境
(int main(int argc, char *argv[]))
。
Thanks in advance for any advice/point in right direction. 在此先感谢您提供任何正确方向的建议/意见。
You can pass a colon :
which means it requires an argument and that argument will be set in optarg
. 您可以传递一个冒号
:
这意味着它需要一个参数,并且该参数将在optarg
设置。
See also my answer here 另请参阅我的答案
如果要链接到GNU C库,请查看getopt_long 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.