DiaDes  0.1
DIAgnosis of Discrete-Event System
CmdInterface.hh
Go to the documentation of this file.
1 #ifndef __DIADES__UTILS__CMDINTERFACE__HH
2 #define __DIADES__UTILS__CMDINTERFACE__HH
3 
13 #include <iostream>
14 #include <string>
15 #include <vector>
16 #include <set>
17 #include <sstream>
18 #include <regex>
19 #include <unordered_map>
20 #include <boost/program_options.hpp>
23 
24 
25 using std::string;
26 using std::vector;
27 using std::set;
28 using std::stringstream;
29 using std::cout;
30 using std::endl;
31 
32 const size_t ERROR_IN_COMMAND_LINE = 1;
33 const size_t WARNING_IN_COMMAND_LINE = 3;
34 const size_t SUCCESS = 0;
35 const size_t ERROR_UNHANDLED_EXCEPTION = 2;
36 
37 
38 namespace Diades
39 {
40  namespace CmdInterface
41  {
42 
50  {
51  private:
52  const string _fileBase = "[a-zA-Z_][a-zA-Z_0-9]*\\.";
53  std::unordered_map<std::string, std::regex> _suffixes;
54 
55  public:
56 
61  FileSuffixes(const std::vector<std::string> & suffixes);
62 
70  bool
71  match(const std::string & fileName,
72  const std::string & suffix) const;
73 
74  };
75 
76 
77 
84  size_t printUsage(const string & programName, boost::program_options::options_description & desc);
85 
92  size_t printUsage(const string & description);
93 
94 
95 
101  size_t printCommandLineError(const string & msg);
102 
108  inline size_t
110  {
111  return printCommandLineError(msg.str());
112  }
113 
119  size_t
120  printCommandLineWarning(const string & msg);
121 
122 
128  inline size_t
130  {
131  return printCommandLineWarning(msg.str());
132  }
133 
134 
135  /**************************************************************************************************/
136 
144  string getSuffix(const string & name);
145 
146  /**************************************************************************************************/
147 
156  bool fileSuffixOk(const string & modelFile, const string & suffix);
157 
158 
159  /**************************************************************************************************/
160 
169  template<typename Option>
170  bool
171  getOption(const string & parameter, const vector<string> & options, Option & result)
172  {
173  bool found = false;
174  vector<string>::size_type index = 0;
175  while(!found && (index < options.size()))
176  {
177  found = options[index] == parameter;
178  if(!found)
179  {
180  ++index;
181  }
182  }
183  result = (Option) index;
184  return found;
185  }
186 
187 
188  /**************************************************************************************************/
189 
200  template<typename T>
201  void
202  getParameter(int argc, char ** argv, int & index, T & param)
203  {
204  stringstream stream;
205 
206  if(argc == index)
207  {
208  stream << "The parameter " << argv[index - 1] << " is not correct.";
209  printCommandLineError(stream.str());
210  }
211  else
212  {
213  stream << argv[index];
214  if(!(stream >> param))
215  {
216  stream.clear();
217  stream << "The parameter " << argv[index] << " has an incorrect format.";
218  printCommandLineError(stream.str());
219  }
220  }
221  ++index;
222  }
223 
224 
225  /**************************************************************************************************/
226 
239  template<typename T>
240  void
241  getParameterList(int argc, char ** argv,
242  const vector<string> & options,
243  const vector<string> & fileExtensions,
244  int & index, set< T > & records)
245  {
246  cout << "index = " << index << " argc = " << argc << endl;
247  if(index != argc)
248  {
249  string newValue;
250  getParameter<string>(argc, argv, index, newValue);
251  --index;
252  cout << "param = " << newValue << endl;
253  bool isAnOption = false;
254  vector<string>::size_type i = 0;
255  while((i < options.size()) && (!isAnOption))
256  {
257  isAnOption = options[i] == newValue;
258  ++i;
259  }
260  if(!isAnOption)
261  {
262  cout << newValue << " is not an option so is part of the list following an option" << endl;
263  if(Diades::Utils::getFileExtension(newValue, fileExtensions.begin(), fileExtensions.end()) == fileExtensions.end())
264  {
265  T param;
266  getParameter<T>(argc, argv, index, param);
267  records.insert(param);
268  cout << "insert the filename " << param << endl;
269  getParameterList(argc, argv, options, fileExtensions, index, records);
270  }
271  }
272  }
273  }
274 
275  };
276 };
277 
278 
279 
280 #endif
size_t printUsage(const string &programName, Poptions::options_description &desc)
Definition: CmdInterface.cc:73
const size_t SUCCESS
Definition: CmdInterface.hh:34
InputIterator getFileExtension(const std::basic_string< CharType, CharTraits > &fileName, InputIterator begin, InputIterator end)
Definition: StringTools.hh:57
std::unordered_map< std::string, std::regex > _suffixes
Definition: CmdInterface.hh:53
const size_t ERROR_UNHANDLED_EXCEPTION
Definition: CmdInterface.hh:35
size_t printCommandLineError(const string &msg)
Definition: CmdInterface.cc:98
vector< string > options(numberOfOptions)
const size_t ERROR_IN_COMMAND_LINE
Definition: CmdInterface.hh:32
FileSuffixes(const std::vector< std::string > &suffixes)
Definition: CmdInterface.cc:37
bool match(const std::string &fileName, const std::string &suffix) const
Definition: CmdInterface.cc:54
bool getOption(const string &parameter, const vector< string > &options, Option &result)
string getSuffix(const string &name)
String utilities for Diades.
Option
Definition: abstract.cc:32
const size_t WARNING_IN_COMMAND_LINE
Definition: CmdInterface.hh:33
bool fileSuffixOk(const string &modelFile, const string &suffix)
Namespace of the Diades project.
void getParameter(int argc, char **argv, int &index, T &param)
size_t printCommandLineWarning(const string &msg)
FileSuffixes suffixes({"aut","ddaut"})
void getParameterList(int argc, char **argv, const vector< string > &options, const vector< string > &fileExtensions, int &index, set< T > &records)
boost::format Msg
Definition: Verbose.hh:42
string description
Definition: abstract.cc:42
vector< string > fileExtensions(numberOfFileExtensions)