COPASI API  4.16.103
Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
copasi::COptionParser Class Reference

#include <COptionParser.h>

Collaboration diagram for copasi::COptionParser:
Collaboration graph
[legend]

Public Member Functions

 COptionParser (void)
 default constructor More...
 
void finalize (void)
 finalize all parsing (eg. check for mandatory options) More...
 
const option_locationsget_locations (void) const
 get the main option locations More...
 
const std::vector< std::string > & get_non_options (void) const
 get a list of nonoptions from the command line More...
 
const optionsget_options (void) const
 get the main options More...
 
void parse (int argc, char *argv[], bool call_finalize=true)
 parse the command line More...
 
void parse (const char *fileName)
 parse a file More...
 

Private Types

enum  opsource { source_cl, source_cf }
 
enum  options_enum {
  option_ConfigDir, option_ConfigFile, option_CopasiDir, option_Home,
  option_Tmp, option_Verbose, option_NoLogo, option_License,
  option_Validate, option_Save, option_ImportSBML, option_ExportSBML,
  option_SBMLSchema, option_ExportBerkeleyMadonna, option_ExportC, option_ExportXPPAUT,
  option_MaxTime
}
 
enum  parser_state { state_option, state_value, state_consume }
 

Private Member Functions

void parse_element (const char *element, int position, opsource source)
 
void parse_long_option (const char *option, int position, opsource source)
 
void parse_short_option (char option, int position, opsource source)
 
void parse_value (const char *value)
 

Private Attributes

option_locations locations_
 
std::vector< std::string > non_options_
 
enum
copasi::COptionParser::options_enum 
openum_
 
options options_
 
enum
copasi::COptionParser::parser_state 
state_
 

Detailed Description

the COptionParser class is the main command line parsing class. You can control the name of it using the cxx_class variable.

Definition at line 166 of file COptionParser.h.

Member Enumeration Documentation

Enumerator
source_cl 
source_cf 

Definition at line 224 of file COptionParser.h.

Enumerator
option_ConfigDir 
option_ConfigFile 
option_CopasiDir 
option_Home 
option_Tmp 
option_Verbose 
option_NoLogo 
option_License 
option_Validate 
option_Save 
option_ImportSBML 
option_ExportSBML 
option_SBMLSchema 
option_ExportBerkeleyMadonna 
option_ExportC 
option_ExportXPPAUT 
option_MaxTime 

Definition at line 196 of file COptionParser.h.

197  {
201  option_Home,
202  option_Tmp,
207  option_Save,
215 
216 #ifdef COPASI_SEDML
217  , option_ImportSEDML
218 #endif
219  } openum_;
enum copasi::COptionParser::options_enum openum_
Enumerator
state_option 
state_value 
state_consume 

Definition at line 221 of file COptionParser.h.

Constructor & Destructor Documentation

copasi::COptionParser::COptionParser ( void  )

default constructor

Definition at line 74 of file COptionParser.cpp.

References locations_.

76 {
77  memset(&locations_, 0, sizeof(locations_));
78 }
option_locations locations_
enum copasi::COptionParser::parser_state state_

Member Function Documentation

void copasi::COptionParser::finalize ( void  )

finalize all parsing (eg. check for mandatory options)

Definition at line 185 of file COptionParser.cpp.

186 {
187  if (state_ == state_value)
188  {
189  switch (openum_)
190  {
191  case option_ConfigDir:
192  throw option_error("missing value for 'configdir' option");
193  case option_ConfigFile:
194  throw option_error("missing value for 'configfile' option");
195  case option_CopasiDir:
196  throw option_error("missing value for 'copasidir' option");
198  throw option_error("missing value for 'exportBerkeleyMadonna' option");
199  case option_ExportC:
200  throw option_error("missing value for 'exportC' option");
201  case option_ExportSBML:
202  throw option_error("missing value for 'exportSBML' option");
203  case option_ExportXPPAUT:
204  throw option_error("missing value for 'exportXPPAUT' option");
205  case option_Home:
206  throw option_error("missing value for 'home' option");
207  case option_ImportSBML:
208  throw option_error("missing value for 'importSBML' option");
209  case option_License:
210  throw option_error("missing value for 'license' option");
211  case option_MaxTime:
212  throw option_error("missing value for 'maxTime' option");
213  case option_NoLogo:
214  throw option_error("missing value for 'nologo' option");
215  case option_SBMLSchema:
216  throw option_error("missing value for 'SBMLSchema' option");
217  case option_Save:
218  throw option_error("missing value for 'save' option");
219  case option_Tmp:
220  throw option_error("missing value for 'tmp' option");
221  case option_Validate:
222  throw option_error("missing value for 'validate' option");
223  case option_Verbose:
224  throw option_error("missing value for 'verbose' option");
225  }
226  }
227 }
enum copasi::COptionParser::options_enum openum_
enum copasi::COptionParser::parser_state state_
const option_locations& copasi::COptionParser::get_locations ( void  ) const
inline

get the main option locations

Definition at line 190 of file COptionParser.h.

References locations_.

191  {return locations_;}
option_locations locations_
const std::vector<std::string>& copasi::COptionParser::get_non_options ( void  ) const
inline

get a list of nonoptions from the command line

Definition at line 182 of file COptionParser.h.

References non_options_.

Referenced by COptions::init().

183  {return non_options_;}
std::vector< std::string > non_options_
const options& copasi::COptionParser::get_options ( void  ) const
inline

get the main options

Definition at line 186 of file COptionParser.h.

References options_.

Referenced by COptions::init().

187  {return options_;}
void copasi::COptionParser::parse ( int  argc,
char *  argv[],
bool  call_finalize = true 
)

parse the command line

Definition at line 80 of file COptionParser.cpp.

Referenced by COptions::init(), and main().

81 {
82  int i = 1;
83 
84 #ifdef Darwin
85 
86  if (argc > 1)
87  {
88  std::string tmp(argv[1]);
89 
90  if (!tmp.compare(0, 4, "-psn")) i = 2;
91  }
92 
93 #endif // Darwin
94 
95  for (; i < argc; ++i) parse_element(argv[i], i, source_cl);
96 
97  if (call_finalize) finalize();
98 }
void parse_element(const char *element, int position, opsource source)
void finalize(void)
finalize all parsing (eg. check for mandatory options)
void copasi::COptionParser::parse ( const char *  fileName)

parse a file

Definition at line 100 of file COptionParser.cpp.

101 {
102  int LineCounter = 0;
103  std::string Line;
104  std::string Option;
105  std::string Value;
106  std::string::size_type pos;
107  std::ifstream File(fileName);
108 
109  if (File.fail())
110  {
111  std::ostringstream error;
112  error << "error opening file: '" << fileName << "'";
113  errno = ENOENT;
114 
115  throw option_error(error.str());
116  }
117 
118  while (true)
119  {
120  try
121  {
122  std::getline(File, Line);
123  LineCounter++;
124 
125  if (File.eof()) break;
126 
127  if (File.fail())
128  {
129  std::ostringstream error;
130  error << "unknown problem";
131 
132  throw option_error(error.str());
133  }
134 
135 #ifndef WIN32
136  /* Take care of dos style line ending under UNIX */
137  /* A case where MS has the better implementation */
138  pos = Line.length();
139  (pos < 1) ? 0 : pos--;
140 
141  if (Line[pos] == 0xd) Line.erase(pos);
142 
143 #endif // not WIN32
144 
145  // eat leading spaces
146  Line.erase(0, Line.find_first_not_of(' '));
147 
148  // skip comments and empty lines
149  if (Line.length() == 0 || Line[0] == '#') continue;
150 
151  // find first position of '=' or ':'
152  pos = Line.find_first_of(":=");
153 
154  Option = "--" + Line.substr(0, pos);
155  Option.erase(Option.find_last_not_of(' ') + 1);
156 
157  parse_element(Option.c_str(), 0, source_cf);
158 
159  if (pos != std::string::npos) // We have a '='
160  {
161  Value = Line.substr(pos + 1);
162  Value.erase(0, Value.find_first_not_of(' '));
163  Value.erase(Value.find_last_not_of(' ') + 1);
164 
165  if (Value.length()) parse_element(Value.c_str(), 0, source_cf);
166  }
167 
168  finalize();
169  }
170 
171  catch (copasi::option_error &e)
172  {
173  std::ostringstream error;
174  error << "error reading file: '" << fileName << "(" << LineCounter
175  << ")': " << e.what();
176 
177  throw option_error(error.str());
178  }
179  }
180 
181  File.clear();
182  File.close();
183 }
void parse_element(const char *element, int position, opsource source)
void finalize(void)
finalize all parsing (eg. check for mandatory options)
void copasi::COptionParser::parse_element ( const char *  element,
int  position,
opsource  source 
)
private

Definition at line 229 of file COptionParser.cpp.

230 {
231  size_t length = strlen(element);
232 
233  switch (state_)
234  {
235  case state_consume:
236  non_options_.push_back(element);
237  break;
238  case state_option:
239 
240  if (length >= 2 && element[0] == '-' && element[1] == '-')
241  {
242  if (length == 2) {state_ = state_consume; return;}
243 
244  element += 2;
245  const char *value = element;
246 
247  while (*value != 0 && *value != '=') ++value;
248 
249  if (*value == '=')
250  {
251  std::string selement(element, value - element), svalue(++value);
252 
253  parse_long_option(selement.c_str(), position, source);
254 
255  if (state_ != state_value)
256  {
257  std::string error("the '"); error += element; error += "' option does not take a value";
258  throw option_error(error);
259  }
260 
261  parse_value(svalue.c_str());
263  }
264  else
265  {
266  parse_long_option(element, position, source);
267  }
268  }
269  else if (length >= 2 && element[0] == '-')
270  {
271  ++element;
272 
273  if (length > 2)
274  {
275  while (*element != 0)
276  {
277  parse_short_option(*element, position, source);
278  ++element;
279 
280  if (state_ == state_value && *element == '=')
281  {
282  parse_value(++element);
284  break;
285  }
286  else if (state_ == state_value) finalize();
287  }
288  }
289  else
290  {
291  parse_short_option(*element, position, source);
292  }
293  }
294  else
295  {
296  non_options_.push_back(element);
297  }
298 
299  break;
300  case state_value:
301  parse_value(element);
303  break;
304  }
305 }
void parse_short_option(char option, int position, opsource source)
std::vector< std::string > non_options_
enum copasi::COptionParser::parser_state state_
void parse_long_option(const char *option, int position, opsource source)
void parse_value(const char *value)
void finalize(void)
finalize all parsing (eg. check for mandatory options)
void copasi::COptionParser::parse_long_option ( const char *  option,
int  position,
opsource  source 
)
private

Definition at line 390 of file COptionParser.cpp.

References copasi::autothrow_help.

391 {
392  option = expand_long_name(option);
393 
394  if (strcmp(option, "SBMLSchema") == 0)
395  {
396  if (source != source_cl) throw option_error("the 'SBMLSchema' option is only allowed on the command line");
397 
399  {
400  throw option_error("the 'SBMLSchema' option is only allowed once");
401  }
402 
404  locations_.SBMLSchema = position;
406  return;
407  }
408  else if (strcmp(option, "configdir") == 0)
409  {
410  if (source != source_cl) throw option_error("the 'configdir' option is only allowed on the command line");
411 
412  if (locations_.ConfigDir)
413  {
414  throw option_error("the 'configdir' option is only allowed once");
415  }
416 
418  locations_.ConfigDir = position;
420  return;
421  }
422  else if (strcmp(option, "configfile") == 0)
423  {
424  if (source != source_cl) throw option_error("the 'configfile' option is only allowed on the command line");
425 
427  {
428  throw option_error("the 'configfile' option is only allowed once");
429  }
430 
432  locations_.ConfigFile = position;
434  return;
435  }
436  else if (strcmp(option, "copasidir") == 0)
437  {
438  if (source != source_cl) throw option_error("the 'copasidir' option is only allowed on the command line");
439 
440  if (locations_.CopasiDir)
441  {
442  throw option_error("the 'copasidir' option is only allowed once");
443  }
444 
446  locations_.CopasiDir = position;
448  return;
449  }
450  else if (strcmp(option, "exportBerkeleyMadonna") == 0)
451  {
452  if (source != source_cl) throw option_error("the 'exportBerkeleyMadonna' option is only allowed on the command line");
453 
455  {
456  throw option_error("the 'exportBerkeleyMadonna' option is only allowed once");
457  }
458 
462  return;
463  }
464  else if (strcmp(option, "exportC") == 0)
465  {
466  if (source != source_cl) throw option_error("the 'exportC' option is only allowed on the command line");
467 
468  if (locations_.ExportC)
469  {
470  throw option_error("the 'exportC' option is only allowed once");
471  }
472 
474  locations_.ExportC = position;
476  return;
477  }
478  else if (strcmp(option, "exportSBML") == 0)
479  {
480  if (source != source_cl) throw option_error("the 'exportSBML' option is only allowed on the command line");
481 
483  {
484  throw option_error("the 'exportSBML' option is only allowed once");
485  }
486 
488  locations_.ExportSBML = position;
490  return;
491  }
492  else if (strcmp(option, "exportXPPAUT") == 0)
493  {
494  if (source != source_cl) throw option_error("the 'exportXPPAUT' option is only allowed on the command line");
495 
497  {
498  throw option_error("the 'exportXPPAUT' option is only allowed once");
499  }
500 
502  locations_.ExportXPPAUT = position;
504  return;
505  }
506  else if (strcmp(option, "home") == 0)
507  {
508  if (source != source_cl) throw option_error("the 'home' option is only allowed on the command line");
509 
510  if (locations_.Home)
511  {
512  throw option_error("the 'home' option is only allowed once");
513  }
514 
516  locations_.Home = position;
518  return;
519  }
520  else if (strcmp(option, "importSBML") == 0)
521  {
522  if (source != source_cl) throw option_error("the 'importSBML' option is only allowed on the command line");
523 
525  {
526  throw option_error("the 'importSBML' option is only allowed once");
527  }
528 
530  locations_.ImportSBML = position;
532  return;
533  }
534  else if (strcmp(option, "license") == 0)
535  {
536  if (source != source_cl) throw option_error("the 'license' option is only allowed on the command line");
537 
538  if (locations_.License)
539  {
540  throw option_error("the 'license' option is only allowed once");
541  }
542 
544  locations_.License = position;
546  return;
547  }
548  else if (strcmp(option, "maxTime") == 0)
549  {
550  if (source != source_cl) throw option_error("the 'maxTime' option is only allowed on the command line");
551 
552  if (locations_.MaxTime)
553  {
554  throw option_error("the 'maxTime' option is only allowed once");
555  }
556 
558  locations_.MaxTime = position;
560  return;
561  }
562  else if (strcmp(option, "nologo") == 0)
563  {
564  source = source; // kill compiler unused variable warning
565 
566  if (locations_.NoLogo)
567  {
568  throw option_error("the 'nologo' option is only allowed once");
569  }
570 
572  locations_.NoLogo = position;
574  return;
575  }
576  else if (strcmp(option, "save") == 0)
577  {
578  source = source; // kill compiler unused variable warning
579 
580  if (locations_.Save)
581  {
582  throw option_error("the 'save' option is only allowed once");
583  }
584 
586  locations_.Save = position;
588  return;
589  }
590  else if (strcmp(option, "tmp") == 0)
591  {
592  source = source; // kill compiler unused variable warning
593 
594  if (locations_.Tmp)
595  {
596  throw option_error("the 'tmp' option is only allowed once");
597  }
598 
600  locations_.Tmp = position;
602  return;
603  }
604  else if (strcmp(option, "validate") == 0)
605  {
606  source = source; // kill compiler unused variable warning
607 
608  if (locations_.Validate)
609  {
610  throw option_error("the 'validate' option is only allowed once");
611  }
612 
614  locations_.Validate = position;
616  return;
617  }
618  else if (strcmp(option, "verbose") == 0)
619  {
620  source = source; // kill compiler unused variable warning
621 
622  if (locations_.Verbose)
623  {
624  throw option_error("the 'verbose' option is only allowed once");
625  }
626 
628  locations_.Verbose = position;
630  return;
631  }
632  else if (source == source_cl && strcmp(option, "help") == 0)
633  {
634  throw autoexcept(autothrow_help, const_usage);
635  }
636 
637  std::string error("unknown option '"); error += option; error += "'";
638  throw option_error(error);
639 }
enum copasi::COptionParser::options_enum openum_
option_locations locations_
enum copasi::COptionParser::parser_state state_
void copasi::COptionParser::parse_short_option ( char  option,
int  position,
opsource  source 
)
private

Definition at line 307 of file COptionParser.cpp.

References copasi::autothrow_help.

308 {
309  switch (option)
310  {
311  case 'c':
312 
313  if (source != source_cl) throw option_error("the 'copasidir' option can only be used on the command line");
314 
315  if (locations_.CopasiDir)
316  {
317  throw option_error("the 'copasidir' option is only allowed once");
318  }
319 
322  locations_.CopasiDir = position;
323  return;
324  case 'e':
325 
326  if (source != source_cl) throw option_error("the 'exportSBML' option can only be used on the command line");
327 
329  {
330  throw option_error("the 'exportSBML' option is only allowed once");
331  }
332 
335  locations_.ExportSBML = position;
336  return;
337  case 'i':
338 
339  if (source != source_cl) throw option_error("the 'importSBML' option can only be used on the command line");
340 
342  {
343  throw option_error("the 'importSBML' option is only allowed once");
344  }
345 
348  locations_.ImportSBML = position;
349  return;
350  case 's':
351  source = source; // kill compiler unused variable warning
352 
353  if (locations_.Save)
354  {
355  throw option_error("the 'save' option is only allowed once");
356  }
357 
360  locations_.Save = position;
361  return;
362  case 't':
363  source = source; // kill compiler unused variable warning
364 
365  if (locations_.Tmp)
366  {
367  throw option_error("the 'tmp' option is only allowed once");
368  }
369 
372  locations_.Tmp = position;
373  return;
374  case 'h':
375 
376  if (source != source_cl) break;
377 
378  throw autoexcept(autothrow_help, const_usage);
379  case '?':
380 
381  if (source != source_cl) break;
382 
383  throw autoexcept(autothrow_help, const_usage);
384  }
385 
386  std::string error("unknown option: '"); error += option; error += "'";
387  throw option_error(error);
388 }
enum copasi::COptionParser::options_enum openum_
option_locations locations_
enum copasi::COptionParser::parser_state state_
void copasi::COptionParser::parse_value ( const char *  value)
private

Definition at line 641 of file COptionParser.cpp.

References copasi::SBMLSchema_L1V1, copasi::SBMLSchema_L1V2, copasi::SBMLSchema_L2V1, copasi::SBMLSchema_L2V2, copasi::SBMLSchema_L2V3, copasi::SBMLSchema_L2V4, and copasi::SBMLSchema_L3V1.

642 {
643  switch (openum_)
644  {
645  case option_ConfigDir:
646  {
647  options_.ConfigDir = value;
648  }
649  break;
650  case option_ConfigFile:
651  {
652  options_.ConfigFile = value;
653  }
654  break;
655  case option_CopasiDir:
656  {
657  options_.CopasiDir = value;
658  }
659  break;
661  {
663  }
664  break;
665  case option_ExportC:
666  {
667  options_.ExportC = value;
668  }
669  break;
670  case option_ExportSBML:
671  {
672  options_.ExportSBML = value;
673  }
674  break;
675  case option_ExportXPPAUT:
676  {
677  options_.ExportXPPAUT = value;
678  }
679  break;
680  case option_Home:
681  {
682  options_.Home = value;
683  }
684  break;
685  case option_ImportSBML:
686  {
687  options_.ImportSBML = value;
688  }
689  break;
690  case option_License:
691  break;
692  case option_MaxTime:
693  {
694  char *endptr; int tmp = std::strtol(value, &endptr, 0);
695 
696  while (*endptr != 0 && std::isspace(*endptr)) ++endptr;
697 
698  if (*endptr != 0)
699  {
700  std::string error("invalid integer value '"); error += value; error += "'";
701  throw option_error(error);
702  }
703 
704  if (tmp < 0)
705  {
706  throw option_error("integer value out of range, 'maxTime' min is 0");
707  }
708 
709  options_.MaxTime = tmp;
710  }
711  break;
712  case option_NoLogo:
713  break;
714  case option_SBMLSchema:
715  {
716  SBMLSchema_enum evalue;
717 
718  if (strcmp(value, "L1V1") == 0)
719  {
720  evalue = SBMLSchema_L1V1;
721  }
722  else if (strcmp(value, "L1V2") == 0)
723  {
724  evalue = SBMLSchema_L1V2;
725  }
726  else if (strcmp(value, "L2V1") == 0)
727  {
728  evalue = SBMLSchema_L2V1;
729  }
730  else if (strcmp(value, "L2V2") == 0)
731  {
732  evalue = SBMLSchema_L2V2;
733  }
734  else if (strcmp(value, "L2V3") == 0)
735  {
736  evalue = SBMLSchema_L2V3;
737  }
738  else if (strcmp(value, "L2V4") == 0)
739  {
740  evalue = SBMLSchema_L2V4;
741  }
742  else if (strcmp(value, "L3V1") == 0)
743  {
744  evalue = SBMLSchema_L3V1;
745  }
746  else
747  {
748  std::string error("'"); error += value; error += "' is an invalid value for the 'SBMLSchema' option";
749  throw option_error(error);
750  }
751 
752  options_.SBMLSchema = evalue;
753  }
754  break;
755  case option_Save:
756  {
757  options_.Save = value;
758  }
759  break;
760  case option_Tmp:
761  {
762  options_.Tmp = value;
763  }
764  break;
765  case option_Validate:
766  break;
767  case option_Verbose:
768  break;
769  }
770 }
std::string ExportXPPAUT
Definition: COptionParser.h:81
std::string CopasiDir
Definition: COptionParser.h:77
enum copasi::COptionParser::options_enum openum_
std::string ExportSBML
Definition: COptionParser.h:80
std::string Save
Definition: COptionParser.h:88
std::string ConfigDir
Definition: COptionParser.h:75
SBMLSchema_enum SBMLSchema
Definition: COptionParser.h:87
std::string ConfigFile
Definition: COptionParser.h:76
std::string ExportC
Definition: COptionParser.h:79
std::string ExportBerkeleyMadonna
Definition: COptionParser.h:78
std::string ImportSBML
Definition: COptionParser.h:83
std::string Tmp
Definition: COptionParser.h:89
std::string Home
Definition: COptionParser.h:82

Member Data Documentation

option_locations copasi::COptionParser::locations_
private

Definition at line 194 of file COptionParser.h.

Referenced by COptionParser(), and get_locations().

std::vector<std::string> copasi::COptionParser::non_options_
private

Definition at line 222 of file COptionParser.h.

Referenced by get_non_options().

enum copasi::COptionParser::options_enum copasi::COptionParser::openum_
private
options copasi::COptionParser::options_
private

Definition at line 193 of file COptionParser.h.

Referenced by get_options().

enum copasi::COptionParser::parser_state copasi::COptionParser::state_
private

The documentation for this class was generated from the following files: