iRoCS Toolbox  1.1.0
StDataCmdLine.hh
Go to the documentation of this file.
1 /**************************************************************************
2  *
3  * Copyright (C) 2004-2015 Olaf Ronneberger, Florian Pigorsch, Jörg Mechnich,
4  * Thorsten Falk
5  *
6  * Image Analysis Lab, University of Freiburg, Germany
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  **************************************************************************/
23 
24 /**************************************************************************
25 ** Title:
26 ** $RCSfile$
27 ** $Revision: 765 $$Name$
28 ** $Date: 2005-10-26 09:39:45 +0200 (Wed, 26 Oct 2005) $
29 ** Copyright: GPL $Author: ronneber $
30 ** Description:
31 **
32 **
33 **
34 **-------------------------------------------------------------------------
35 **
36 ** $Log$
37 ** Revision 1.4 2005/10/26 07:39:45 ronneber
38 ** - made all get/read-functions const. They were non-const, because the
39 ** StDataCmdLine stores, wether parameters were accessed
40 ** not. Making the functions const and store these informations in a
41 ** mutable member seems to be the cleaner way
42 **
43 ** Revision 1.3 2005/01/28 14:03:30 fehr
44 ** some include bugfixing for external library use
45 **
46 ** Revision 1.2 2004/09/08 14:39:35 ronneber
47 ** - added updateShortCutTable() and translateShortKeys() for short key, e.g.,
48 ** "-g" processing
49 **
50 ** Revision 1.1 2004/08/26 08:36:59 ronneber
51 ** initital import
52 **
53 **
54 **
55 **************************************************************************/
56 
57 #ifndef STDATACMDLINE_HH
58 #define STDATACMDLINE_HH
59 
60 #ifdef HAVE_CONFIG_H
61 #include <config.hh>
62 #endif
63 
64 #include "StDataASCII.hh"
65 #include "ParamInfo.hh"
66 #include "SVMError.hh"
67 
68 namespace svt
69 {
70 
71  class StDataCmdLine : public StDataASCII
72  {
73  public:
74 
75  /*======================================================================*/
79  /*======================================================================*/
81  :_helpRequested( false)
82  {
83  }
84 
85  StDataCmdLine( int argc, const char** argv)
86  :_helpRequested( false)
87  {
88  parseCommandline( argc, argv);
89  }
90 
91 
92  /*======================================================================*/
120  /*======================================================================*/
121  void parseCommandline( int argc, const char** argv);
122 
123 
124  /*======================================================================*/
139  /*======================================================================*/
140  void updateShortCutTable( const std::vector<ParamInfo>& params);
141 
142 
143  /*======================================================================*/
152  /*======================================================================*/
153  void translateShortKeys();
154 
155  /*======================================================================*/
162  /*======================================================================*/
163  const std::string& mode() const
164  {
165  return _mode;
166  }
167 
168  /*======================================================================*/
175  /*======================================================================*/
176  const std::vector<std::string> filenames() const
177  {
178  return _filenames;
179  }
180 
181 
182 
183  /*======================================================================*/
200  /*======================================================================*/
201  template<typename T>
202  void getValue( const std::string& key, T& value) const
203  {
204  _keyOccurenceMap[key]++;
205  StDataASCII::getValue( key, value);
206  }
207 
208  /*======================================================================*/
231  /*======================================================================*/
232  template<typename ForwardIter>
233  void getArray( const std::string& key, const ForwardIter& arrBegin,
234  int containerSize=-1) const
235  {
236  _keyOccurenceMap[key]++;
237  StDataASCII::getArray( key, arrBegin, containerSize);
238  }
239 
240 
241 
242  /*======================================================================*/
249  /*======================================================================*/
250  std::map<std::string, int>& keyOccurenceMap()
251  {
252  return _keyOccurenceMap;
253  }
254 
255 
256  /*======================================================================*/
265  /*======================================================================*/
266  void findUnusedParameters( std::vector<std::string>& unusedParameters) const
267  {
268  std::map<std::string, std::string>::const_iterator
269  itParam = StDataASCII::begin();
270  std::map<std::string,int>::const_iterator
271  itOccu = _keyOccurenceMap.begin();
272  while( itParam != StDataASCII::end()
273  && itOccu != _keyOccurenceMap.end())
274  {
275  if( itParam->first == itOccu->first)
276  {
277  ++itParam;
278  ++itOccu;
279  }
280  else if( itParam->first < itOccu->first)
281  {
282  // keys in the commandline, that were not requested
283  unusedParameters.push_back( itParam->first);
284  ++itParam;
285  }
286  else
287  {
288  // requested values, that where not in the commandline
289  ++itOccu;
290  }
291  }
292 
293  // There may be remaining parameters in parameter map
294  for( ; itParam != StDataASCII::end(); ++itParam)
295  {
296  unusedParameters.push_back( itParam->first);
297  }
298 
299 
300  }
301 
302  /*======================================================================*/
308  /*======================================================================*/
309  bool helpRequested() const
310  {
311  return _helpRequested;
312  }
313 
314 
315  void debugPrintShortCutTable( std::ostream& os) const
316  {
317  for( std::map<std::string, std::string>::const_iterator
318  p = _shortToLongKeyMap.begin();
319  p != _shortToLongKeyMap.end(); ++p)
320  {
321  os << "'" << p->first << "' --> '" << p->second << "'\n";
322  }
323  }
324 
325 
326  private:
327  mutable std::map<std::string, int> _keyOccurenceMap;
328  std::string _mode;
329  std::vector<std::string> _filenames;
330  std::map<std::string, std::string> _shortToLongKeyMap;
331  bool _helpRequested;
332 
333  };
334 
335 
336 
337 
338 
339 }
340 #endif
341 
std::map< std::string, std::string >::const_iterator begin() const
const access to internal map
Definition: StDataASCII.hh:392
bool helpRequested() const
check if the parameter "--help" was given
void getValue(const std::string &key, T &value) const
get a value (with arbitrary type) from the internal map<string,string> specified by the given key...
void translateShortKeys()
translate short keys (like "-g") to long keys ("gamma") using the internal short cut table...
void findUnusedParameters(std::vector< std::string > &unusedParameters) const
find unused parameters (these are parameters, that were extracted from command line ba parseCommandli...
StDataCmdLine()
Constructors.
const std::vector< std::string > filenames() const
return the filenames that were extracted by parseCommandline()
void updateShortCutTable(const std::vector< ParamInfo > &params)
update shortcut table.
StDataCmdLine(int argc, const char **argv)
void debugPrintShortCutTable(std::ostream &os) const
std::map< std::string, std::string >::const_iterator end() const
Definition: StDataASCII.hh:397
void getArray(const std::string &key, const ForwardIter &arrBegin, int containerSize=-1) const
getArray.
void getValue(const std::string &key, T &value) const
get a value (with arbitrary type) from the internal map<string,string> specified by the given key...
void parseCommandline(int argc, const char **argv)
parse given commandline.
const std::string & mode() const
return the mode (1st Command line argument) that was extracted by parseCommandline() ...
void getArray(const std::string &key, const ForwardIter &arrBegin, int containerSize=-1) const
getArray.
std::map< std::string, int > & keyOccurenceMap()
return the key Occurence map.
The StDataASCII class is a container for "structured data", that is kept completly in memory...
Definition: StDataASCII.hh:83