Beginning Linux Programming

Speakers : Neil Matthew and Richard Stones


Example getopt

#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int opt;
while((opt = getopt(argc, argv, "if:lr")) != -1) {
switch(opt) {
case 'i':
case 'l':
case 'r':
printf("Option: %c\n", opt);
break;
case 'f':
printf("Filename: %s\n", optarg);
break;
case ':':
printf("Option needs a value\n");
break;
case '?':
printf("Unknown option: %c\n", optopt);
break;
}
}
for(;optind < argc;optind++)
printf("Argument: %s\n", argv[optind]);
exit(0);
}

Back  |  Index  |  Next