TF++, Miscellaneous classes and modules in C++:
commandline.h
Go to the documentation of this file.
1 
40 #ifndef TF_COMMANDLINE_H_VERSION
41 
42 #define TF_COMMANDLINE_H_VERSION \
43  "TF_COMMANDLINE_H V1.5"
44 
45 #include<tfxx/error.h>
46 #include<iostream>
47 #include<string>
48 #include<cstdlib>
49 
76 namespace tfxx {
77 
85 namespace cmdline {
86 
98  {
100  arg_no=0,
105  };
106 
136  struct Declare {
138  const char* opt_string;
142  const char* arg_default;
143  };
144 
201 class Commandline {
202  public:
204  Commandline(int& iargc, char** argv, Declare* declarations);
206  ~Commandline() { delete[] Mresult; }
207 
212  bool optset(const int& iopt) const
214  { check_index(iopt); return(Mresult[iopt].opt_set); }
216  std::string string_arg(const int& iopt) const
217  { check_index(iopt); return(Mresult[iopt].arg); }
219  int int_arg(const int& iopt) const
220  { check_index(iopt); return(atoi(Mresult[iopt].arg.c_str())); }
222  float float_arg(const int& iopt) const
223  { check_index(iopt); return(float(atof(Mresult[iopt].arg.c_str()))); }
225  double double_arg(const int& iopt) const
226  { check_index(iopt); return(atof(Mresult[iopt].arg.c_str())); }
228  long long_arg(const int& iopt) const
229  { check_index(iopt); return(atol(Mresult[iopt].arg.c_str())); }
231  bool bool_arg(const int& iopt) const
232  { check_index(iopt); return(bool(atoi(Mresult[iopt].arg.c_str()))); }
234 
239  bool extra() const
241  { return(Moptind<Miargc); }
243  char* next()
244  { if (Moptind<Miargc) {return(Margv[Moptind++]);} else {return(NULL);} }
246  private:
248  void check_index(const int& iopt) const
249  {
250  if ((iopt < 0) || (iopt >= Mnopt))
251  {
252  std::cerr << "Commandline: illegal option index '"
253  << iopt << "' on query" << std::endl;
254  TFXX_abort("bailing out...");
255  }
256  }
257 
263  struct Result {
265  bool opt_set;
267  std::string arg;
268  };
269 
273  int Mnopt;
275  int Miargc;
277  char** Margv;
279  int Moptind;
280 };
281 
295 } // namespace cmdline
296 
297 } // namespace tfxx
298 
299 #endif // TF_COMMANDLINE_H_
300 
301 /* ----- END OF commandline.h ----- */
Result * Mresult
Array of command line contents.
Definition: commandline.h:271
int int_arg(const int &iopt) const
return argument of option # iopt as int value
Definition: commandline.h:219
bool opt_set
true if option was used on command line
Definition: commandline.h:265
void check_index(const int &iopt) const
Check requested index before query.
Definition: commandline.h:248
const char * opt_string
option name
Definition: commandline.h:138
option requires an argument
Definition: commandline.h:102
bool optset(const int &iopt) const
true if option # iopt was set on commandline
Definition: commandline.h:213
double double_arg(const int &iopt) const
return argument of option # iopt as double value
Definition: commandline.h:225
char ** Margv
Pointer to array of command line strings (as passed to main)
Definition: commandline.h:277
Eopt_arg_mode opt_arg_mode
option argument mode
Definition: commandline.h:140
bool bool_arg(const int &iopt) const
return argument of option # iopt as bool value
Definition: commandline.h:231
long long_arg(const int &iopt) const
return argument of option # iopt as long value
Definition: commandline.h:228
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
char * next()
returns char-array of next commandline argument
Definition: commandline.h:243
~Commandline()
Destructor has to remove result array.
Definition: commandline.h:206
int Miargc
Number of command line strings (as passed to main)
Definition: commandline.h:275
bool extra() const
true if there are more commandline arguments
Definition: commandline.h:240
Eopt_arg_mode
option modes ,The following enums are used to define whether an option has or has not an argument...
Definition: commandline.h:97
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
Evaluates commandline by calling long_getopt. ,You may instantiate a Commandline object by passing th...
Definition: commandline.h:201
option may have an optional argument
Definition: commandline.h:104
const char * arg_default
option default argument
Definition: commandline.h:142
struct to define options ,This struct is used to define a list of options. An example is: ...
Definition: commandline.h:136
float float_arg(const int &iopt) const
return argument of option # iopt as float value
Definition: commandline.h:222
option has no argument
Definition: commandline.h:100
std::string arg
holds option argument
Definition: commandline.h:267
std::string string_arg(const int &iopt) const
return argument of option # iopt as string value
Definition: commandline.h:216
#define TFXX_abort(M)
Abort and give a message.
Definition: error.h:183
Namespace containing all code of library libtfxx.