TF++, Miscellaneous classes and modules in C++:
commandline.cc
Go to the documentation of this file.
1 
35 #include <iostream>
36 #include <getopt.h>
37 #include <tfxx/commandline.h>
38 #include <tfxx/error.h>
39 
40 namespace tfxx {
41 namespace cmdline {
42 
45  char** argv,
46  Declare* declarations):
47  Miargc(iargc), Margv(argv)
48  {
49 // cout << "build getopt array" << endl;
50 // cout << declarations[0].opt_string << endl;
51 // cout << declarations[1].opt_string << endl;
52 
53  // count the number of options by looking for the last one
54  // which has to have a NULL pointer in the name
55  Mnopt=0;
56 // cout << "count options" << endl;
57  while (declarations[Mnopt].opt_string != NULL) { Mnopt++; }
58 // cout << Mnopt << " options found" << endl;
59 
60  // allocate temporary array long_optins for function
61  // getopt_long_only
62  struct option* long_options=new struct option[Mnopt];
63  // allocate result array
64  Mresult=new Result[Mnopt];
65 
66 // cout << "fill getopt array" << endl;
67  // fill array long_options with required values
68  // and Mresult with default values
69  int iopt=0;
70  while (declarations[iopt].opt_string != NULL)
71  {
72  long_options[iopt].name =declarations[iopt].opt_string;
73  long_options[iopt].has_arg =declarations[iopt].opt_arg_mode;
74  long_options[iopt].flag =NULL;
75  long_options[iopt].val =iopt;
76  Mresult[iopt].opt_set =false;
77  Mresult[iopt].arg =std::string(declarations[iopt].arg_default);
78  iopt++;
79  }
80 
81 // cout << "evaluate getopt" << endl;
82  // evaluate command line by calling getopt_long_only
83  int c;
84  while (1)
85  {
86  iopt=0;
87  c=getopt_long_only(iargc, argv, "", long_options, &iopt);
88 // cout << "returned " << c << " and " << iopt << endl;
89 
90  // finish on return value -1
91  if (c == -1) break;
92 
93  // check for error condition
94  if ((c < 0) || (c >= Mnopt))
95  {
96  std::cerr << "Commandline: ERROR on option '"
97  << long_options[iopt].name << "'" << std::endl;
98  TFXX_abort("bailing out...");
99  }
100 
101  // store results
102  Mresult[iopt].opt_set = true;
103  if (optarg) Mresult[iopt].arg=optarg;
104 // cout << "** option '" << long_options[iopt].name <<
105 // "' with argument " << Mresult[iopt].arg << endl;
106  };
107 
108  // remember local optind (for later scan)
109  Moptind=optind;
110 
111  // remove temporaray array
112  delete[] long_options;
113 // cout << "initialized" << endl;
114  }
115 
116 } // namespace cmdline
117 } // namespace tf
118 
119 /* ----- END OF commandline.cc ----- */
Result * Mresult
Array of command line contents.
Definition: commandline.h:271
bool opt_set
true if option was used on command line
Definition: commandline.h:265
const char * opt_string
option name
Definition: commandline.h:138
Eopt_arg_mode opt_arg_mode
option argument mode
Definition: commandline.h:140
int Mnopt
Total number of options evaluated ot Mresult.
Definition: commandline.h:273
Commandline(int &iargc, char **argv, Declare *declarations)
Constructor evaluates commandline by calling long_getopt.
Definition: commandline.cc:44
Structure to store result of command line scanning.
Definition: commandline.h:263
int Moptind
Next option string to read after last defined option.
Definition: commandline.h:279
struct to define options ,This struct is used to define a list of options. An example is: ...
Definition: commandline.h:136
std::string arg
holds option argument
Definition: commandline.h:267
#define TFXX_abort(M)
Abort and give a message.
Definition: error.h:183
Namespace containing all code of library libtfxx.