DDS  ver. 3.4
dds-agent-cmd/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 "BOOSTHelper.h"
13 #include "ProtocolCommands.h"
14 #include "Res.h"
15 #include "version.h"
16 //=============================================================================
17 namespace bpo = boost::program_options;
18 //=============================================================================
19 namespace dds
20 {
21  enum class EAgentCmdType
22  {
23  UNKNOWN = -1,
24  GETLOG = 0,
25  };
26  typedef std::map<EAgentCmdType, std::string> mapAgentCmdTypeCodes_t;
28 
29  // A custom streamer to help boost program options to convert string options to EAgentCmdType
30  inline std::istream& operator>>(std::istream& _in, EAgentCmdType& _agentCmd)
31  {
32  std::string token;
33  _in >> token;
34  if (token == "getlog")
35  _agentCmd = EAgentCmdType::GETLOG;
36  else
37  throw bpo::invalid_option_value(token);
38  return _in;
39  }
40 
41  inline std::ostream& operator<<(std::ostream& _out, EAgentCmdType& _agentCmd)
42  {
43  auto found = AgentCmdTypeCodeToString.find(_agentCmd);
44  if (found != AgentCmdTypeCodeToString.end())
45  _out << found->second;
46 
47  return _out;
48  }
49 
50  namespace agent_cmd_cmd
51  {
53  typedef struct SOptions
54  {
57  , m_agentCmd(EAgentCmdType::UNKNOWN)
58  , m_verbose(false)
59  , m_sid(boost::uuids::nil_uuid())
60  {
61  }
62 
65  bool m_verbose;
66  boost::uuids::uuid m_sid;
67  } SOptions_t;
68  //=============================================================================
69  inline void PrintVersion()
70  {
71  LOG(MiscCommon::log_stdout) << " v" << PROJECT_VERSION_STRING << "\n"
72  << "DDS configuration"
73  << " v" << USER_DEFAULTS_CFG_VERSION << "\n"
75  }
76  //=============================================================================
77  // Command line parser
78  inline bool ParseCmdLine(int _argc, char* _argv[], SOptions* _options)
79  {
80  if (nullptr == _options)
81  throw std::runtime_error("Internal error: options' container is empty.");
82 
83  // Generic options
84  bpo::options_description options("dds-agent-cmd options");
85  options.add_options()("help,h", "Produce help message");
86  options.add_options()("version,v", "Version information");
87  options.add_options()("session,s", bpo::value<std::string>(), "DDS Session ID");
88  options.add_options()("verbose", "Verbose output");
89  options.add_options()(
90  "command",
91  bpo::value<EAgentCmdType>(&_options->m_agentCmd),
92  "The command is a name of a dds-agent-cmd command."
93  " Can be one of the following: getlog.\n"
94  "For user's convenience it is allowed to call dds-agent-cmd without \"--command\" option"
95  " by just specifying the command name directly, like:\ndds-agent-cmd getlog\n\n"
96  "Commands:\n"
97  " getlog: \tRetrieve log files from worker nodes. Files will be saved in ~/.DDS/log/agents\n");
98  options.add_options()("all,a", "Send command to all active agents");
99 
100  bpo::positional_options_description positional;
101  positional.add("command", -1);
102 
103  // Parsing command-line
104  bpo::variables_map vm;
105  bpo::store(bpo::command_line_parser(_argc, _argv).options(options).positional(positional).run(), vm);
106  bpo::notify(vm);
107 
108  if (vm.count("help") || vm.empty())
109  {
110  LOG(MiscCommon::log_stdout) << options;
111  return false;
112  }
113  if (vm.count("version"))
114  {
115  PrintVersion();
116  return false;
117  }
118  if (vm.count("verbose"))
119  {
120  _options->m_verbose = true;
121  }
122  if (!vm.count("command") || _options->m_agentCmd == EAgentCmdType::UNKNOWN)
123  {
124  LOG(MiscCommon::log_stderr) << "Nothing to do. Please, specify a command"
125  << "\n\n"
126  << options;
127  return false;
128  }
129  if (vm.count("all"))
130  {
131  _options->m_sendCommandToAllAgents = true;
132  }
133  if (vm.count("session"))
134  {
135  _options->m_sid = boost::uuids::string_generator()(vm["session"].as<std::string>());
136  }
137 
138  return true;
139  }
140  } // namespace agent_cmd_cmd
141 } // namespace dds
142 #endif
Definition: def.h:156
Definition: def.h:154
dds-agent-cmd's container of options
Definition: dds-agent-cmd/src/Options.h:53
EAgentCmdType
Definition: dds-agent-cmd/src/Options.h:21
bool m_sendCommandToAllAgents
Definition: dds-agent-cmd/src/Options.h:63
std::istream & operator>>(std::istream &_in, EAgentCmdType &_agentCmd)
Definition: dds-agent-cmd/src/Options.h:30
struct dds::agent_cmd_cmd::SOptions SOptions_t
dds-agent-cmd's container of options
bool m_verbose
Definition: dds-agent-cmd/src/Options.h:65
#define LOG(severity)
Definition: Logger.h:56
const LPCSTR g_cszReportBugsAddr("Report bugs/comments to fairroot@gsi.de")
Definition: AgentConnectionManager.h:13
bool ParseCmdLine(int _argc, char *_argv[], SOptions *_options)
Definition: dds-agent-cmd/src/Options.h:78
void PrintVersion()
Definition: dds-agent-cmd/src/Options.h:69
const mapAgentCmdTypeCodes_t AgentCmdTypeCodeToString
Definition: dds-agent-cmd/src/Options.h:27
EAgentCmdType m_agentCmd
Definition: dds-agent-cmd/src/Options.h:64
std::map< EAgentCmdType, std::string > mapAgentCmdTypeCodes_t
Definition: dds-agent-cmd/src/Options.h:26
std::ostream & operator<<(std::ostream &_out, EAgentCmdType &_agentCmd)
Definition: dds-agent-cmd/src/Options.h:41
SOptions()
Definition: dds-agent-cmd/src/Options.h:55
boost::uuids::uuid m_sid
Definition: dds-agent-cmd/src/Options.h:66