102 int find(
const std::string& key,
size_t startPosition = 0,
bool findAlsoHandled =
true);
112 bool read(
const std::string& key,
size_t startPosition = 0,
bool findAlsoHandled =
true );
124 template <
typename T>
125 bool read1(
const std::string& key, T& value,
size_t startPosition = 0,
bool findAlsoHandled =
true );
139 template <
typename T1,
typename T2>
140 bool read2(
const std::string& key, T1& value1, T2& value2,
size_t startPosition = 0,
bool findAlsoHandled =
true );
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 );
175 template <
typename T>
176 T defaultRead(
const std::string& key,
const T& defaultValue,
size_t startPosition = 0,
bool findAlsoHandled =
true);
203 static bool assignStringContent (
const std::string& key, T& type);
205 static bool assignStringContent (
const std::string& key, std::string& type);
220 template <
typename T>
222 bool findAlsoHandled )
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))
228 m_arguments[pos].setHandled(
true );
229 m_arguments[pos + 1].setHandled(
true );
236 template <
typename T1,
typename T2>
238 bool findAlsoHandled )
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))
245 m_arguments[pos].setHandled(
true );
246 m_arguments[pos + 1].setHandled(
true );
247 m_arguments[pos + 2].setHandled(
true );
254 template <
typename T1,
typename T2,
typename T3>
256 bool findAlsoHandled )
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))
264 m_arguments[pos].setHandled(
true );
265 m_arguments[pos + 1].setHandled(
true );
266 m_arguments[pos + 2].setHandled(
true );
267 m_arguments[pos + 3].setHandled(
true );
274 template <
typename T>
276 bool findAlsoHandled )
279 if (this->
read1( key, ret, startPosition, findAlsoHandled ))
285 template <
typename T>
286 bool ArgumentParser::assignStringContent (
const std::string& key, T& type)
288 std::stringstream ss( key );
290 return (ss.good() || ss.rdstate() == std::ios_base::eofbit);
294 bool AGXCORE_EXPORT ArgumentParser::assignStringContent<std::string>(
const std::string& key, std::string& type);
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.
virtual ~ArgumentParser()
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.
Base class providing referencing counted objects.
Smart pointer for handling referenced counted objects.
The agxIO namespace contains classes for reading, writing and finding files.
agx::ref_ptr< ArgumentParser > ArgumentParserRef