RSC  0.16.0
CommandLinePropertySource.cpp
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is a part of the rsc project.
4  *
5  * Copyright (C) 2013 by Johannes Wienke <jwienke at techfak dot uni-bielefeld dot de>
6  *
7  * This file may be licensed under the terms of the
8  * GNU Lesser General Public License Version 3 (the ``LGPL''),
9  * or (at your option) any later version.
10  *
11  * Software distributed under the License is distributed
12  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13  * express or implied. See the LGPL for the specific language
14  * governing rights and limitations.
15  *
16  * You should have received a copy of the LGPL along with this
17  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
18  * or write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * The development of this software was supported by:
22  * CoR-Lab, Research Institute for Cognition and Robotics
23  * Bielefeld University
24  *
25  * ============================================================ */
26 
28 
29 #include <stdexcept>
30 #include <vector>
31 
32 using namespace std;
33 
34 namespace rsc {
35 namespace config {
36 
37 CommandLinePropertySource::CommandLinePropertySource(int argc,
38  const char** argv,
39  bool reportSyntaxErrors,
40  char option) :
41  logger(logging::Logger::getLogger("rsc.config.CommandLineSource")),
42  argc(argc), argv(argv),
43  reportSyntaxErrors(reportSyntaxErrors), option(option) {
44 }
45 
47 }
48 
50 
51  for (int i = 1; i < argc; ++i) {
52 
53  string rawOption = argv[i];
54  RSCTRACE(logger, "Processing raw option '" << rawOption << "'");
55 
56  // there must be enough room for -D
57  if (rawOption.size() < 2) {
58  RSCTRACE(logger, "Ignoring uninteresting option '" << rawOption << "'");
59  continue;
60  }
61 
62  // check whether the separator -- for positional arguments was reached
63  // if so, stop all processing
64  if (rawOption == "--") {
65  break;
66  }
67 
68  // check whether we want to process this option at all
69  if (rawOption.substr(0, 2) != string("-") + option) {
70  RSCTRACE(logger, "Ignoring uninteresting option '" << rawOption << "'");
71  continue;
72  }
73 
74  // check whether we need to go to the next argument because only the flag
75  // was given
76  if (rawOption.length() == 2 && i < argc - 1) {
77  ++i;
78  rawOption = argv[i];
79  } else {
80  rawOption = rawOption.substr(2, rawOption.length() - 2);
81  }
82 
83  size_t equalsPos = rawOption.find_first_of('=');
84  if (equalsPos == string::npos) {
86  "Cannot parse option '" << rawOption << "'. Reason: = character missing");
87  if (reportSyntaxErrors) {
88  throw invalid_argument(
89  "Cannot parse option '" + rawOption
90  + "'. Reason: = character missing");
91  } else {
92  continue;
93  }
94  }
95 
96  string rawKey = rawOption.substr(0, equalsPos);
97  string value = rawOption.substr(equalsPos + 1,
98  rawOption.length() - equalsPos - 1);
99 
101  "Parsed rawKey = '" << rawKey << "', value = '" << value << "'");
102 
103  vector<string> key;
104  // preserve original backtrace
105  if (reportSyntaxErrors) {
106  splitKeyAtDots(rawKey, key);
107  } else {
108  try {
109  splitKeyAtDots(rawKey, key);
110  } catch (invalid_argument& e) {
111  RSCWARN(logger, "Unable to parse option key: " << e.what());
112  continue;
113  }
114  }
115 
116  handler.handleOption(key, value);
117 
118  }
119 
120 }
121 
122 }
123 }
virtual void handleOption(const std::vector< std::string > &key, const std::string &value)=0
This method is called once for each individual option available from a given ConfigSource.
void splitKeyAtDots(const std::string &input, std::vector< std::string > &output)
Creates a key vector structure used in the options backend from a dot-separated string.
Implementations of this interface receive options from ConfigSource objects.
Definition: OptionHandler.h:43
STL namespace.
#define RSCWARN(logger, msg)
Definition: Logger.h:231
LoggerPtr getLogger()
#define RSCTRACE(logger, msg)
Definition: Logger.h:210
void handler(int signal)
void provideOptions(OptionHandler &handler)
Implementations should pass all configuration options to handler.