Use getopt in c/c++
// getopt.cpp
//
// $Id$
//
#include <unistd.h>
#include <stdio.h>
#include <getopt.h>
using namespace std;
int main(int argc, char **argv);
int main(int argc, char **argv) {
int c;
int digit_optind = 0;
while (1) {
int options_index = 0;
static struct option long_options[] = {
{ "date", 1, 0, 0},
};
c = getopt_long(argc, argv, "d:",
long_options, &options_index);
if (c == -1) break;
switch (c) {
case 0:
printf("options %s", long_options[options_index].name);
if (optarg) printf(" with arg %s ", optarg);
printf("\n");
break;
case 'd':
printf("option d with value '%s'\n", optarg);
break;
}
if (optind < argc) {
printf("non-option argv elements");
while (optind < argc) {
printf("%s ", argv[optind++]);
printf("\n");
}
}
}
return 0;
}