DDS  ver. 3.4
dds-custom-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 "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 custom_cmd
23  {
25  typedef struct SOptions
26  {
28  : m_sCmd()
29  , m_sCondition()
30  {
31  }
32 
33  std::string m_sCmd;
34  std::string m_sCondition;
35  } SOptions_t;
36  //=============================================================================
37  inline void PrintVersion()
38  {
39  LOG(MiscCommon::log_stdout) << " v" << PROJECT_VERSION_STRING << "\n"
40  << "DDS configuration"
41  << " v" << USER_DEFAULTS_CFG_VERSION << "\n"
43  }
44  //=============================================================================
45  // Command line parser
46  inline bool ParseCmdLine(int _argc, char* _argv[], SOptions* _options)
47  {
48  if (nullptr == _options)
49  throw std::runtime_error("Internal error: options' container is empty.");
50 
51  // Generic options
52  bpo::options_description options("dds-custom-cmd options");
53  options.add_options()("help,h", "Produce help message");
54  options.add_options()("version,v", "Version information");
55  options.add_options()(
56  "condition,t", bpo::value<std::string>(&_options->m_sCondition), "Condition to be applied to a task.");
57  options.add_options()("cmd,c", bpo::value<std::string>(&_options->m_sCmd), "Command to be sent to task.");
58 
59  // Parsing command-line
60  bpo::variables_map vm;
61  bpo::store(bpo::command_line_parser(_argc, _argv).options(options).run(), vm);
62  bpo::notify(vm);
63 
64  if (vm.count("help") || vm.empty())
65  {
66  LOG(MiscCommon::log_stdout) << options;
67  return false;
68  }
69  if (vm.count("version"))
70  {
71  PrintVersion();
72  return false;
73  }
74 
75  if (!vm.count("cmd"))
76  {
77  LOG(MiscCommon::log_stdout) << "Option cmd must be provided.";
78  return false;
79  }
80 
81  return true;
82  }
83  } // namespace custom_cmd
84 } // namespace dds
85 #endif
Definition: def.h:154
std::string m_sCmd
Definition: dds-custom-cmd/src/Options.h:33
dds-commander's container of options
Definition: dds-custom-cmd/src/Options.h:25
SOptions()
Definition: dds-custom-cmd/src/Options.h:27
#define LOG(severity)
Definition: Logger.h:56
void PrintVersion()
Definition: dds-custom-cmd/src/Options.h:37
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-custom-cmd/src/Options.h:46
struct dds::custom_cmd::SOptions SOptions_t
dds-commander's container of options
std::string m_sCondition
Definition: dds-custom-cmd/src/Options.h:34