简体   繁体   中英

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):

./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). 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[])) .

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 .

See also my answer here

如果要链接到GNU C库,请查看getopt_long

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM