ApplicationOptions.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package ArmarXCore::core
19 * @author Jan Issac (jan dot issac at gmail dot com)
20 * @date 2012
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24
25#include "ApplicationOptions.h"
26
27#include <stddef.h> // for NULL
28
29#include <map> // for map, _Rb_tree_iterator
30#include <ostream> // for char_traits, ostream, etc
31#include <vector> // for vector, vector<>::iterator
32
33#include <boost/program_options.hpp>
34
35#include <Ice/BuiltinSequences.h> // for StringSeq
36#include <Ice/Handle.h> // for Handle
37#include <Ice/Initialize.h> // for argsToStringSeq
38#include <Ice/ObjectF.h> // for upCast
39#include <Ice/PropertiesF.h> // for PropertiesPtr
40
41#include <SimoxUtility/algorithm/string/string_tools.h>
42
43#include "ArmarXCore/core/ManagedIceObject.h" // for ManagedIceObjectPtr, etc
55#include "ArmarXCore/core/logging/LogSender.h" // for LogSender
56#include "ArmarXCore/core/logging/Logging.h" // for ARMARX_WARNING
57
58#include "Application.h" // for Application, ApplicationPtr
59
60namespace armarx
61{
63 ApplicationOptions::mergeProperties(Ice::PropertiesPtr properties, int argc, char* argv[])
64 {
65 // override and merge new properties given by the command line
66 Ice::StringSeq args = Ice::argsToStringSeq(argc, argv);
67 args = properties->parseCommandLineOptions("", args);
68
69 return properties;
70 }
71
73 ApplicationOptions::parseHelpOptions(Ice::PropertiesPtr properties, int argc, char* argv[])
74 {
75 using namespace boost::program_options;
76 Options options;
77 options.error = false;
78 options.showHelp = false;
79 options.showVersion = false;
80 options.outfile = "";
81
82 // create program options
83 options.description.reset(new options_description(
84 std::string("This executable is an application within the ArmarX framework.\n") +
85 "==================================================================\n" +
86 " Starting an ArmarX application\n" +
87 "==================================================================\n" +
88 "In order to start the application, execute it normally and pass optional config" +
89 " parameters to it:\n\n"
90
91 + " > " + argv[0] + " [options]\n\n"
92
93 + "Make sure that Ice is started using the ice-start.sh script.\n" +
94 "Options for the component can be provided either in the command line\n" +
95 "or in a separate config file. In order to provide config files use \n\n"
96
97 + " > " + argv[0] + " --Ice.Config=cfg1,cfg2\n\n"
98
99 + "where multiple config files are allowed.\n\n"
100
101 + "Help options"));
102
103 options.description->add_options()("help,h", "Help option");
104 options.description->add_options()(
105 "version,v",
106 "Shows version of this application, which is usually the version of the package.");
107
108 options.description->add_options()("print-options,p",
109 "Print detailed application options description");
110
111 options.description->add_options()(
112 "options-format,f",
113 value<std::string>(),
114 "Options print format: { help, config, doxygen, doxygen_component_pages, xml }");
115
116 options.description->add_options()(
117 "output-file,o",
118 value<std::string>(),
119 "Optional output file (outherwise gets printed to std::cout)");
120
121 variables_map vm;
122 store(command_line_parser(argc, argv)
123 .options(*options.description)
124 .allow_unregistered()
125 .run(),
126 vm);
127 notify(vm);
128
129 if (vm.count("print-options"))
130 {
131 std::map<std::string, OptionsFormat> optionsFormats;
132 optionsFormats["help"] = eOptionsDetailed;
133 optionsFormats["doxygen"] = eDoxygen;
134 optionsFormats["doxygen_component_pages"] = eDoxygenComponentPages;
135 optionsFormats["config"] = eConfig;
136 optionsFormats["xml"] = eXml;
137
138 if (vm.count("options-format"))
139 {
140 std::string optionsFormatName = vm["options-format"].as<std::string>();
141 optionsFormatName = simox::alg::to_lower(optionsFormatName);
142
143 if (optionsFormats.find(optionsFormatName) != optionsFormats.end())
144 {
145 options.format = optionsFormats[optionsFormatName];
146 }
147 else
148 {
149 std::cout << "Unknown options-format:" << optionsFormatName << std::endl;
150
151 options.showHelp = false;
152 options.error = true;
153 return options;
154 }
155 }
156 else
157 {
158 options.format = eOptionsDetailed;
159 }
160 }
161 else if (vm.count("help") || properties->getProperty("Ice.Default.Locator").empty())
162 {
163 options.format = eHelpBrief;
164 }
165 else if (vm.count("version"))
166 {
167 options.showVersion = true;
168 return options;
169 }
170 else
171 {
172 return options;
173 }
174
175 if (vm.count("output-file"))
176 {
177 std::string outputFilePath = vm["output-file"].as<std::string>();
178 options.outfile = outputFilePath;
179 }
180
181 options.showHelp = true;
182
183 return options;
184 }
185
186 void
188 ArmarXDummyManagerPtr dummyManager,
190 Ice::PropertiesPtr properties,
191 std::ostream& out)
192 {
193 OptionsFormat optionsFormat = options.format;
194 boost::program_options::options_description& desc = *options.description;
195
196 PropertyDefinitionFormatter* defFormatter = nullptr;
197 PropertyDefinitionContainerFormatter* pdcFormatter = nullptr;
198 PropertyUserList puList;
199
200 switch (optionsFormat)
201 {
202 case eHelpBrief:
203 {
204 PropertyDefinitionBriefHelpFormatter* definitionFormatter;
206
207 // help options
208 out << desc << std::endl;
209
210 out << "By default the following config files are loaded:\n ";
211 Ice::StringSeq paths = application->GetDefaultsPaths();
212
213 for (auto p : paths)
214 {
215 out << "\t" << p << std::endl;
216 }
217
218 out << std::endl;
219 // application options
220 boost::program_options::options_description applicationDesc(
221 "Application properties");
222
223 definitionFormatter = new PropertyDefinitionBriefHelpFormatter(&applicationDesc);
224 containerFormatter = new PropertyDefinitionContainerBriefHelpFormatter(
225 *definitionFormatter, &applicationDesc);
226
227 puList.insert(puList.begin(), application);
228 out << containerFormatter->formatPropertyUsers(puList) << std::endl;
229
230 // component options
231 boost::program_options::options_description componentDesc("Component properties");
232
233 delete definitionFormatter;
234 delete containerFormatter;
235
236 definitionFormatter = new PropertyDefinitionBriefHelpFormatter(&componentDesc);
237 containerFormatter = new PropertyDefinitionContainerBriefHelpFormatter(
238 *definitionFormatter, &componentDesc);
239
240 puList = getPropertyUsers(dummyManager);
241 out << containerFormatter->formatPropertyUsers(puList) << std::endl;
242
243 delete definitionFormatter;
244 delete containerFormatter;
245 }
246
247 return;
248
249 case eOptionsDetailed:
250 defFormatter = new PropertyDefinitionHelpFormatter();
251 pdcFormatter = new PropertyDefinitionContainerFormatter(*defFormatter);
252 break;
253
254 case eXml:
255 defFormatter = new PropertyDefinitionXmlFormatter();
256 pdcFormatter = new PropertyDefinitionContainerFormatter(*defFormatter);
257 break;
258
259 case eDoxygen:
260 defFormatter = new PropertyDefinitionDoxygenFormatter();
261 pdcFormatter = new PropertyDefinitionContainerFormatter(*defFormatter);
262 break;
263
266 pdcFormatter = new PropertyDefinitionContainerFormatter(*defFormatter);
267 break;
268
269 case eConfig:
270 defFormatter = new PropertyDefinitionConfigFormatter();
271 pdcFormatter = new PropertyDefinitionContainerFormatter(*defFormatter);
272 break;
273 default:
274 ARMARX_WARNING << "ConfigFormatter with id " << (int)optionsFormat
275 << " not available";
276 return;
277 }
278
279 pdcFormatter->setProperties(properties);
280 // add this application to the property user list
281 puList = getPropertyUsers(dummyManager);
282 puList.insert(puList.begin(), application);
283
284 out << pdcFormatter->formatPropertyUsers(puList) << std::endl;
285
286 delete defFormatter;
287 delete pdcFormatter;
288 }
289
292 {
293 PropertyUserList propertyUsers;
294
295 std::vector<ManagedIceObjectPtr> objects = dummyManager->getManagedObjects();
296
297 for (auto& it : objects)
298 {
299 PropertyUserPtr user = PropertyUserPtr::dynamicCast(it);
300
301 if (user)
302 {
303 propertyUsers.push_back(user);
304 }
305
306 ComponentPtr comp = ComponentPtr::dynamicCast(it);
307 if (comp)
308 {
309 auto additionalPropertyUsers = comp->getAdditionalPropertyUsers();
310 propertyUsers.insert(propertyUsers.end(),
311 additionalPropertyUsers.begin(),
312 additionalPropertyUsers.end());
313 }
314 }
315
316 return propertyUsers;
317 }
318} // namespace armarx
std::string formatPropertyUsers(const PropertyUserList &propertyUserList) override
virtual std::string formatPropertyUsers(const PropertyUserList &propertyUserList)
PropertyDefinitionDoxygenComponentPagesFormatter creates doxygen output with an own group for every c...
PropertyDefinitionDoxygenFormatter creates doxygen output.
PropertyDefinitionFormatter is the base class for all formatters of PropertyDefinitions.
OptionsFormat
Help format to display.
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
::IceInternal::Handle<::Ice::Properties > PropertiesPtr
PropertyUserList getPropertyUsers(ArmarXDummyManagerPtr dummyManager)
Return the list of property users.
Options parseHelpOptions(Ice::PropertiesPtr properties, int argc, char *argv[])
Parse the help options.
Ice::PropertiesPtr mergeProperties(Ice::PropertiesPtr properties, int argc, char *argv[])
Merge command line options into properties.
void showHelp(ApplicationPtr application, ArmarXDummyManagerPtr dummyManager, Options options, Ice::PropertiesPtr properties, std::ostream &out=std::cout)
Prints help according to the format selection in options.
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::vector< PropertyUserPtr > PropertyUserList
UserProperty list type.
IceUtil::Handle< Application > ApplicationPtr
Definition Application.h:93
IceUtil::Handle< class PropertyUser > PropertyUserPtr
PropertyUser smart pointer type.
IceUtil::Handle< ArmarXDummyManager > ArmarXDummyManagerPtr
IceInternal::Handle< Component > ComponentPtr
Component smart pointer type.
Definition ArmarXFwd.h:45
Stucture containing the parsed options of the application.
std::shared_ptr< boost::program_options::options_description > description