PLCnext API Documentation 23.0.2.9
CommandLineOptions.hpp
1
2//
3// Copyright PHOENIX CONTACT Electronics GmbH
4//
6#pragma once
8#include "Arp/System/Core/Enum.hxx"
9#include "Arp/System/Commons/Exceptions/Exceptions.h"
10#include "boost/program_options.hpp"
11
12namespace Arp { namespace System { namespace Commons { namespace Configuration
13{
14
15using namespace boost::program_options;
16
18{
19public: // typedefs
20
21public: // construction/destruction
29 ~CommandLineOptions(void) = default;
30
31public: // operators
32
33public: // static operations
34
35public: // setter/getter operations
36
37public: // 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
60protected: // operations
61
62private: // static methods
63
64private: // methods
65
66private: // fields
67 options_description desc;
68 variables_map optionMap;
69
70public: // static fields
71 static const String helpOptionName;
72
73private: // 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
86inline void CommandLineOptions::AddOption(const String& name, const String& help)
87{
88 desc.add_options()
89 (name, help);
90}
91
92template<class T>
93inline void CommandLineOptions::AddOption(const String& name, const String& help)
94{
95 desc.add_options()
96 (name, value<T>(), help);
97}
98
99template<class T>
100inline 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
106template<class T>
107inline void CommandLineOptions::AddOptionEnum(const String& name, const String& help)
108{
109 desc.add_options()
110 (name, value<String>(), help);
111}
112
113template<class T>
114inline 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
120inline 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
126inline void CommandLineOptions::PrintDescription(std::ostream& os)
127{
128 os << desc << std::endl;
129}
130
131inline 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
141template<class T>
142inline 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
152template<class T>
153inline 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
164template<class T>
165Enum<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
180template<class T>
181bool 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(void)=default
Destructs this instance and frees all resources.
CommandLineOptions & operator=(const CommandLineOptions &arg)=delete
Assignment operator.
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