Unix to Windows Porting Dictionary for HPC |
||
|
|
||
|
|
LinksFunction List
|
Table of Contents header files: unistd.h, getopt.h int getopt(int argc, char * const *argv, const char *optstring); int getopt_long(int argc, char * const *argv, const char *optstring, const struct option *longopts, int *longindex); int getopt_long_only(int argc, char * const *argv, const char *optstring, const struct option *longopts, int *longindex); header file: getopt.h int getopt(int argc, char * const *argv, const char *optstring); int getopt_long(int argc, char * const *argv, const char *optstring, const struct option *longopts, int *longindex); int getopt_long_only(int argc, char * const *argv, const char *optstring, const struct option *longopts, int *longindex); These functions parse a command line into options and operands so that the main program can easily deal with user directives. These three functions parse the command line with different restrictions for what is and is not allowable. There is no Windows equivalent of these three functions (or library calls). Source and binary code for these functions is made available at the site listed at the bottom of this page (More Information). The site also contains a longer description of each function with a manual page. The provided code for these functions is based on the BSD Unix source code and is licensed under the BSD license. As such the functions meet historical and current Unix practices. The getopt() function performs strictly historical Unix command line parsing where only "short options" (single option letters following a dash). The getopt_long() function has the same abilities as getopt() with the extended functionality of parsing "long options". Long options allow for options to be more than one character length and are identified on the command line with a double dash ("--"). The getopt_long_only() function only parses long options and does not deal with short options. While Unix systems include unistd.h for getopt() and getopt.h for getopt_long() and getopt_long_only() the include files are unified for Windows programs to include getopt.h for all these functions. This example is identical to the use on Unix systems and is from the manual page for getopt(). You should need no modification or very little modification to your source code.
#include <getopt.h>
int bflag, ch, fd;
bflag = 0;
while ((ch = getopt(argc, argv, "bf:")) != -1) {
switch (ch) {
case 'b':
bflag = 1;
break;
case 'f':
if ((fd = open(optarg, O_RDONLY, 0)) < 0) {
(void)fprintf(stderr,
"myname: %s: %s\n", optarg, strerror(errno));
exit(1);
}
break;
case '?':
default:
usage();
}
}
argc -= optind;
argv += optind;
|