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