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