DDS  ver. 3.4
dds-stat/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 stat_cmd
23  {
25  typedef struct SOptions
26  {
28  : m_bEnable(false)
29  , m_bDisable(false)
30  , m_bGet(false)
31  {
32  }
33 
34  bool m_bEnable;
35  bool m_bDisable;
36  bool m_bGet;
37  } SOptions_t;
38  //=============================================================================
39  inline void PrintVersion()
40  {
41  LOG(MiscCommon::log_stdout) << " v" << PROJECT_VERSION_STRING << "\n"
42  << "DDS configuration"
43  << " v" << USER_DEFAULTS_CFG_VERSION << "\n"
45  }
46  //=============================================================================
47  // Command line parser
48  inline bool ParseCmdLine(int _argc, char* _argv[], SOptions* _options)
49  {
50  if (nullptr == _options)
51  throw std::runtime_error("Internal error: options' container is empty.");
52 
53  // Generic options
54  bpo::options_description options("dds-stat options");
55  options.add_options()("help,h", "Produce help message");
56  options.add_options()("version,v", "Version information");
57  options.add_options()(
58  "command",
59  bpo::value<std::string>(),
60  "The command is a name of a dds-stat command."
61  " Can be one of the following: enable, disable and get.\n"
62  "For user's convenience it is allowed to call dds-stat without \"--command\" option"
63  " by just specifying the command name directly, like:\ndds-stat enable or dds-stat get.\n\n"
64  "Commands:\n"
65  " enable: \tEnable statistics on the commander server.\n"
66  " disable: \tDisable statistics on the commander server.\n"
67  " get: \tGet statistics from the commander server.\n");
68 
69  //...positional
70  bpo::positional_options_description pd;
71  pd.add("command", 1);
72 
73  // Parsing command-line
74  bpo::variables_map vm;
75  bpo::store(bpo::command_line_parser(_argc, _argv).options(options).positional(pd).run(), vm);
76  bpo::notify(vm);
77 
78  if (vm.count("help") || vm.empty())
79  {
80  LOG(MiscCommon::log_stdout) << options;
81  return false;
82  }
83  if (vm.count("version"))
84  {
85  PrintVersion();
86  return false;
87  }
88 
89  if (vm.count("command"))
90  {
91  std::string cmd = vm["command"].as<std::string>();
92  if (cmd == "enable")
93  {
94  _options->m_bEnable = true;
95  }
96  else if (cmd == "disable")
97  {
98  _options->m_bDisable = true;
99  }
100  else if (cmd == "get")
101  {
102  _options->m_bGet = true;
103  }
104  else
105  {
106  LOG(MiscCommon::log_stderr) << "unknown command: " << cmd << "\n\n" << options;
107  return false;
108  }
109  }
110  else
111  {
112  LOG(MiscCommon::log_stderr) << "Nothing to do\n\n" << options;
113  return false;
114  }
115 
116  return true;
117  }
118  } // namespace stat_cmd
119 } // namespace dds
120 #endif
Definition: def.h:156
Definition: def.h:154
bool m_bGet
Definition: dds-stat/src/Options.h:36
void PrintVersion()
Definition: dds-stat/src/Options.h:39
struct dds::stat_cmd::SOptions SOptions_t
dds-commander's container of options
#define LOG(severity)
Definition: Logger.h:56
dds-commander's container of options
Definition: dds-stat/src/Options.h:25
const LPCSTR g_cszReportBugsAddr("Report bugs/comments to fairroot@gsi.de")
Definition: AgentConnectionManager.h:13
bool m_bDisable
Definition: dds-stat/src/Options.h:35
SOptions()
Definition: dds-stat/src/Options.h:27
bool ParseCmdLine(int _argc, char *_argv[], SOptions *_options)
Definition: dds-stat/src/Options.h:48
bool m_bEnable
Definition: dds-stat/src/Options.h:34