TF++, Miscellaneous classes and modules in C++:

◆ Commandline()

tfxx::cmdline::Commandline::Commandline ( int &  iargc,
char **  argv,
Declare declarations 
)

Constructor evaluates commandline by calling long_getopt.

create and parse

Definition at line 44 of file commandline.cc.

References tfxx::cmdline::Commandline::Result::arg, Mnopt, Moptind, Mresult, tfxx::cmdline::Declare::opt_arg_mode, tfxx::cmdline::Commandline::Result::opt_set, tfxx::cmdline::Declare::opt_string, and TFXX_abort.

46  :
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  }
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
char ** Margv
Pointer to array of command line strings (as passed to main)
Definition: commandline.h:277
int Mnopt
Total number of options evaluated ot Mresult.
Definition: commandline.h:273
int Miargc
Number of command line strings (as passed to main)
Definition: commandline.h:275
int Moptind
Next option string to read after last defined option.
Definition: commandline.h:279
std::string arg
holds option argument
Definition: commandline.h:267
#define TFXX_abort(M)
Abort and give a message.
Definition: error.h:183