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 <Ice/BuiltinSequences.h> // for StringSeq
26 #include <Ice/Handle.h> // for Handle
27 #include <Ice/Initialize.h> // for argsToStringSeq
28 #include <Ice/ObjectF.h> // for upCast
29 #include <Ice/PropertiesF.h> // for PropertiesPtr
30 
31 #include <boost/program_options.hpp>
32 
33 #include <stddef.h> // for NULL
34 #include <map> // for map, _Rb_tree_iterator
35 #include <ostream> // for char_traits, ostream, etc
36 #include <vector> // for vector, vector<>::iterator
37 
38 #include "Application.h" // for Application, ApplicationPtr
39 #include "ApplicationOptions.h"
40 #include "ArmarXCore/core/ManagedIceObject.h" // for ManagedIceObjectPtr, etc
52 #include "ArmarXCore/core/logging/LogSender.h" // for LogSender
53 #include "ArmarXCore/core/logging/Logging.h" // for ARMARX_WARNING
54 
55 #include <SimoxUtility/algorithm/string/string_tools.h>
56 
57 namespace armarx
58 {
60  {
61  // override and merge new properties given by the command line
62  Ice::StringSeq args = Ice::argsToStringSeq(argc, argv);
63  args = properties->parseCommandLineOptions("", args);
64 
65  return properties;
66  }
67 
69  {
70  using namespace boost::program_options;
71  Options options;
72  options.error = false;
73  options.showHelp = false;
74  options.showVersion = false;
75  options.outfile = "";
76 
77  // create program options
78  options.description.reset(new options_description(std::string(
79  "This executable is an application within the ArmarX framework.\n")
80  + "==================================================================\n"
81  + " Starting an ArmarX application\n"
82  + "==================================================================\n"
83  + "In order to start the application, execute it normally and pass optional config"
84  + " parameters to it:\n\n"
85 
86  + " > " + argv[0] + " [options]\n\n"
87 
88  + "Make sure that Ice is started using the ice-start.sh script.\n"
89  + "Options for the component can be provided either in the command line\n"
90  + "or in a separate config file. In order to provide config files use \n\n"
91 
92  + " > " + argv[0] + " --Ice.Config=cfg1,cfg2\n\n"
93 
94  + "where multiple config files are allowed.\n\n"
95 
96  + "Help options"));
97 
98  options.description->add_options()("help,h", "Help option");
99  options.description->add_options()("version,v", "Shows version of this application, which is usually the version of the package.");
100 
101  options.description->add_options()("print-options,p", "Print detailed application options description");
102 
103  options.description->add_options()("options-format,f", value<std::string>(), "Options print format: { help, config, doxygen, doxygen_component_pages, xml }");
104 
105  options.description->add_options()("output-file,o", value <std::string>(), "Optional output file (outherwise gets printed to std::cout)");
106 
107  variables_map vm;
108  store(command_line_parser(argc, argv).options(*options.description).allow_unregistered().run(), vm);
109  notify(vm);
110 
111  if (vm.count("print-options"))
112  {
113  std::map<std::string, OptionsFormat> optionsFormats;
114  optionsFormats["help"] = eOptionsDetailed;
115  optionsFormats["doxygen"] = eDoxygen;
116  optionsFormats["doxygen_component_pages"] = eDoxygenComponentPages;
117  optionsFormats["config"] = eConfig;
118  optionsFormats["xml"] = eXml;
119 
120  if (vm.count("options-format"))
121  {
122  std::string optionsFormatName = vm["options-format"].as<std::string>();
123  optionsFormatName = simox::alg::to_lower(optionsFormatName);
124 
125  if (optionsFormats.find(optionsFormatName) != optionsFormats.end())
126  {
127  options.format = optionsFormats[optionsFormatName];
128  }
129  else
130  {
131  std::cout << "Unknown options-format:" << optionsFormatName << std::endl;
132 
133  options.showHelp = false;
134  options.error = true;
135  return options;
136  }
137  }
138  else
139  {
140  options.format = eOptionsDetailed;
141  }
142  }
143  else if (vm.count("help") || properties->getProperty("Ice.Default.Locator").empty())
144  {
145  options.format = eHelpBrief;
146  }
147  else if (vm.count("version"))
148  {
149  options.showVersion = true;
150  return options;
151  }
152  else
153  {
154  return options;
155  }
156 
157  if (vm.count("output-file"))
158  {
159  std::string outputFilePath = vm["output-file"].as<std::string>();
160  options.outfile = outputFilePath;
161  }
162 
163  options.showHelp = true;
164 
165  return options;
166  }
167 
168 
169  void ApplicationOptions::showHelp(ApplicationPtr application, ArmarXDummyManagerPtr dummyManager, ApplicationOptions::Options options, Ice::PropertiesPtr properties, std::ostream& out)
170  {
171  OptionsFormat optionsFormat = options.format;
172  boost::program_options::options_description& desc = *options.description;
173 
174  PropertyDefinitionFormatter* defFormatter = nullptr;
175  PropertyDefinitionContainerFormatter* pdcFormatter = nullptr;
176  PropertyUserList puList;
177 
178  switch (optionsFormat)
179  {
180  case eHelpBrief:
181  {
182  PropertyDefinitionBriefHelpFormatter* definitionFormatter;
184 
185  // help options
186  out << desc << std::endl;
187 
188  out << "By default the following config files are loaded:\n ";
189  Ice::StringSeq paths = application->GetDefaultsPaths();
190 
191  for (auto p : paths)
192  {
193  out << "\t" << p << std::endl;
194  }
195 
196  out << std::endl;
197  // application options
198  boost::program_options::options_description applicationDesc("Application properties");
199 
200  definitionFormatter = new PropertyDefinitionBriefHelpFormatter(&applicationDesc);
201  containerFormatter = new PropertyDefinitionContainerBriefHelpFormatter(*definitionFormatter, &applicationDesc);
202 
203  puList.insert(puList.begin(), application);
204  out << containerFormatter->formatPropertyUsers(puList) << std::endl;
205 
206  // component options
207  boost::program_options::options_description componentDesc("Component properties");
208 
209  delete definitionFormatter;
210  delete containerFormatter;
211 
212  definitionFormatter = new PropertyDefinitionBriefHelpFormatter(&componentDesc);
213  containerFormatter = new PropertyDefinitionContainerBriefHelpFormatter(*definitionFormatter, &componentDesc);
214 
215  puList = getPropertyUsers(dummyManager);
216  out << containerFormatter->formatPropertyUsers(puList) << std::endl;
217 
218  delete definitionFormatter;
219  delete containerFormatter;
220  }
221 
222  return;
223 
224  case eOptionsDetailed:
225  defFormatter = new PropertyDefinitionHelpFormatter();
226  pdcFormatter = new PropertyDefinitionContainerFormatter(*defFormatter);
227  break;
228 
229  case eXml:
230  defFormatter = new PropertyDefinitionXmlFormatter();
231  pdcFormatter = new PropertyDefinitionContainerFormatter(*defFormatter);
232  break;
233 
234  case eDoxygen:
235  defFormatter = new PropertyDefinitionDoxygenFormatter();
236  pdcFormatter = new PropertyDefinitionContainerFormatter(*defFormatter);
237  break;
238 
241  pdcFormatter = new PropertyDefinitionContainerFormatter(*defFormatter);
242  break;
243 
244  case eConfig:
245  defFormatter = new PropertyDefinitionConfigFormatter();
246  pdcFormatter = new PropertyDefinitionContainerFormatter(*defFormatter);
247  break;
248  default:
249  ARMARX_WARNING << "ConfigFormatter with id " << (int)optionsFormat << " not available";
250  return;
251  }
252 
253  pdcFormatter->setProperties(properties);
254  // add this application to the property user list
255  puList = getPropertyUsers(dummyManager);
256  puList.insert(puList.begin(), application);
257 
258  out << pdcFormatter->formatPropertyUsers(puList) << std::endl;
259 
260  delete defFormatter;
261  delete pdcFormatter;
262  }
263 
264 
266  {
267  PropertyUserList propertyUsers;
268 
269  std::vector<ManagedIceObjectPtr> objects = dummyManager->getManagedObjects();
270 
271  for (auto& it : objects)
272  {
273  PropertyUserPtr user = PropertyUserPtr::dynamicCast(it);
274 
275  if (user)
276  {
277  propertyUsers.push_back(user);
278  }
279 
280  ComponentPtr comp = ComponentPtr::dynamicCast(it);
281  if (comp)
282  {
283  auto additionalPropertyUsers = comp->getAdditionalPropertyUsers();
284  propertyUsers.insert(propertyUsers.end(), additionalPropertyUsers.begin(), additionalPropertyUsers.end());
285  }
286  }
287 
288  return propertyUsers;
289  }
290 }
armarx::ApplicationOptions::getPropertyUsers
PropertyUserList getPropertyUsers(ArmarXDummyManagerPtr dummyManager)
Return the list of property users.
Definition: ApplicationOptions.cpp:265
armarx::PropertyDefinitionDoxygenFormatter
PropertyDefinitionDoxygenFormatter creates doxygen output.
Definition: PropertyDefinitionDoxygenFormatter.h:40
armarx::ApplicationOptions::mergeProperties
Ice::PropertiesPtr mergeProperties(Ice::PropertiesPtr properties, int argc, char *argv[])
Merge command line options into properties.
Definition: ApplicationOptions.cpp:59
PropertyDefinitionXmlFormatter.h
armarx::PropertyDefinitionContainerFormatter::setProperties
void setProperties(Ice::PropertiesPtr properties)
Definition: PropertyDefinitionContainerFormatter.h:52
PropertyDefinitionContainerFormatter.h
armarx::PropertyDefinitionContainerBriefHelpFormatter::formatPropertyUsers
std::string formatPropertyUsers(const PropertyUserList &propertyUserList) override
Definition: PropertyDefinitionContainerBriefHelpFormatter.cpp:50
armarx::PropertyDefinitionContainerFormatter::formatPropertyUsers
virtual std::string formatPropertyUsers(const PropertyUserList &propertyUserList)
Definition: PropertyDefinitionContainerFormatter.h:66
PropertyDefinitionDoxygenFormatter.h
armarx::ApplicationOptions::eHelpBrief
@ eHelpBrief
Definition: ApplicationOptions.h:64
armarx::ApplicationOptions::parseHelpOptions
Options parseHelpOptions(Ice::PropertiesPtr properties, int argc, char *argv[])
Parse the help options.
Definition: ApplicationOptions.cpp:68
armarx::PropertyDefinitionContainerFormatter
PropertyDefinitionContainerFormatter.
Definition: PropertyDefinitionContainerFormatter.h:41
PropertyDefinitionFormatter.h
boost::program_options
Definition: ApplicationOptions.h:37
armarx::ApplicationOptions::OptionsFormat
OptionsFormat
Help format to display.
Definition: ApplicationOptions.h:62
armarx::ApplicationOptions::Options::showHelp
bool showHelp
Definition: ApplicationOptions.h:78
ApplicationOptions.h
PropertyDefinitionHelpFormatter.h
armarx::ApplicationOptions::eConfig
@ eConfig
Definition: ApplicationOptions.h:68
armarx::ApplicationOptions::Options::description
std::shared_ptr< boost::program_options::options_description > description
Definition: ApplicationOptions.h:83
IceInternal::Handle< ::Ice::Properties >
ArmarXDummyManager.h
armarx::ApplicationOptions::eDoxygenComponentPages
@ eDoxygenComponentPages
Definition: ApplicationOptions.h:67
armarx::armem::server::ltm::mongodb::util::store
void store(const mongocxx::database &db, const armem::wm::Memory &m)
Definition: operations.cpp:237
armarx::PropertyDefinitionHelpFormatter
PropertyDefinitionHelpFormatter.
Definition: PropertyDefinitionHelpFormatter.h:40
armarx::ApplicationOptions::eDoxygen
@ eDoxygen
Definition: ApplicationOptions.h:66
armarx::PropertyUserList
std::vector< PropertyUserPtr > PropertyUserList
UserProperty list type.
Definition: PropertyUser.h:262
ManagedIceObject.h
PropertyUser.h
armarx::ApplicationOptions::Options::format
OptionsFormat format
Definition: ApplicationOptions.h:82
armarx::PropertyDefinitionContainerBriefHelpFormatter
PropertyDefinitionContainerBriefHelpFormatter.
Definition: PropertyDefinitionContainerBriefHelpFormatter.h:50
armarx::ApplicationOptions::Options::showVersion
bool showVersion
Definition: ApplicationOptions.h:79
PropertyDefinitionConfigFormatter.h
armarx::ApplicationOptions::eXml
@ eXml
Definition: ApplicationOptions.h:69
armarx::ApplicationOptions::Options
Stucture containing the parsed options of the application.
Definition: ApplicationOptions.h:76
PropertyDefinitionBriefHelpFormatter.h
armarx::PropertyDefinitionConfigFormatter
PropertyDefinitionConfigFormatter.
Definition: PropertyDefinitionConfigFormatter.h:40
PropertyDefinitionDoxygenComponentPagesFormatter.h
armarx::PropertyDefinitionBriefHelpFormatter
PropertyDefinitionBriefHelpFormatter.
Definition: PropertyDefinitionBriefHelpFormatter.h:46
armarx::ApplicationOptions::Options::error
bool error
Definition: ApplicationOptions.h:80
armarx::PropertyDefinitionDoxygenComponentPagesFormatter
PropertyDefinitionDoxygenComponentPagesFormatter creates doxygen output with an own group for every c...
Definition: PropertyDefinitionDoxygenComponentPagesFormatter.h:37
IceUtil::Handle< Application >
LogSender.h
PropertyDefinitionContainerBriefHelpFormatter.h
Logging.h
armarx::ApplicationOptions::showHelp
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.
Definition: ApplicationOptions.cpp:169
armarx::ApplicationOptions::Options::outfile
std::string outfile
Definition: ApplicationOptions.h:81
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::PropertyDefinitionFormatter
PropertyDefinitionFormatter is the base class for all formatters of PropertyDefinitions.
Definition: PropertyDefinitionFormatter.h:38
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
Application.h
armarx::ApplicationOptions::eOptionsDetailed
@ eOptionsDetailed
Definition: ApplicationOptions.h:65
armarx::PropertyDefinitionXmlFormatter
PropertyDefinitionXmlFormatter.
Definition: PropertyDefinitionXmlFormatter.h:40