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
To be clear, getopt is not part of the C standard, it is part of the POSIX standard.
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.
The getopt function returns the option character for the next command line option. When no more option arguments are available, it returns -1 .
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With