PLCnext API Documentation 25.0.2.69
CommandLineOptions.hpp
1
2//
3// Copyright Phoenix Contact GmbH & Co. KG
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 <iosfwd>
12
13namespace Arp { namespace System { namespace Commons { namespace Configuration
14{
15
16using namespace boost::program_options;
17
19{
20public: // construction/destruction
21
28 ~CommandLineOptions(void) = default;
29
30public: // operations
31 void AddOption(const String& name, const String& help);
32 template<class T>
33 void AddOption(const String& name, const String& help);
34 template<class T>
35 void AddOption(const String& name, const T& defaultValue, const String& help);
36 template<class T>
37 void AddOptionEnum(const String& name, const String& help);
38 template<class T>
39 void AddOptionEnum(const String& name, const Enum<T>& defaultValue, const String& help);
40 void ParseArgs(int argc, const char* const argv[]);
41 void PrintDescription();
42 void PrintDescription(std::ostream& os);
43 bool ExistsOption(const String& optionName) const;
44
45 template<class T>
46 T GetOptionValue(const String& optionName) const;
47 template<class T>
48 bool TryGetOptionValue(const String& optionName, T& result) const;
49 template<class T>
50 Enum<T> GetOptionValueEnum(const char* optionName);
51 template<class T>
52 bool TryGetOptionValueEnum(const char* optionName, Enum<T>& value);
53
54private: // fields
55 options_description desc;
56 variables_map optionMap;
57
58public: // static fields
59 static const String helpOptionName;
60
61private: // static fields
62 static const String helpOptionDescription;
63};
64
66// inline methods of class CommandLineOptions
67
68template<class T>
69inline void CommandLineOptions::AddOption(const String& name, const String& help)
70{
71 desc.add_options()
72 (name, value<T>(), help);
73}
74
75template<class T>
76inline void CommandLineOptions::AddOption(const String& name, const T& defaultValue, const String& help)
77{
78 desc.add_options()
79 (name, value<T>()->default_value(defaultValue), help);
80}
81
82template<class T>
83inline void CommandLineOptions::AddOptionEnum(const String& name, const String& help)
84{
85 desc.add_options()
86 (name, value<String>(), help);
87}
88
89template<class T>
90inline void CommandLineOptions::AddOptionEnum(const String& name, const Enum<T>& defaultValue, const String& help)
91{
92 desc.add_options()
93 (name, value<String>()->default_value(defaultValue.ToString()), help);
94}
95
96
97
98template<class T>
99inline T CommandLineOptions::GetOptionValue(const String& optionName) const
100{
101 T result;
102 if(!this->TryGetOptionValue(optionName, result))
103 {
104 throw KeyNotFoundException::Create(optionName);
105 }
106 return result;
107}
108
109template<class T>
110inline bool CommandLineOptions::TryGetOptionValue(const String& optionName, T& result) const
111{
112 bool found = false;
113 if(optionMap.count(optionName) > 0)
114 {
115 result = optionMap[optionName].as<T>();
116 found = true;
117 }
118 return found;
119}
120
121template<class T>
122Enum<T> CommandLineOptions::GetOptionValueEnum(const char* optionName)
123{
124 Enum<T> returnValue;
125
126 if (TryGetOptionValueEnum(optionName, returnValue))
127 {
128 return returnValue;
129 }
130 else
131 {
132 throw Exception("GetOptionValueEnum failed for mandatory option: {0}", optionName);
133 }
134
135}
136
137template<class T>
138bool CommandLineOptions::TryGetOptionValueEnum(const char* optionName, Enum<T>& value)
139{
140 String returnValue;
141 if (!this->TryGetOptionValue<String>(optionName, returnValue))
142 {
143 return false;
144 }
145 // else
146 value = Enum<T>::Parse(returnValue);
147 return true;
148}
149
150}}}} // end of namespace Arp::System::Commons
Adapter class for enums to make them loggable and parsable from e.g. XML files.
Definition: Enum.hxx:21
This class represents the Arp String. The implementation is based on std::string.
Definition: String.hpp:39
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.cpp:16
static KeyNotFoundException Create(const T &keyValue)
Creates an KeyNotFoundException instance using a default message text.
Definition: KeyNotFoundException.hpp:56
String ToString(bool throwIfInvalid=true) const
Converts this instance to its string representation.
Definition: Enum.ipp:199
static Enum Parse(const String &input)
Parses the given input string.
Definition: Enum.ipp:90
Root namespace for the PLCnext API