DDS  ver. 3.6
dds-submit/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 #include <boost/property_tree/ini_parser.hpp>
14 #include <boost/property_tree/ptree.hpp>
15 // DDS
16 #include "BoostHelper.h"
17 #include "ProtocolCommands.h"
18 #include "SubmitCmd.h"
19 #include "SysHelper.h"
20 #include "Version.h"
21 
22 namespace bpo = boost::program_options;
23 
24 namespace dds
25 {
26  namespace submit_cmd
27  {
29  typedef struct SOptions
30  {
31  std::string m_sRMS{ "localhost" };
32  std::string m_sCfgFile;
33  std::string m_sPath;
34  size_t m_number{ 0 };
35  size_t m_slots{ 0 };
36  bool m_bListPlugins{ false };
37  boost::uuids::uuid m_sid = boost::uuids::nil_uuid();
38  } SOptions_t;
39  //=============================================================================
40  inline std::ostream& operator<<(std::ostream& _stream, const SOptions& val)
41  {
42  return _stream << "\nRMS: " << val.m_sRMS << "\nPlug-in's configuration file: " << val.m_sCfgFile;
43  }
44 
45  // Command line parser
46  inline bool ParseCmdLine(int _argc, char* _argv[], SOptions* _options)
47  {
48  if (nullptr == _options)
49  throw std::runtime_error("Internal error: options' container is empty.");
50 
51  // Generic options
52  bpo::options_description options("dds-submit options");
53  options.add_options()("help,h", "Produce help message");
54  options.add_options()("version,v", "Version information");
55  options.add_options()("session,s", bpo::value<std::string>(), "DDS Session ID");
56  options.add_options()(
57  "list,l", bpo::bool_switch(&_options->m_bListPlugins), "List all available RMS plug-ins");
58  options.add_options()("rms,r",
59  bpo::value<std::string>(&_options->m_sRMS),
60  "Defines a destination resource "
61  "management system plug-in. Use "
62  "\"--list\" to find out names "
63  "of available RMS plug-ins.");
64  options.add_options()("config,c",
65  bpo::value<std::string>(&_options->m_sCfgFile),
66  "A plug-in's configuration file. It can be used to provide additional RMS options");
67  options.add_options()("path",
68  bpo::value<std::string>(&_options->m_sPath),
69  "A plug-in's directory search path. It can be used for external RMS plug-ins.");
70  options.add_options()("number,n",
71  bpo::value<size_t>(&_options->m_number)->default_value(1),
72  "Defines a number of agents to spawn."
73  "If 0 is provided as an argument, then a number of available logical cores will be "
74  "used.\n");
75  options.add_options()(
76  "slots", bpo::value<size_t>(&_options->m_slots), "Defines a number of task slots per agent.");
77 
78  // Parsing command-line
79  bpo::variables_map vm;
80  bpo::store(bpo::command_line_parser(_argc, _argv).options(options).run(), vm);
81  bpo::notify(vm);
82 
83  dds::misc::conflicting_options(vm, "list", "rms");
84  dds::misc::conflicting_options(vm, "list", "config");
85  dds::misc::conflicting_options(vm, "list", "slots");
86 
87  // check for non-defaulted arguments
88  bpo::variables_map::const_iterator found =
89  find_if(vm.begin(),
90  vm.end(),
91  [](const bpo::variables_map::value_type& _v) { return (!_v.second.defaulted()); });
92 
93  if (vm.count("help") || vm.end() == found)
94  {
95  LOG(dds::misc::log_stdout) << options;
96  return false;
97  }
98  // "rms" requires either "config" or "number"
99  if (vm.count("rms") && !vm.count("config") && !vm.count("number") && !vm.count("slots"))
100  {
101  LOG(dds::misc::log_stderr) << "--rms options requires either --config, --number, or --slots";
102  LOG(dds::misc::log_stdout) << options;
103  return false;
104  }
105  if (vm.count("version"))
106  {
107  LOG(dds::misc::log_stdout) << dds::misc::DDSVersionInfoString();
108  return false;
109  }
110 
111  if (vm.count("session"))
112  _options->m_sid = boost::uuids::string_generator()(vm["session"].as<std::string>());
113 
114  // RMS plug-ins are always lower cased
115  boost::to_lower(_options->m_sRMS);
116 
117  // make absolute path
118  if (!_options->m_sCfgFile.empty())
119  {
120  boost::filesystem::path pathCfgFile(_options->m_sCfgFile);
121  _options->m_sCfgFile = boost::filesystem::absolute(pathCfgFile).string();
122  }
123  return true;
124  }
125  } // namespace submit_cmd
126 } // namespace dds
127 #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
size_t m_number
Definition: dds-submit/src/Options.h:34
dds-commander's container of options
Definition: dds-submit/src/Options.h:29
#define LOG(severity)
Definition: Logger.h:34
std::string m_sRMS
Definition: dds-submit/src/Options.h:31
Definition: def.h:151
Miscellaneous functions and helpers are located here.
Definition: AgentConnectionManager.h:13
struct dds::submit_cmd::SOptions SOptions_t
dds-commander's container of options
std::string m_sPath
Definition: dds-submit/src/Options.h:33
Definition: def.h:153
boost::uuids::uuid m_sid
Definition: dds-submit/src/Options.h:37
std::string m_sCfgFile
Definition: dds-submit/src/Options.h:32
_T & to_lower(_T &_str)
convert string to lower case.
Definition: MiscUtils.h:249
std::ostream & operator<<(std::ostream &_stream, const SOptions &val)
Definition: dds-submit/src/Options.h:40
bool m_bListPlugins
Definition: dds-submit/src/Options.h:36
size_t m_slots
Definition: dds-submit/src/Options.h:35
bool ParseCmdLine(int _argc, char *_argv[], SOptions *_options)
Definition: dds-submit/src/Options.h:46