Options: added operators

- operator[char/string/int] for option access
- option bool() overload
- option string() argument overload
This commit is contained in:
zawz 2019-10-16 19:20:27 +02:00
parent ec2c2c8c37
commit b23028347f
2 changed files with 28 additions and 9 deletions

View file

@ -59,9 +59,9 @@ namespace ztd
//! @brief Create a char defined option
option(char c, bool arg=false, std::string helptext="", std::string argname="arg");
//! @brief Create a string defined option
option(std::string const& str, bool arg=false, std::string helptext="", std::string argname="arg");
option(const std::string& str, bool arg=false, std::string helptext="", std::string argname="arg");
//! @brief Create a char and string defined option
option(char c, std::string const& str, bool arg=false, std::string helptext="", std::string argname="arg");
option(char c, const std::string& str, bool arg=false, std::string helptext="", std::string argname="arg");
//! @brief Create copy
option(const option& opt);
@ -95,6 +95,9 @@ namespace ztd
//! @brief Option's argument
std::string argument; // argument of the option
inline operator bool() const { return activated; }
inline operator std::string() const { return argument; }
};
//! @brief Set of POSIX/GNU style options
@ -124,9 +127,13 @@ namespace ztd
/*QUERY FUNCTIONS*/
//! @brief Find char option
option* find(char c);
/*! @return nullptr if no option found
*/
option* find(const char c);
//! @brief Find string option
option* find(std::string const& str);
/*! @return nullptr if no option found
*/
option* find(const std::string& str);
inline option* find(const char* str) { return this->find(std::string(str)); }
/*PROCESSING FUNCTIONS*/
@ -143,9 +150,21 @@ namespace ztd
*/
inline std::vector<std::string> process(int argc, char** argv, bool ignore_numbers=false) { return this->process(ztd::argVector(argc, argv), ignore_numbers); }
//! @brief Get option with char name
/*! @see option* find(char c)
*/
option& operator[](const char c) { return *this->find(c); }
//! @brief Get option with char name
/*! @see option* find(const std::string& str);
*/
option& operator[](const std::string& str) { return *this->find(str); }
//! @brief Get nth option
/*! Reads nth option from @a option_sequence
*/
option& operator[](unsigned int n) { return option_sequence[n]; }
//! @brief Options in the set
std::vector<option> option_vec;
//! @brief Ordered result of option processing
std::vector<option> option_sequence;
};

View file

@ -50,7 +50,7 @@ ztd::option::option(char c, bool arg, std::string helptext, std::string argname)
activated=false;
}
ztd::option::option(std::string const& str, bool arg, std::string helptext, std::string argname)
ztd::option::option(const std::string& str, bool arg, std::string helptext, std::string argname)
{
//char
shortDef=false;
@ -67,7 +67,7 @@ ztd::option::option(std::string const& str, bool arg, std::string helptext, std:
activated=false;
}
ztd::option::option(char c, std::string const& str, bool arg, std::string helptext, std::string argname)
ztd::option::option(char c, const std::string& str, bool arg, std::string helptext, std::string argname)
{
//char
shortDef=true;
@ -139,7 +139,7 @@ void ztd::option_set::print_help(int leftpad, int rightpad) const
it.print_help(leftpad,rightpad);
}
ztd::option* ztd::option_set::find(char c)
ztd::option* ztd::option_set::find(const char c)
{
for( auto it=option_vec.begin() ; it!=option_vec.end() ; it++ )
{
@ -149,7 +149,7 @@ ztd::option* ztd::option_set::find(char c)
return nullptr;
}
ztd::option* ztd::option_set::find(std::string const& str)
ztd::option* ztd::option_set::find(const std::string& str)
{
for( auto it=option_vec.begin() ; it!=option_vec.end() ; it++ )
{