DDS  ver. 1.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 "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  {
64  }
65 
68  std::string m_sUpdKey_key;
69  std::string m_sUpdKey_value;
70  bool m_verbose;
71  } SOptions_t;
72  //=============================================================================
73  inline void PrintVersion()
74  {
75  LOG(MiscCommon::log_stdout) << " v" << PROJECT_VERSION_STRING << "\n"
76  << "DDS configuration"
77  << " v" << USER_DEFAULTS_CFG_VERSION << "\n"
79  }
80  //=============================================================================
81  // Command line parser
82  inline bool ParseCmdLine(int _argc, char* _argv[], SOptions* _options) throw(std::exception)
83  {
84  if (nullptr == _options)
85  throw std::runtime_error("Internal error: options' container is empty.");
86 
87  // Generic options
88  bpo::options_description options("dds-agent-cmd options");
89  options.add_options()("help,h", "Produce help message");
90  options.add_options()("version,v", "Version information");
91  options.add_options()("verbose", "Verbose output");
92  options.add_options()(
93  "command",
94  bpo::value<EAgentCmdType>(&_options->m_agentCmd),
95  "The command is a name of a dds-agent-cmd command."
96  " Can be one of the following: getlog, and update-key.\n"
97  "For user's convenience it is allowed to call dds-agent-cmd without \"--command\" option"
98  " by just specifying the command name directly, like:\ndds-agent-cmd getlog\n\n"
99  "Commands:\n"
100  " getlog: \tRetrieve log files from worker nodes. Files will be saved in ~/.DDS/log/agents\n"
101  " update-key: \tIt forces an update of a given task's property in the topology. Name of the property "
102  "and "
103  "its value should be provided additionally (see --key and --value) \n");
104  options.add_options()("all,a", "Send command to all active agents");
105  options.add_options()(
106  "key", bpo::value<std::string>(&_options->m_sUpdKey_key), "Specefies the key to update");
107  options.add_options()("value",
108  bpo::value<std::string>(&_options->m_sUpdKey_value),
109  "Specefies the new value ofthe given key");
110 
111  bpo::positional_options_description positional;
112  positional.add("command", -1);
113 
114  // Parsing command-line
115  bpo::variables_map vm;
116  bpo::store(bpo::command_line_parser(_argc, _argv).options(options).positional(positional).run(), vm);
117  bpo::notify(vm);
118 
119  MiscCommon::BOOSTHelper::option_dependency(vm, "key", "value");
120 
121  if (vm.count("help") || vm.empty())
122  {
123  LOG(MiscCommon::log_stdout) << options;
124  return false;
125  }
126  if (vm.count("version"))
127  {
128  PrintVersion();
129  return false;
130  }
131  if (vm.count("verbose"))
132  {
133  _options->m_verbose = true;
134  }
135  if (!vm.count("command") || _options->m_agentCmd == EAgentCmdType::UNKNOWN)
136  {
137  LOG(MiscCommon::log_stderr) << "Nothing to do. Please, specify a command"
138  << "\n\n"
139  << options;
140  return false;
141  }
142  if (EAgentCmdType::UPDATE_KEY == _options->m_agentCmd && !vm.count("key"))
143  {
144  LOG(MiscCommon::log_stderr) << "Please, specify the key to update"
145  << "\n\n"
146  << options;
147  return false;
148  }
149  if (vm.count("all"))
150  {
151  _options->m_sendCommandToAllAgents = true;
152  }
153 
154  return true;
155  }
156  }
157 }
158 #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:66
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:70
#define LOG(severity)
Definition: Logger.h:54
std::string m_sUpdKey_value
Definition: dds-agent-cmd/src/Options.h:69
const LPCSTR g_cszReportBugsAddr("Report bugs/comments to fairroot@gsi.de")
Definition: dds-agent/src/AgentConnectionManager.h:16
bool ParseCmdLine(int _argc, char *_argv[], SOptions *_options)
Definition: dds-agent-cmd/src/Options.h:82
void PrintVersion()
Definition: dds-agent-cmd/src/Options.h:73
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:67
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:68