Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the alternative to getopt function on Windows c++? [duplicate]

The code below I'm using Posix C:

while ((opt = getopt(argc, argv, "a:p:h")) != -1)

How can I port this code to Windows C++ using an alternative function?

Thanks

like image 613
Jones Jardel Poersch Avatar asked May 12 '15 02:05

Jones Jardel Poersch


People also ask

Is getopt standard C?

To be clear, getopt is not part of the C standard, it is part of the POSIX standard.

What is getopt function in C?

The getopt() function is a builtin function in C and is used to parse command line arguments. Syntax: getopt(int argc, char *const argv[], const char *optstring) optstring is simply a list of characters, each representing a single character option.

What does the getopt function return when there is no more work to do?

The getopt function returns the option character for the next command line option. When no more option arguments are available, it returns -1 .

What is Optarg getopt?

optarg indicates an optional parameter to a command line option. opterr can be set to 0 to prevent getopt() from printing error messages. optind is the index of the next element of the argument list to be process, and optopt is the command line option last matched.


1 Answers

Microsoft has provided a nice implementation (as well as some other helpers) inside the open source IoTivity project here you could possibly re-use (pending any licensing requirements you may have):

Take a look at the "src" and "include" directories here for "getopt.h/.c".

https://github.com/iotivity/iotivity/tree/master/resource/c_common/windows

like image 107
Joey Avatar answered Sep 28 '22 04:09

Joey