1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
#include <iostream>
#include <string>
#include <sstream>
#include <getopt.h>
int main(int argc, char **argv) {
int c, intvar;
std::istringstream iss;
std::string str;
static struct option long_options[] = {
{"test", required_argument, 0, 0},
{NULL, 0, NULL, 0}
};
int option_index = 0;
while ((c= getopt_long(argc, argv, "t", long_options, &option_index)) != -1)
switch (c) {
case 0:
iss.str(optarg);
iss >> intvar;
break;
default:
std::cout<<"default";
}
}
if (optind >=argc || optind < (argc-1))
std::cout<<"more or less than one additional argument given!"<<std::endl;
else {
iss.str(argv[optind]);
iss >> str;
std::cout<< "intvar=" << intvar << ", argv[optind]="
<< argv[optind] << ", str=" << str << std::endl;
}
return 0;
}
|