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