source: testjes/testgetopt.cpp @ 10

Last change on this file since 10 was 10, checked in by willem, 11 years ago

willem

File size: 1.2 KB
Line 
1#include <iostream>
2#include <getopt.h>
3int main(int argc, char*argv[])
4{
5  std::string programname = "prog";
6  static struct option long_options[] =
7    {
8      {"help",     0, 0, 'h'},
9      {"pool",     1, 0, 'p'},
10      {"version",  0, 0, 'v'},
11      {"multi",    0, 0, 'm'},
12      {"file",     1, 0, 'f'},
13      {0,          0, 0, 0}
14    };
15
16  while (1)
17  {
18    int c = getopt_long(argc,argv,"-hp:vmf:",long_options,0);
19    if (c == -1)
20    {
21      if (optind < argc)  // unexpected extra argument
22      {
23        std::cerr << programname << ": Unexpected argument:" << argv[optind] << std::endl;
24        return 1;
25      }
26      break;
27    }
28    if ( c == '?' || c == ':' )
29    {
30      std::cerr << "Invalid parameter, try "<< argv[0]<<" --help " <<std::endl;
31      return 1;
32    }
33    switch(c)
34    {
35      case 'h': 
36                std::cerr << (char)c << ":" << std::endl;
37                break;
38      case 'p': 
39                std::cerr << (char)c << ":" << optarg << std::endl;
40                break;
41      case 'f': 
42                std::cerr << (char)c << ":" << optarg << std::endl;
43                break;
44      case 'v': 
45                std::cerr << (char)c << ":" << std::endl;
46                break;
47      case 'm':
48                std::cerr << (char)c << ":" << std::endl;
49                break;
50      case 1:
51                std::cerr << c << ":" << optarg << std::endl;
52                break;
53    }
54
55  }
56  return 0;
57}
Note: See TracBrowser for help on using the repository browser.