DDS  ver. 2.0
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 "Res.h"
17 #include "version.h"
18 //=============================================================================
19 namespace bpo = boost::program_options;
20 //=============================================================================
21 namespace dds
22 {
23  namespace topology_cmd
24  {
25  enum class ETopologyCmdType
26  {
27  UNKNOWN = -1,
28  UPDATE = 0,
29  ACTIVATE,
30  STOP,
31  VALIDATE
32  };
33 
35  typedef struct SOptions
36  {
38  : m_topologyCmd(ETopologyCmdType::UNKNOWN)
39  , m_verbose(false)
40  , m_bDisableValidation(false)
41  , m_sid(boost::uuids::nil_uuid())
42  {
43  }
44 
46  std::string m_sTopoFile;
47  bool m_verbose;
49  boost::uuids::uuid m_sid;
50  } SOptions_t;
51  //=============================================================================
52  inline void PrintVersion()
53  {
54  LOG(MiscCommon::log_stdout) << " v" << PROJECT_VERSION_STRING << "\n"
55  << "DDS configuration"
56  << " v" << USER_DEFAULTS_CFG_VERSION << "\n"
58  }
59  //=============================================================================
60  // Command line parser
61  inline bool ParseCmdLine(int _argc, char* _argv[], SOptions* _options)
62  {
63  if (nullptr == _options)
64  throw std::runtime_error("Internal error: options' container is empty.");
65 
66  // Generic options
67  bpo::options_description options("dds-agent-cmd options");
68  options.add_options()("help,h", "Produce help message");
69  options.add_options()("version,v", "Version information");
70  options.add_options()("session,s", bpo::value<std::string>(), "DDS Session ID");
71  options.add_options()("update,u",
72  bpo::value<std::string>(&_options->m_sTopoFile),
73  "Define a topology to update currently active topology.");
74  options.add_options()("disable-validation",
75  bpo::bool_switch(&_options->m_bDisableValidation),
76  "Disable topology valiadation.");
77  options.add_options()("activate",
78  bpo::value<std::string>(&_options->m_sTopoFile),
79  "Request to activate agents, i.e. distribute and start user tasks.");
80  options.add_options()("stop", "Request to stop execution of user tasks.");
81  options.add_options()("validate",
82  bpo::value<std::string>(&_options->m_sTopoFile),
83  "Validate topology file against XSD schema.");
84  options.add_options()("verbose,V", "Verbose output");
85 
86  // Parsing command-line
87  bpo::variables_map vm;
88  bpo::store(bpo::command_line_parser(_argc, _argv).options(options).run(), vm);
89  bpo::notify(vm);
90 
91  MiscCommon::BOOSTHelper::conflicting_options(vm, "activate", "stop");
93  MiscCommon::BOOSTHelper::conflicting_options(vm, "update", "activate");
94 
95  if (vm.count("help") || vm.empty())
96  {
97  LOG(MiscCommon::log_stdout) << options;
98  return false;
99  }
100  if (vm.count("version"))
101  {
102  PrintVersion();
103  return false;
104  }
105  if (vm.count("session"))
106  _options->m_sid = boost::uuids::string_generator()(vm["session"].as<std::string>());
107  if (vm.count("verbose"))
108  {
109  _options->m_verbose = true;
110  }
111  if (_options->m_bDisableValidation)
112  {
113  if (!vm.count("activate") && !vm.count("update"))
114  {
115  throw std::runtime_error("--disable-validation must be used together with --activate or --update");
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("update") || vm.count("activate"))
127  {
128  // check, that topo file exists
129  if (!boost::filesystem::exists(_options->m_sTopoFile))
130  {
131  std::string sMsg("Can't find the topo file: ");
132  sMsg += _options->m_sTopoFile;
133  throw std::runtime_error(sMsg);
134  }
135 
136  if (vm.count("update"))
137  {
139  }
140  else if (vm.count("activate"))
141  {
143  }
144  // make absolute path
145  boost::filesystem::path pathTopoFile(_options->m_sTopoFile);
146  _options->m_sTopoFile = boost::filesystem::absolute(pathTopoFile).string();
147  return true;
148  }
149  else if (vm.count("stop"))
150  {
152  _options->m_sTopoFile = "";
153  return true;
154  }
155  else
156  {
157  LOG(MiscCommon::log_stdout) << options;
158  return false;
159  }
160 
161  return true;
162  }
163  }
164 }
165 #endif
Definition: def.h:154
ETopologyCmdType m_topologyCmd
Definition: dds-topology/src/Options.h:45
struct dds::topology_cmd::SOptions SOptions_t
dds-agent-cmd&#39;s container of options
boost::uuids::uuid m_sid
Definition: dds-topology/src/Options.h:49
#define LOG(severity)
Definition: Logger.h:54
SOptions()
Definition: dds-topology/src/Options.h:37
bool m_verbose
Definition: dds-topology/src/Options.h:47
bool ParseCmdLine(int _argc, char *_argv[], SOptions *_options)
Definition: dds-topology/src/Options.h:61
const LPCSTR g_cszReportBugsAddr("Report bugs/comments to fairroot@gsi.de")
Definition: dds-agent/src/AgentConnectionManager.h:18
ETopologyCmdType
Definition: dds-topology/src/Options.h:25
dds-agent-cmd&#39;s container of options
Definition: dds-topology/src/Options.h:35
void PrintVersion()
Definition: dds-topology/src/Options.h:52
void conflicting_options(const boost::program_options::variables_map &_vm, const char *_opt1, const char *_opt2)
The conflicting_options function used to check that &#39;opt1&#39; and &#39;opt2&#39; are not specified at the same t...
Definition: BOOSTHelper.h:62
std::string m_sTopoFile
Definition: dds-topology/src/Options.h:46
bool m_bDisableValidation
Definition: dds-topology/src/Options.h:48