AGX Dynamics 2.42.3.1
Loading...
Searching...
No Matches
ArgumentParser.h
Go to the documentation of this file.
1/*
2Copyright 2007-2025. 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#pragma once
18
19#include <string>
20#include <sstream>
21#include <agx/HashVector.h>
22#include <agx/Referenced.h>
23#include <agx/String.h>
24#include <iostream>
25#include <agx/agxCore_export.h>
26
27namespace agxIO
28{
29
38 {
39
40 public:
46 {
47 public:
50
52 Argument (const std::string& argumentName);
53
55 std::string getName() const;
56
58 bool getHandled() const;
59
61 void setHandled( bool handled);
62
64 bool isOption() const;
65
66
67 private:
68 agx::String m_argumentName;
69 bool m_handled;
70 };
71
73
74 public:
75
78
79#ifndef SWIG
86 ArgumentParser( int argc, char** argv );
87#endif
88
93
102 int find( const std::string& key, size_t startPosition = 0, bool findAlsoHandled = true);
103
112 bool read( const std::string& key, size_t startPosition = 0, bool findAlsoHandled = true );
113
124 template <typename T>
125 bool read1( const std::string& key, T& value, size_t startPosition = 0, bool findAlsoHandled = true );
126
139 template <typename T1, typename T2>
140 bool read2( const std::string& key, T1& value1, T2& value2, size_t startPosition = 0, bool findAlsoHandled = true );
141
156 template <typename T1, typename T2, typename T3>
157 bool read3( const std::string& key, T1& value1, T2& value2, T3& value3, size_t startPosition = 0, bool findAlsoHandled = true );
158
163 void addArgument( const char* argumentName);
164
175 template <typename T>
176 T defaultRead(const std::string& key, const T& defaultValue, size_t startPosition = 0, bool findAlsoHandled = true);
177
183 bool reportUnreadArguments( std::ostream& out = std::cout );
184
186 size_t getNumArguments() const;
187
189 Argument* getArgument( size_t index );
190
195 char** getArgv();
196
197 protected:
199
200 private:
201 // Relies on std streams to cast values in strings to templated type.
202 template<typename T>
203 static bool assignStringContent (const std::string& key, T& type);
204
205 static bool assignStringContent (const std::string& key, std::string& type);
206
207
210 ArgumentParser& operator=(const ArgumentParser&);
211
212 private:
213 ArgumentVector m_arguments;
214 char** m_argv;
215
216 };
217
218
219// IMPLEMENTATIONS //
220 template <typename T>
221 bool ArgumentParser::read1(const std::string& key, T& value, size_t startPosition /*= 0*/,
222 bool findAlsoHandled /*= true*/)
223 {
224 int pos = this->find(key, startPosition, findAlsoHandled);
225 if (pos >= 0 && pos + 1 < (int)m_arguments.size()) {
226 if (!assignStringContent (m_arguments[pos + 1].getName(), value))
227 return false;
228 m_arguments[pos].setHandled( true ); // key handled
229 m_arguments[pos + 1].setHandled( true ); // value handled
230 return true;
231 }
232 return false;
233 }
234
235
236 template <typename T1, typename T2>
237 bool ArgumentParser::read2( const std::string& key, T1& value1, T2& value2, size_t startPosition /*= 0*/,
238 bool findAlsoHandled /*= true*/)
239 {
240 int pos = find(key, startPosition, findAlsoHandled);
241 if (pos >= 0 && size_t(pos + 2) < m_arguments.size()) {
242 if (!assignStringContent (m_arguments[pos + 1].getName(), value1) ||
243 !assignStringContent (m_arguments[pos + 2].getName(), value2))
244 return false;
245 m_arguments[pos].setHandled( true ); // key handled
246 m_arguments[pos + 1].setHandled( true ); // value1 handled
247 m_arguments[pos + 2].setHandled( true ); // value2 handled
248 return true;
249 }
250 return false;
251 }
252
253
254 template <typename T1, typename T2, typename T3>
255 bool ArgumentParser::read3( const std::string& key, T1& value1, T2& value2, T3& value3, size_t startPosition /*= 0*/,
256 bool findAlsoHandled /*= true*/)
257 {
258 int pos = this->find(key, startPosition, findAlsoHandled);
259 if (pos >= 0 && pos + 3 < (int)m_arguments.size()) {
260 if (!assignStringContent (m_arguments[pos + 1].getName(), value1) ||
261 !assignStringContent (m_arguments[pos + 2].getName(), value2) ||
262 !assignStringContent (m_arguments[pos + 3].getName(), value3))
263 return false;
264 m_arguments[pos].setHandled( true ); // key handled
265 m_arguments[pos + 1].setHandled( true ); // value1 handled
266 m_arguments[pos + 2].setHandled( true ); // value2 handled
267 m_arguments[pos + 3].setHandled( true ); // value3 handled
268 return true;
269 }
270 return false;
271 }
272
273
274 template <typename T>
275 T ArgumentParser::defaultRead(const std::string& key, const T& defaultValue, size_t startPosition /*= 0*/,
276 bool findAlsoHandled /*= true*/)
277 {
278 T ret;
279 if (this->read1( key, ret, startPosition, findAlsoHandled ))
280 return ret;
281 return defaultValue;
282 }
283
284
285 template <typename T>
286 bool ArgumentParser::assignStringContent (const std::string& key, T& type)
287 {
288 std::stringstream ss( key );
289 ss >> type;
290 return (ss.good() || ss.rdstate() == std::ios_base::eofbit);
291 }
292
293 template <>
294 bool AGXCORE_EXPORT ArgumentParser::assignStringContent<std::string>(const std::string& key, std::string& type);
295
296 template <>
297 bool AGXCORE_EXPORT ArgumentParser::assignStringContent<agx::String>(const std::string& key, agx::String& type);
298
300}
#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