PLCnext API Documentation 23.6.0.37
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#include <iostream>
12
13namespace Arp { namespace System { namespace Commons { namespace Configuration
14{
15
16using namespace boost::program_options;
17
19{
20public: // typedefs
21
22public: // construction/destruction
30 ~CommandLineOptions(void) = default;
31
32public: // operators
33
34public: // static operations
35
36public: // setter/getter operations
37
38public: // operations
39 void AddOption(const String& name, const String& help);
40 template<class T>
41 void AddOption(const String& name, const String& help);
42 template<class T>
43 void AddOption(const String& name, const T& defaultValue, const String& help);
44 template<class T>
45 void AddOptionEnum(const String& name, const String& help);
46 template<class T>
47 void AddOptionEnum(const String& name, const Enum<T>& defaultValue, const String& help);
48 void ParseArgs(int argc, const char* const argv[]);
49 void PrintDescription(std::ostream& os = std::cout);
50 bool ExistsOption(const String& optionName) const;
51
52 template<class T>
53 T GetOptionValue(const String& optionName) const;
54 template<class T>
55 bool TryGetOptionValue(const String& optionName, T& result) const;
56 template<class T>
57 Enum<T> GetOptionValueEnum(const char* optionName);
58 template<class T>
59 bool TryGetOptionValueEnum(const char* optionName, Enum<T>& value);
60
61protected: // operations
62
63private: // static methods
64
65private: // methods
66
67private: // fields
68 options_description desc;
69 variables_map optionMap;
70
71public: // static fields
72 static const String helpOptionName;
73
74private: // static fields
75 static const String helpOptionDescription;
76};
77
79// inline methods of class CommandLineOptions
81 : desc("Allowed options")
82{
83 desc.add_options()
84 (helpOptionName, helpOptionDescription);
85}
86
87inline void CommandLineOptions::AddOption(const String& name, const String& help)
88{
89 desc.add_options()
90 (name, help);
91}
92
93template<class T>
94inline void CommandLineOptions::AddOption(const String& name, const String& help)
95{
96 desc.add_options()
97 (name, value<T>(), help);
98}
99
100template<class T>
101inline void CommandLineOptions::AddOption(const String& name, const T& defaultValue, const String& help)
102{
103 desc.add_options()
104 (name, value<T>()->default_value(defaultValue), help);
105}
106
107template<class T>
108inline void CommandLineOptions::AddOptionEnum(const String& name, const String& help)
109{
110 desc.add_options()
111 (name, value<String>(), help);
112}
113
114template<class T>
115inline void CommandLineOptions::AddOptionEnum(const String& name, const Enum<T>& defaultValue, const String& help)
116{
117 desc.add_options()
118 (name, value<String>()->default_value(defaultValue.ToString()), help);
119}
120
121inline void CommandLineOptions::ParseArgs(int argc, const char* const argv[])
122{
123 store(parse_command_line(argc, argv, this->desc), optionMap);
124 notify(optionMap);
125}
126
127inline void CommandLineOptions::PrintDescription(std::ostream& os)
128{
129 os << desc << std::endl;
130}
131
132inline bool CommandLineOptions::ExistsOption(const String& optionName) const
133{
134 bool found = false;
135 if (optionMap.count(optionName))
136 {
137 found = true;
138 }
139 return found;
140}
141
142template<class T>
143inline T CommandLineOptions::GetOptionValue(const String& optionName) const
144{
145 T result;
146 if(!this->TryGetOptionValue(optionName, result))
147 {
148 throw KeyNotFoundException::Create(optionName);
149 }
150 return result;
151}
152
153template<class T>
154inline bool CommandLineOptions::TryGetOptionValue(const String& optionName, T& result) const
155{
156 bool found = false;
157 if(optionMap.count(optionName) > 0)
158 {
159 result = optionMap[optionName].as<T>();
160 found = true;
161 }
162 return found;
163}
164
165template<class T>
166Enum<T> CommandLineOptions::GetOptionValueEnum(const char* optionName)
167{
168 Enum<T> returnValue;
169
170 if (TryGetOptionValueEnum(optionName, returnValue))
171 {
172 return returnValue;
173 }
174 else
175 {
176 throw Exception("GetOptionValueEnum failed for mandatory option: {0}", optionName);
177 }
178
179}
180
181template<class T>
182bool CommandLineOptions::TryGetOptionValueEnum(const char* optionName, Enum<T>& value)
183{
184 String returnValue;
185 if (!this->TryGetOptionValue<String>(optionName, returnValue))
186 {
187 return false;
188 }
189 // else
190 value = Enum<T>::Parse(returnValue);
191 return true;
192}
193
194}}}} // 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:19
~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:80
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
Root namespace for the PLCnext API