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