AGX Dynamics 2.38.0.2
Loading...
Searching...
No Matches
ArgumentParser.h
Go to the documentation of this file.
1/*
2Copyright 2007-2024. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx Simulation AB.
15*/
16
17#ifndef AGXIO_ARGUMENT_PARSER_H
18#define AGXIO_ARGUMENT_PARSER_H
19
20#include <string>
21#include <sstream>
22#include <agx/HashVector.h>
23#include <agx/Referenced.h>
24#include <agx/String.h>
25#include <iostream>
26#include <agx/agxCore_export.h>
27
28namespace agxIO
29{
30
39 {
40
41 public:
47 {
48 public:
51
53 Argument (const std::string& argumentName);
54
56 std::string getName() const;
57
59 bool getHandled() const;
60
62 void setHandled( bool handled);
63
65 bool isOption() const;
66
67
68 private:
69 agx::String m_argumentName;
70 bool m_handled;
71 };
72
74
75 public:
76
79
80#ifndef SWIG
87 ArgumentParser( int argc, char** argv );
88#endif
89
94
103 int find( const std::string& key, size_t startPosition = 0, bool findAlsoHandled = true);
104
113 bool read( const std::string& key, size_t startPosition = 0, bool findAlsoHandled = true );
114
125 template <typename T>
126 bool read1( const std::string& key, T& value, size_t startPosition = 0, bool findAlsoHandled = true );
127
140 template <typename T1, typename T2>
141 bool read2( const std::string& key, T1& value1, T2& value2, size_t startPosition = 0, bool findAlsoHandled = true );
142
157 template <typename T1, typename T2, typename T3>
158 bool read3( const std::string& key, T1& value1, T2& value2, T3& value3, size_t startPosition = 0, bool findAlsoHandled = true );
159
164 void addArgument( const char* argumentName);
165
176 template <typename T>
177 T defaultRead(const std::string& key, const T& defaultValue, size_t startPosition = 0, bool findAlsoHandled = true);
178
184 bool reportUnreadArguments( std::ostream& out = std::cout );
185
187 size_t getNumArguments() const;
188
190 Argument* getArgument( size_t index );
191
196 char** getArgv();
197
198 protected:
200
201 private:
202 // Relies on std streams to cast values in strings to templated type.
203 template<typename T>
204 static bool assignStringContent (const std::string& key, T& type);
205
206 static bool assignStringContent (const std::string& key, std::string& type);
207
208
211 ArgumentParser& operator=(const ArgumentParser&);
212
213 private:
214 ArgumentVector m_arguments;
215 char** m_argv;
216
217 };
218
219
220// IMPLEMENTATIONS //
221 template <typename T>
222 bool ArgumentParser::read1(const std::string& key, T& value, size_t startPosition /*= 0*/,
223 bool findAlsoHandled /*= true*/)
224 {
225 int pos = this->find(key, startPosition, findAlsoHandled);
226 if (pos >= 0 && pos + 1 < (int)m_arguments.size()) {
227 if (!assignStringContent (m_arguments[pos + 1].getName(), value))
228 return false;
229 m_arguments[pos].setHandled( true ); // key handled
230 m_arguments[pos + 1].setHandled( true ); // value handled
231 return true;
232 }
233 return false;
234 }
235
236
237 template <typename T1, typename T2>
238 bool ArgumentParser::read2( const std::string& key, T1& value1, T2& value2, size_t startPosition /*= 0*/,
239 bool findAlsoHandled /*= true*/)
240 {
241 int pos = find(key, startPosition, findAlsoHandled);
242 if (pos >= 0 && size_t(pos + 2) < m_arguments.size()) {
243 if (!assignStringContent (m_arguments[pos + 1].getName(), value1) ||
244 !assignStringContent (m_arguments[pos + 2].getName(), value2))
245 return false;
246 m_arguments[pos].setHandled( true ); // key handled
247 m_arguments[pos + 1].setHandled( true ); // value1 handled
248 m_arguments[pos + 2].setHandled( true ); // value2 handled
249 return true;
250 }
251 return false;
252 }
253
254
255 template <typename T1, typename T2, typename T3>
256 bool ArgumentParser::read3( const std::string& key, T1& value1, T2& value2, T3& value3, size_t startPosition /*= 0*/,
257 bool findAlsoHandled /*= true*/)
258 {
259 int pos = this->find(key, startPosition, findAlsoHandled);
260 if (pos >= 0 && pos + 3 < (int)m_arguments.size()) {
261 if (!assignStringContent (m_arguments[pos + 1].getName(), value1) ||
262 !assignStringContent (m_arguments[pos + 2].getName(), value2) ||
263 !assignStringContent (m_arguments[pos + 3].getName(), value3))
264 return false;
265 m_arguments[pos].setHandled( true ); // key handled
266 m_arguments[pos + 1].setHandled( true ); // value1 handled
267 m_arguments[pos + 2].setHandled( true ); // value2 handled
268 m_arguments[pos + 3].setHandled( true ); // value3 handled
269 return true;
270 }
271 return false;
272 }
273
274
275 template <typename T>
276 T ArgumentParser::defaultRead(const std::string& key, const T& defaultValue, size_t startPosition /*= 0*/,
277 bool findAlsoHandled /*= true*/)
278 {
279 T ret;
280 if (this->read1( key, ret, startPosition, findAlsoHandled ))
281 return ret;
282 return defaultValue;
283 }
284
285
286 template <typename T>
287 bool ArgumentParser::assignStringContent (const std::string& key, T& type)
288 {
289 std::stringstream ss( key );
290 ss >> type;
291 return (ss.good() || ss.rdstate() == std::ios_base::eofbit);
292 }
293
294 template <>
295 bool AGXCORE_EXPORT ArgumentParser::assignStringContent<std::string>(const std::string& key, std::string& type);
296
297 template <>
298 bool AGXCORE_EXPORT ArgumentParser::assignStringContent<agx::String>(const std::string& key, agx::String& type);
299
301}
302
303#endif
304
#define AGXCORE_EXPORT
Help class for handling arguments.
bool isOption() const
Is the argument an option (starting with "--")?
std::string getName() const
Gets the name of the argument.
Argument()
Constructs an empty argument.
Argument(const std::string &argumentName)
Constructs and argument given its name.
bool getHandled() const
Has the argument been handled?
void setHandled(bool handled)
Set if the argument has been handled.
A small argument parser class.
size_t getNumArguments() const
Returns the number of arguments.
agx::Vector< Argument > ArgumentVector
bool read3(const std::string &key, T1 &value1, T2 &value2, T3 &value3, size_t startPosition=0, bool findAlsoHandled=true)
Reads three argument into variables.
bool read2(const std::string &key, T1 &value1, T2 &value2, size_t startPosition=0, bool findAlsoHandled=true)
Reads two argument into variables.
T defaultRead(const std::string &key, const T &defaultValue, size_t startPosition=0, bool findAlsoHandled=true)
Reads an argument into a variable.
ArgumentParser(const agx::StringVector &arguments)
Constructs an argument parser from the program command line input given as an array of strings.
Argument * getArgument(size_t index)
Gets an argument at a position in the argument container.
bool read1(const std::string &key, T &value, size_t startPosition=0, bool findAlsoHandled=true)
Looks for an argument, and reads it into a variable if found.
void addArgument(const char *argumentName)
Adds an argument manually.
bool read(const std::string &key, size_t startPosition=0, bool findAlsoHandled=true)
Looks for an argument, and returns true if it finds it.
ArgumentParser(int argc, char **argv)
Constructs an argument parser from the program command line input.
int find(const std::string &key, size_t startPosition=0, bool findAlsoHandled=true)
Finds the position of an argument.
bool reportUnreadArguments(std::ostream &out=std::cout)
Writes all arguments which have not been handled to a stream.
ArgumentParser()
Constructs an argument parser from the program command line input.
char ** getArgv()
Gets the original array of c-strings.
size_t size() const
Definition Container.h:134
Base class providing referencing counted objects.
Definition Referenced.h:120
Templated vector class.
Definition agx/Vector.h:53
Smart pointer for handling referenced counted objects.
Definition ref_ptr.h:30
The agxIO namespace contains classes for reading, writing and finding files.
Definition Material.h:42
agx::ref_ptr< ArgumentParser > ArgumentParserRef