DDS  ver. 3.6
dds-topology/src/Options.h
Go to the documentation of this file.
1 // Copyright 2014 GSI, Inc. All rights reserved.
2 //
3 //
4 //
5 #ifndef DDSOPTIONS_H
6 #define DDSOPTIONS_H
7 
8 // BOOST
9 #include <boost/filesystem/operations.hpp>
10 #include <boost/filesystem/path.hpp>
11 #include <boost/program_options/options_description.hpp>
12 #include <boost/program_options/parsers.hpp>
13 // DDS
14 #include "BoostHelper.h"
15 #include "ProtocolCommands.h"
16 #include "Version.h"
17 
18 namespace bpo = boost::program_options;
19 
20 namespace dds
21 {
22  namespace topology_cmd
23  {
24  enum class ETopologyCmdType
25  {
26  UNKNOWN = -1,
27  UPDATE = 0,
28  ACTIVATE,
29  STOP,
30  VALIDATE,
33  };
34 
36  typedef struct SOptions
37  {
40  , m_verbose(false)
41  , m_bDisableValidation(false)
42  , m_sid(boost::uuids::nil_uuid())
43  {
44  }
45 
47  std::string m_sTopoFile;
48  bool m_verbose;
50  boost::uuids::uuid m_sid;
51  } SOptions_t;
52 
53  // Command line parser
54  inline bool ParseCmdLine(int _argc, char* _argv[], SOptions* _options)
55  {
56  if (nullptr == _options)
57  throw std::runtime_error("Internal error: options' container is empty.");
58 
59  // Generic options
60  bpo::options_description options("dds-agent-cmd options");
61  options.add_options()("help,h", "Produce help message");
62  options.add_options()("version,v", "Version information");
63  options.add_options()("session,s", bpo::value<std::string>(), "DDS Session ID");
64  options.add_options()("update,u",
65  bpo::value<std::string>(&_options->m_sTopoFile),
66  "Define a topology to update currently active topology.");
67  options.add_options()("disable-validation",
68  bpo::bool_switch(&_options->m_bDisableValidation),
69  "Disable topology valiadation.");
70  options.add_options()("activate",
71  bpo::value<std::string>(&_options->m_sTopoFile),
72  "Request to activate agents, i.e. distribute and start user tasks.");
73  options.add_options()("stop", "Request to stop execution of user tasks.");
74  options.add_options()("validate",
75  bpo::value<std::string>(&_options->m_sTopoFile),
76  "Validate topology file against XSD schema.");
77  options.add_options()("required-agents",
78  bpo::value<std::string>(&_options->m_sTopoFile),
79  "Get the required number of agents for the topology.");
80  options.add_options()(
81  "topology-name", bpo::value<std::string>(&_options->m_sTopoFile), "Get the name of the topology.");
82  options.add_options()("verbose,V", "Verbose output");
83 
84  // Parsing command-line
85  bpo::variables_map vm;
86  bpo::store(bpo::command_line_parser(_argc, _argv).options(options).run(), vm);
87  bpo::notify(vm);
88 
89  dds::misc::conflicting_options(vm, "activate", "stop");
90  dds::misc::conflicting_options(vm, "update", "stop");
91  dds::misc::conflicting_options(vm, "update", "activate");
92 
93  if (vm.count("help") || vm.empty())
94  {
95  LOG(dds::misc::log_stdout) << options;
96  return false;
97  }
98  if (vm.count("version"))
99  {
100  LOG(dds::misc::log_stdout) << dds::misc::DDSVersionInfoString();
101  return false;
102  }
103  if (vm.count("session"))
104  _options->m_sid = boost::uuids::string_generator()(vm["session"].as<std::string>());
105  if (vm.count("verbose"))
106  {
107  _options->m_verbose = true;
108  }
109  if (_options->m_bDisableValidation)
110  {
111  if (!vm.count("activate") && !vm.count("update") && !vm.count("required-agents") &&
112  !vm.count("topology-name"))
113  {
114  throw std::runtime_error("--disable-validation must be used together with --activate, --update, "
115  "--required-agents or --topology-name");
116  }
117  }
118  if (vm.count("validate") && !_options->m_sTopoFile.empty())
119  {
121  // make absolute path
122  boost::filesystem::path pathTopoFile(_options->m_sTopoFile);
123  _options->m_sTopoFile = boost::filesystem::absolute(pathTopoFile).string();
124  return true;
125  }
126  if (vm.count("required-agents") && !_options->m_sTopoFile.empty())
127  {
129  // make absolute path
130  boost::filesystem::path pathTopoFile(_options->m_sTopoFile);
131  _options->m_sTopoFile = boost::filesystem::absolute(pathTopoFile).string();
132  return true;
133  }
134  if (vm.count("topology-name") && !_options->m_sTopoFile.empty())
135  {
137  // make absolute path
138  boost::filesystem::path pathTopoFile(_options->m_sTopoFile);
139  _options->m_sTopoFile = boost::filesystem::absolute(pathTopoFile).string();
140  return true;
141  }
142  if (vm.count("update") || vm.count("activate"))
143  {
144  // check, that topo file exists
145  if (!boost::filesystem::exists(_options->m_sTopoFile))
146  {
147  std::string sMsg("Can't find the topo file: ");
148  sMsg += _options->m_sTopoFile;
149  throw std::runtime_error(sMsg);
150  }
151 
152  if (vm.count("update"))
153  {
155  }
156  else if (vm.count("activate"))
157  {
159  }
160  // make absolute path
161  boost::filesystem::path pathTopoFile(_options->m_sTopoFile);
162  _options->m_sTopoFile = boost::filesystem::absolute(pathTopoFile).string();
163  return true;
164  }
165  else if (vm.count("stop"))
166  {
168  _options->m_sTopoFile = "";
169  return true;
170  }
171  else
172  {
173  LOG(dds::misc::log_stdout) << options;
174  return false;
175  }
176 
177  return true;
178  }
179  } // namespace topology_cmd
180 } // namespace dds
181 #endif
void conflicting_options(const boost::program_options::variables_map &_vm, const char *_opt1, const char *_opt2)
The conflicting_options function used to check that 'opt1' and 'opt2' are not specified at the same t...
Definition: BoostHelper.h:40
ETopologyCmdType m_topologyCmd
Definition: dds-topology/src/Options.h:46
struct dds::topology_cmd::SOptions SOptions_t
dds-agent-cmd's container of options
boost::uuids::uuid m_sid
Definition: dds-topology/src/Options.h:50
#define LOG(severity)
Definition: Logger.h:34
SOptions()
Definition: dds-topology/src/Options.h:38
Definition: def.h:151
bool m_verbose
Definition: dds-topology/src/Options.h:48
bool ParseCmdLine(int _argc, char *_argv[], SOptions *_options)
Definition: dds-topology/src/Options.h:54
Miscellaneous functions and helpers are located here.
Definition: AgentConnectionManager.h:13
ETopologyCmdType
Definition: dds-topology/src/Options.h:24
dds-agent-cmd's container of options
Definition: dds-topology/src/Options.h:36
std::string m_sTopoFile
Definition: dds-topology/src/Options.h:47
bool m_bDisableValidation
Definition: dds-topology/src/Options.h:49