PLCnext API Documentation  22.9.0.33
CommandLineOptions.hpp
1 //
3 // Copyright PHOENIX CONTACT Electronics GmbH
4 //
6 #pragma once
7 #include "Arp/System/Core/Arp.h"
8 #include "Arp/System/Core/Enum.hxx"
9 #include "Arp/System/Commons/Exceptions/Exceptions.h"
10 #include "boost/program_options.hpp"
11 
12 namespace Arp { namespace System { namespace Commons { namespace Configuration
13 {
14 
15 using namespace boost::program_options;
16 
18 {
19 public: // typedefs
20 
21 public: // construction/destruction
23  CommandLineOptions(void);
25  CommandLineOptions(const CommandLineOptions& arg) = delete;
29  ~CommandLineOptions(void) = default;
30 
31 public: // operators
32 
33 public: // static operations
34 
35 public: // setter/getter operations
36 
37 public: // operations
38  void AddOption(const String& name, const String& help);
39  template<class T>
40  void AddOption(const String& name, const String& help);
41  template<class T>
42  void AddOption(const String& name, const T& defaultValue, const String& help);
43  template<class T>
44  void AddOptionEnum(const String& name, const String& help);
45  template<class T>
46  void AddOptionEnum(const String& name, const Enum<T>& defaultValue, const String& help);
47  void ParseArgs(int argc, const char* const argv[]);
48  void PrintDescription(std::ostream& os = std::cout);
49  bool ExistsOption(const String& optionName) const;
50 
51  template<class T>
52  T GetOptionValue(const String& optionName) const;
53  template<class T>
54  bool TryGetOptionValue(const String& optionName, T& result) const;
55  template<class T>
56  Enum<T> GetOptionValueEnum(const char* optionName);
57  template<class T>
58  bool TryGetOptionValueEnum(const char* optionName, Enum<T>& value);
59 
60 protected: // operations
61 
62 private: // static methods
63 
64 private: // methods
65 
66 private: // fields
67  options_description desc;
68  variables_map optionMap;
69 
70 public: // static fields
71  static const String helpOptionName;
72 
73 private: // static fields
74  static const String helpOptionDescription;
75 };
76 
78 // inline methods of class CommandLineOptions
80  : desc("Allowed options")
81 {
82  desc.add_options()
83  (helpOptionName, helpOptionDescription);
84 }
85 
86 inline void CommandLineOptions::AddOption(const String& name, const String& help)
87 {
88  desc.add_options()
89  (name, help);
90 }
91 
92 template<class T>
93 inline void CommandLineOptions::AddOption(const String& name, const String& help)
94 {
95  desc.add_options()
96  (name, value<T>(), help);
97 }
98 
99 template<class T>
100 inline void CommandLineOptions::AddOption(const String& name, const T& defaultValue, const String& help)
101 {
102  desc.add_options()
103  (name, value<T>()->default_value(defaultValue), help);
104 }
105 
106 template<class T>
107 inline void CommandLineOptions::AddOptionEnum(const String& name, const String& help)
108 {
109  desc.add_options()
110  (name, value<String>(), help);
111 }
112 
113 template<class T>
114 inline void CommandLineOptions::AddOptionEnum(const String& name, const Enum<T>& defaultValue, const String& help)
115 {
116  desc.add_options()
117  (name, value<String>()->default_value(defaultValue.ToString()), help);
118 }
119 
120 inline void CommandLineOptions::ParseArgs(int argc, const char* const argv[])
121 {
122  store(parse_command_line(argc, argv, this->desc), optionMap);
123  notify(optionMap);
124 }
125 
126 inline void CommandLineOptions::PrintDescription(std::ostream& os)
127 {
128  os << desc << std::endl;
129 }
130 
131 inline bool CommandLineOptions::ExistsOption(const String& optionName) const
132 {
133  bool found = false;
134  if (optionMap.count(optionName))
135  {
136  found = true;
137  }
138  return found;
139 }
140 
141 template<class T>
142 inline T CommandLineOptions::GetOptionValue(const String& optionName) const
143 {
144  T result;
145  if(!this->TryGetOptionValue(optionName, result))
146  {
147  throw KeyNotFoundException::Create(optionName);
148  }
149  return result;
150 }
151 
152 template<class T>
153 inline bool CommandLineOptions::TryGetOptionValue(const String& optionName, T& result) const
154 {
155  bool found = false;
156  if(optionMap.count(optionName) > 0)
157  {
158  result = optionMap[optionName].as<T>();
159  found = true;
160  }
161  return found;
162 }
163 
164 template<class T>
165 Enum<T> CommandLineOptions::GetOptionValueEnum(const char* optionName)
166 {
167  Enum<T> returnValue;
168 
169  if (TryGetOptionValueEnum(optionName, returnValue))
170  {
171  return returnValue;
172  }
173  else
174  {
175  throw Exception("GetOptionValueEnum failed for mandatory option: {0}", optionName);
176  }
177 
178 }
179 
180 template<class T>
181 bool CommandLineOptions::TryGetOptionValueEnum(const char* optionName, Enum<T>& value)
182 {
183  String returnValue;
184  if (!this->TryGetOptionValue<String>(optionName, returnValue))
185  {
186  return false;
187  }
188  // else
189  value = Enum<T>::Parse(returnValue);
190  return true;
191 }
192 
193 }}}} // end of namespace Arp::System::Commons
Adapter class for enums to make them loggable and parsable from e.g. XML files.
Definition: Enum.hxx:23
Definition: CommandLineOptions.hpp:18
CommandLineOptions & operator=(const CommandLineOptions &arg)=delete
Assignment operator.
~CommandLineOptions(void)=default
Destructs this instance and frees all resources.
CommandLineOptions(const CommandLineOptions &arg)=delete
Copy constructor.
CommandLineOptions(void)
Constructs an CommandLineOptions instance.
Definition: CommandLineOptions.hpp:79
static KeyNotFoundException Create(const T &keyValue)
Creates an KeyNotFoundException instance using a default message text.
Definition: KeyNotFoundException.hpp:79
static Enum Parse(const String &input)
Parses the given input string.
Definition: Enum.hxx:228
Arp::BasicString< char8 > String
The Arp String class.
Definition: TypeSystem.h:27
@ System
System components used by the System, Device, Plc or Io domains.
Root namespace for the PLCnext API