DDS  ver. 3.4
dds-info/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 // DDS
12 #include "ProtocolCommands.h"
13 #include "Res.h"
14 #include "version.h"
15 // STD
16 #include <string>
17 //=============================================================================
18 namespace bpo = boost::program_options;
19 //=============================================================================
20 namespace dds
21 {
22  namespace info_cmd
23  {
25  typedef struct SOptions
26  {
28  : m_bNeedCommanderPid(false)
29  , m_bNeedDDSStatus(false)
30  , m_bNeedAgentsList(false)
31  , m_bNeedPropList(false)
32  , m_bNeedPropValues(false)
33  , m_bNeedActiveTopology(false)
34  , m_propertyName()
35  , m_sid(boost::uuids::nil_uuid())
36  , m_bNeedActiveCount(false)
37  , m_bNeedIdleCount(false)
38  , m_bNeedExecutingCount(false)
39  , m_nWaitCount(0)
40  {
41  }
42 
49  std::string m_propertyName;
50  boost::uuids::uuid m_sid;
55  } SOptions_t;
56  //=============================================================================
57  inline void PrintVersion()
58  {
59  LOG(MiscCommon::log_stdout) << " v" << PROJECT_VERSION_STRING << "\n"
60  << "DDS configuration"
61  << " v" << USER_DEFAULTS_CFG_VERSION << "\n"
63  }
64  //=============================================================================
65  // Command line parser
66  inline bool ParseCmdLine(int _argc, char* _argv[], SOptions* _options)
67  {
68  if (nullptr == _options)
69  throw std::runtime_error("Internal error: options' container is empty.");
70 
71  // Generic options
72  bpo::options_description options("dds-info options");
73  options.add_options()("help,h", "Produce help message");
74  options.add_options()("version,v", "Version information");
75  options.add_options()("session,s", bpo::value<std::string>(), "DDS Session ID");
76  options.add_options()("commander-pid",
77  bpo::bool_switch(&_options->m_bNeedCommanderPid),
78  "Return the pid of the commander server");
79  options.add_options()("status",
80  bpo::bool_switch(&_options->m_bNeedDDSStatus),
81  "Query current status of DDS commander server");
82  options.add_options()("agents-list,l",
83  bpo::bool_switch(&_options->m_bNeedAgentsList),
84  "Show detailed info about all online agents");
85  options.add_options()(
86  "prop-list", bpo::bool_switch(&_options->m_bNeedPropList), "Returns a property list from all agents.");
87  options.add_options()("prop-values",
88  bpo::bool_switch(&_options->m_bNeedPropValues),
89  "Returns a key-value pairs from all agents.");
90  options.add_options()("prop-name",
91  bpo::value<std::string>(&_options->m_propertyName),
92  "Specify property names that have to be returned.");
93  options.add_options()("active-count,n",
94  bpo::bool_switch(&_options->m_bNeedActiveCount),
95  "Returns a number of online agents.");
96  options.add_options()(
97  "idle-count", bpo::bool_switch(&_options->m_bNeedIdleCount), "Returns a number of idle agents.");
98  options.add_options()("executing-count",
99  bpo::bool_switch(&_options->m_bNeedExecutingCount),
100  "Returns a number of executing agents.");
101  options.add_options()("wait",
102  bpo::value<int>(&_options->m_nWaitCount),
103  "The command will block infinitely until a required number of agents are available. "
104  "Must be used together with --active-count, --idle-count or --executing-count");
105  options.add_options()("active-topology",
106  bpo::bool_switch(&_options->m_bNeedActiveTopology),
107  "Returns the name of the active topology");
108 
109  // Parsing command-line
110  bpo::variables_map vm;
111  bpo::store(bpo::command_line_parser(_argc, _argv).options(options).run(), vm);
112  bpo::notify(vm);
113 
114  // check for non-defaulted arguments
115  bpo::variables_map::const_iterator found =
116  find_if(vm.begin(), vm.end(), [](const bpo::variables_map::value_type& _v) {
117  return (!_v.second.defaulted());
118  });
119 
120  if (vm.count("help") || vm.end() == found)
121  {
122  LOG(MiscCommon::log_stdout) << options;
123  return false;
124  }
125  if (vm.count("version"))
126  {
127  PrintVersion();
128  return false;
129  }
130  if (vm.count("prop-name") && !_options->m_bNeedPropValues)
131  {
132  LOG(MiscCommon::log_stdout) << "Option prop-id has to be used together with prop-values.";
133  return false;
134  }
135 
136  if (vm.count("session"))
137  _options->m_sid = boost::uuids::string_generator()(vm["session"].as<std::string>());
138 
139  size_t numCounters =
140  _options->m_bNeedActiveCount + _options->m_bNeedIdleCount + _options->m_bNeedExecutingCount;
141  if (numCounters > 1)
142  {
144  << "--active-count, --idle-count, --executing-count can't be used together.";
145  return false;
146  }
147 
148  bool needCount =
149  _options->m_bNeedActiveCount || _options->m_bNeedIdleCount || _options->m_bNeedExecutingCount;
150  if (vm.count("wait") && !needCount)
151  {
153  << "Option --wait must be used together with --active-count, --idle-count or --executing-count.";
154  return false;
155  }
156 
157  if (vm.count("wait") && _options->m_nWaitCount <= 0)
158  {
159  LOG(MiscCommon::log_stderr) << "A number of agents to wait must be higher than 0.";
160  return false;
161  }
162 
163  return true;
164  }
165  } // namespace info_cmd
166 } // namespace dds
167 #endif
Definition: def.h:156
Definition: def.h:154
dds-commander's container of options
Definition: dds-info/src/Options.h:25
void PrintVersion()
Definition: dds-info/src/Options.h:57
bool m_bNeedIdleCount
Definition: dds-info/src/Options.h:52
bool m_bNeedAgentsList
Definition: dds-info/src/Options.h:45
bool m_bNeedActiveTopology
Definition: dds-info/src/Options.h:48
#define LOG(severity)
Definition: Logger.h:56
boost::uuids::uuid m_sid
Definition: dds-info/src/Options.h:50
const LPCSTR g_cszReportBugsAddr("Report bugs/comments to fairroot@gsi.de")
Definition: AgentConnectionManager.h:13
bool m_bNeedPropValues
Definition: dds-info/src/Options.h:47
bool m_bNeedActiveCount
Definition: dds-info/src/Options.h:51
bool m_bNeedDDSStatus
Definition: dds-info/src/Options.h:44
SOptions()
Definition: dds-info/src/Options.h:27
bool m_bNeedPropList
Definition: dds-info/src/Options.h:46
struct dds::info_cmd::SOptions SOptions_t
dds-commander's container of options
bool m_bNeedExecutingCount
Definition: dds-info/src/Options.h:53
int m_nWaitCount
Definition: dds-info/src/Options.h:54
bool m_bNeedCommanderPid
Definition: dds-info/src/Options.h:43
std::string m_propertyName
Definition: dds-info/src/Options.h:49
bool ParseCmdLine(int _argc, char *_argv[], SOptions *_options)
Definition: dds-info/src/Options.h:66