DDS  ver. 1.6
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_bNeedAgentsNumber(false)
31  , m_bNeedAgentsList(false)
32  , m_bNeedPropList(false)
33  , m_bNeedPropValues(false)
34  , m_propertyID()
35  {
36  }
37 
44  std::string m_propertyID;
45  } SOptions_t;
46  //=============================================================================
47  inline void PrintVersion()
48  {
49  LOG(MiscCommon::log_stdout) << " v" << PROJECT_VERSION_STRING << "\n"
50  << "DDS configuration"
51  << " v" << USER_DEFAULTS_CFG_VERSION << "\n"
53  }
54  //=============================================================================
55  // Command line parser
56  inline bool ParseCmdLine(int _argc, char* _argv[], SOptions* _options) throw(std::exception)
57  {
58  if (nullptr == _options)
59  throw std::runtime_error("Internal error: options' container is empty.");
60 
61  // Generic options
62  bpo::options_description options("dds-info options");
63  options.add_options()("help,h", "Produce help message");
64  options.add_options()("version,v", "Version information");
65  options.add_options()("commander-pid",
66  bpo::bool_switch(&_options->m_bNeedCommanderPid),
67  "Return the pid of the commander server");
68  options.add_options()("status",
69  bpo::bool_switch(&_options->m_bNeedDDSStatus),
70  "Query current status of DDS commander server");
71  options.add_options()("agents-number,n",
72  bpo::bool_switch(&_options->m_bNeedAgentsNumber),
73  "Returns a number of online agents");
74  options.add_options()("agents-list,l",
75  bpo::bool_switch(&_options->m_bNeedAgentsList),
76  "Show detailed info about all online agents");
77  options.add_options()(
78  "prop-list", bpo::bool_switch(&_options->m_bNeedPropList), "Returns a property list from all agents.");
79  options.add_options()("prop-values",
80  bpo::bool_switch(&_options->m_bNeedPropValues),
81  "Returns a key-value pairs from all agents.");
82  options.add_options()("prop-id",
83  bpo::value<std::string>(&_options->m_propertyID),
84  "Specify property IDs that have to be returned.");
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  // check for non-defaulted arguments
92  bpo::variables_map::const_iterator found =
93  find_if(vm.begin(), vm.end(), [](const bpo::variables_map::value_type& _v) {
94  return (!_v.second.defaulted());
95  });
96 
97  if (vm.count("help") || vm.end() == found)
98  {
99  LOG(MiscCommon::log_stdout) << options;
100  return false;
101  }
102  if (vm.count("version"))
103  {
104  PrintVersion();
105  return false;
106  }
107  if (vm.count("prop-id") && !_options->m_bNeedPropValues)
108  {
109  LOG(MiscCommon::log_stdout) << "Option prop-id has to be used together with prop-values.";
110  return false;
111  }
112 
113  return true;
114  }
115  }
116 }
117 #endif
Definition: def.h:154
dds-commander&#39;s container of options
Definition: dds-info/src/Options.h:25
void PrintVersion()
Definition: dds-info/src/Options.h:47
bool m_bNeedAgentsList
Definition: dds-info/src/Options.h:41
#define LOG(severity)
Definition: Logger.h:54
const LPCSTR g_cszReportBugsAddr("Report bugs/comments to fairroot@gsi.de")
Definition: dds-agent/src/AgentConnectionManager.h:16
bool m_bNeedPropValues
Definition: dds-info/src/Options.h:43
bool m_bNeedAgentsNumber
Definition: dds-info/src/Options.h:40
bool m_bNeedDDSStatus
Definition: dds-info/src/Options.h:39
SOptions()
Definition: dds-info/src/Options.h:27
bool m_bNeedPropList
Definition: dds-info/src/Options.h:42
std::string m_propertyID
Definition: dds-info/src/Options.h:44
struct dds::info_cmd::SOptions SOptions_t
dds-commander&#39;s container of options
bool m_bNeedCommanderPid
Definition: dds-info/src/Options.h:38
bool ParseCmdLine(int _argc, char *_argv[], SOptions *_options)
Definition: dds-info/src/Options.h:56