CFEL - ASG Software Suite  2.5.0
CASS
cl_parser.hpp
Go to the documentation of this file.
1 //Copyright (C) 2013-2015 Lutz Foucar
2 
3 /**
4  * @file cl_parser.hpp contains a parser for command line parameters
5  *
6  * @author Lutz Foucar
7  */
8 
9 #ifndef _COMMANDLINEPARSER_
10 #define _COMMANDLINEPARSER_
11 
12 #include <map>
13 #include <string>
14 #include <iostream>
15 
16 #include <QtCore/QStringList>
17 
18 namespace cass
19 {
20 /** command line argument parser
21  *
22  * object that will parse the command line parameters and set the switches and
23  * retrieve the arguments
24  *
25  * @author Lutz Foucar
26  */
28 {
29 public:
30  /** a container type for switches */
31  typedef std::map<std::string,std::pair<bool*,std::string> > switches_t;
32 
33  /** a container type for switches */
34  typedef std::map<std::string,std::pair<int*,std::string> > intarguments_t;
35 
36  /** a container type for switches */
37  typedef std::map<std::string,std::pair<std::string*,std::string> > stringarguments_t;
38 
39  /** output which commandline parameters are available */
40  void usage()
41  {
42  using namespace std;
43  switches_t::iterator boolarg(_switches.begin());
44  switches_t::iterator boolargEnd(_switches.end());
45  for (;boolarg != boolargEnd; ++boolarg)
46  {
47  cout << boolarg->first <<":"<<boolarg->second.second<<endl;
48  }
49 
50  intarguments_t::iterator intarg(_intargs.begin());
51  intarguments_t::iterator intargEnd(_intargs.end());
52  for (;intarg != intargEnd; ++intarg)
53  {
54  cout << intarg->first <<":"<<intarg->second.second
55  <<" Default value is '"<<*(intarg->second.first)<<"'"<<endl;
56  }
57 
58  stringarguments_t::iterator strarg(_stringargs.begin());
59  stringarguments_t::iterator strargEnd(_stringargs.end());
60  for (;strarg != strargEnd; ++strarg)
61  {
62  cout << strarg->first <<":"<<strarg->second.second
63  <<" Default value is '"<<*(strarg->second.first)<<"'"<<endl;
64  }
65  }
66 
67  /** operator to parse the argumetns
68  *
69  * the arguments are retrieved as a QStringList from Qt. Go through the list
70  * and try to find the parameter in the containers. If it is the switches
71  * container simply set the switch to true. Otherwise take the next parameter
72  * that should be the argument of the preceding parameter.
73  * Start at the 2nd argument of the list, since the first is just the
74  * program name.
75  *
76  * @param argumentList the list of arguments
77  */
78  void operator()(const QStringList& argumentList)
79  {
80  QStringList::const_iterator argument(argumentList.constBegin()+1);
81  for (; argument != argumentList.constEnd(); ++argument)
82  {
83  switches_t::iterator boolarg(_switches.find(argument->toStdString()));
84  if (boolarg != _switches.end())
85  {
86  *(boolarg->second.first) = true;
87  continue;
88  }
89  intarguments_t::iterator intarg(_intargs.find(argument->toStdString()));
90  if (intarg != _intargs.end())
91  {
92  ++argument;
93  if (argument == argumentList.constEnd())
94  break;
95  *(intarg->second.first) = argument->toInt();
96  continue;
97  }
98  stringarguments_t::iterator stringarg(_stringargs.find(argument->toStdString()));
99  if (stringarg != _stringargs.end())
100  {
101  ++argument;
102  if (argument == argumentList.constEnd())
103  break;
104  *(stringarg->second.first) = argument->toStdString();
105  continue;
106  }
107 
108  std::cout << "CommandlineArgumentParser(): parameter '" << argument->toStdString()
109  << "' is unknown. Possible values for this version of the program are: "
110  << std::endl;
111  usage();
112  exit(2);
113  }
114  }
115 
116  /** add a switch to the switches container
117  *
118  * @param sw the name of the parameter to look for
119  * @param desc the description of the parameter
120  * @param val a reference to the value that should be changed.
121  */
122  void add(const std::string &sw, const std::string& desc, bool &val)
123  {
124  _switches[sw] = make_pair(&val,desc);
125  }
126 
127  /** add a switch to the string container
128  *
129  * @param sw the name of the parameter to look for
130  * @param desc the description of the parameter
131  * @param val a reference to the value that should be changed.
132  */
133  void add(const std::string &sw, const std::string& desc, std::string &val)
134  {
135  _stringargs[sw] = make_pair(&val,desc);
136  }
137 
138  /** add a switch to the int container
139  *
140  * @param sw the name of the parameter to look for
141  * @param desc the description of the parameter
142  * @param val a reference to the value that should be changed.
143  */
144  void add(const std::string &sw, const std::string& desc, int &val)
145  {
146  _intargs[sw] = make_pair(&val, desc);
147  }
148 
149 private:
150  /** container for the switches */
151  switches_t _switches;
152 
153  /** container for the string arguments */
154  stringarguments_t _stringargs;
155 
156  /** container for the int arguments */
157  intarguments_t _intargs;
158 };
159 }//end namspace cass
160 
161 #endif
void add(const std::string &sw, const std::string &desc, std::string &val)
add a switch to the string container
Definition: cl_parser.hpp:133
void usage()
output which commandline parameters are available
Definition: cl_parser.hpp:40
STL namespace.
std::map< std::string, std::pair< int *, std::string > > intarguments_t
a container type for switches
Definition: cl_parser.hpp:34
stringarguments_t _stringargs
container for the string arguments
Definition: cl_parser.hpp:154
switches_t _switches
container for the switches
Definition: cl_parser.hpp:151
void add(const std::string &sw, const std::string &desc, bool &val)
add a switch to the switches container
Definition: cl_parser.hpp:122
void add(const std::string &sw, const std::string &desc, int &val)
add a switch to the int container
Definition: cl_parser.hpp:144
command line argument parser
Definition: cl_parser.hpp:27
intarguments_t _intargs
container for the int arguments
Definition: cl_parser.hpp:157
std::map< std::string, std::pair< std::string *, std::string > > stringarguments_t
a container type for switches
Definition: cl_parser.hpp:37
void operator()(const QStringList &argumentList)
operator to parse the argumetns
Definition: cl_parser.hpp:78
std::map< std::string, std::pair< bool *, std::string > > switches_t
a container type for switches
Definition: cl_parser.hpp:31