DDS  ver. 3.6
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 "Version.h"
14 // STD
15 #include <string>
16 //=============================================================================
17 namespace bpo = boost::program_options;
18 //=============================================================================
19 namespace dds
20 {
21  namespace custom_cmd
22  {
24  typedef struct SOptions
25  {
27  : m_sCmd()
28  , m_sCondition()
29  {
30  }
31 
32  std::string m_sCmd;
33  std::string m_sCondition;
34  } SOptions_t;
35 
36  // Command line parser
37  inline bool ParseCmdLine(int _argc, char* _argv[], SOptions* _options)
38  {
39  if (nullptr == _options)
40  throw std::runtime_error("Internal error: options' container is empty.");
41 
42  // Generic options
43  bpo::options_description options("dds-custom-cmd options");
44  options.add_options()("help,h", "Produce help message");
45  options.add_options()("version,v", "Version information");
46  options.add_options()(
47  "condition,t", bpo::value<std::string>(&_options->m_sCondition), "Condition to be applied to a task.");
48  options.add_options()("cmd,c", bpo::value<std::string>(&_options->m_sCmd), "Command to be sent to task.");
49 
50  // Parsing command-line
51  bpo::variables_map vm;
52  bpo::store(bpo::command_line_parser(_argc, _argv).options(options).run(), vm);
53  bpo::notify(vm);
54 
55  if (vm.count("help") || vm.empty())
56  {
57  LOG(dds::misc::log_stdout) << options;
58  return false;
59  }
60  if (vm.count("version"))
61  {
62  LOG(dds::misc::log_stdout) << dds::misc::DDSVersionInfoString();
63  return false;
64  }
65 
66  if (!vm.count("cmd"))
67  {
68  LOG(dds::misc::log_stdout) << "Option cmd must be provided.";
69  return false;
70  }
71 
72  return true;
73  }
74  } // namespace custom_cmd
75 } // namespace dds
76 #endif
std::string m_sCmd
Definition: dds-custom-cmd/src/Options.h:32
dds-commander's container of options
Definition: dds-custom-cmd/src/Options.h:24
SOptions()
Definition: dds-custom-cmd/src/Options.h:26
#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-custom-cmd/src/Options.h:37
struct dds::custom_cmd::SOptions SOptions_t
dds-commander's container of options
std::string m_sCondition
Definition: dds-custom-cmd/src/Options.h:33