DDS  ver. 2.0
dds-session/src/Options.h
Go to the documentation of this file.
1 // Copyright 2018 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 #include <boost/property_tree/ptree.hpp>
12 
13 // silence "Unused typedef" warning using clang 3.7+ and boost < 1.59
14 #if BOOST_VERSION < 105900
15 #pragma clang diagnostic push
16 #pragma clang diagnostic ignored "-Wunused-local-typedef"
17 #endif
18 #include <boost/property_tree/ini_parser.hpp>
19 #if BOOST_VERSION < 105900
20 #pragma clang diagnostic pop
21 #endif
22 
23 #include <boost/filesystem/operations.hpp>
24 #include <boost/filesystem/path.hpp>
25 // DDS
26 #include "BOOSTHelper.h"
27 #include "MiscUtils.h"
28 #include "ProtocolCommands.h"
29 #include "Res.h"
30 #include "SubmitCmd.h"
31 #include "SysHelper.h"
32 #include "version.h"
33 
34 namespace bpo = boost::program_options;
35 
36 namespace dds
37 {
38  namespace submit_cmd
39  {
41  {
42  enum ETypes
43  {
47  };
48  SSessionsSorting(const std::string& _val)
49  : m_value(_val)
50  {
51  if (_val == "all")
53  else if (_val == "run")
55  else
57  }
58  std::string m_value;
60  };
61 
62  void validate(boost::any& _v, std::vector<std::string> const& _values, SSessionsSorting*, int)
63  {
64  // Make sure no previous assignment to 'v' was made.
65  bpo::validators::check_first_occurrence(_v);
66 
67  // Extract the first string from 'values'. If there is more than
68  // one string, it's an error, and exception will be thrown.
69  const std::string& s = bpo::validators::get_single_string(_values);
70 
71  if (s == "all" || s == "run")
72  {
73  _v = boost::any(SSessionsSorting(s));
74  }
75  else
76  {
77  throw bpo::validation_error(bpo::validation_error::invalid_option_value);
78  }
79  }
80 
82  typedef struct SOptions
83  {
85  : m_ListSessions("")
86  , m_bRemoveAllStopped(false)
87  , m_bForce(false)
88  {
89  }
92  bool m_bForce;
93  std::string m_sDefault;
94  } SOptions_t;
95  //=============================================================================
96  inline void PrintVersion()
97  {
98  LOG(MiscCommon::log_stdout) << " v" << PROJECT_VERSION_STRING << "\n"
99  << "DDS configuration"
100  << " v" << USER_DEFAULTS_CFG_VERSION << "\n"
102  }
103  //=============================================================================
104  // Command line parser
105  inline bool ParseCmdLine(int _argc, char* _argv[], SOptions* _options)
106  {
107  if (nullptr == _options)
108  throw std::runtime_error("Internal error: options' container is empty.");
109 
110  // Generic options
111  bpo::options_description options("dds-submit options");
112  options.add_options()("help,h", "Produce help message");
113  options.add_options()("version,v", "Version information");
114  options.add_options()("list,l",
115  bpo::value<SSessionsSorting>(&_options->m_ListSessions),
116  "List DDS sessions.\n\n"
117  "Values:\n"
118  " all: list all sessions\n"
119  " run: list only running sessions\n");
120  options.add_options()("set-default",
121  bpo::value<std::string>(&_options->m_sDefault),
122  "Set a giving session id as a default DDS session");
123  options.add_options()(
124  "remove,r", bpo::bool_switch(&_options->m_bRemoveAllStopped), "Remove all STOPPED DDS sessions");
125  options.add_options()("force,f",
126  bpo::bool_switch(&_options->m_bForce),
127  "Force commands without prompting for a confirmation.\n"
128  "For example, can be used with the \"remove\" command.");
129 
130  // Parsing command-line
131  bpo::variables_map vm;
132  bpo::store(bpo::command_line_parser(_argc, _argv).options(options).run(), vm);
133  bpo::notify(vm);
134 
135  // check for non-defaulted arguments
136  bpo::variables_map::const_iterator found =
137  find_if(vm.begin(), vm.end(), [](const bpo::variables_map::value_type& _v) {
138  return (!_v.second.defaulted());
139  });
140 
141  if (vm.count("help") || vm.end() == found)
142  {
143  LOG(MiscCommon::log_stdout) << options;
144  return false;
145  }
146  if (vm.count("version"))
147  {
148  PrintVersion();
149  return false;
150  }
151 
152  return true;
153  }
154  }
155 }
156 #endif
Definition: def.h:154
SOptions()
Definition: dds-session/src/Options.h:84
std::string m_sDefault
Definition: dds-session/src/Options.h:93
ETypes
Definition: dds-session/src/Options.h:42
dds-commander&#39;s container of options
Definition: dds-session/src/Options.h:82
Definition: dds-session/src/Options.h:45
#define LOG(severity)
Definition: Logger.h:54
void validate(boost::any &_v, std::vector< std::string > const &_values, SSessionsSorting *, int)
Definition: dds-session/src/Options.h:62
void PrintVersion()
Definition: dds-session/src/Options.h:96
const LPCSTR g_cszReportBugsAddr("Report bugs/comments to fairroot@gsi.de")
Definition: dds-agent/src/AgentConnectionManager.h:18
SSessionsSorting(const std::string &_val)
Definition: dds-session/src/Options.h:48
ETypes m_typedValue
Definition: dds-session/src/Options.h:59
SSessionsSorting m_ListSessions
Definition: dds-session/src/Options.h:90
bool m_bRemoveAllStopped
Definition: dds-session/src/Options.h:91
Definition: dds-session/src/Options.h:44
std::string m_value
Definition: dds-session/src/Options.h:58
Definition: dds-session/src/Options.h:46
struct dds::submit_cmd::SOptions SOptions_t
dds-commander&#39;s container of options
bool m_bForce
Definition: dds-session/src/Options.h:92
Definition: dds-session/src/Options.h:40
bool ParseCmdLine(int _argc, char *_argv[], SOptions *_options)
Definition: dds-session/src/Options.h:105