Options: added operators
- operator[char/string/int] for option access - option bool() overload - option string() argument overload
This commit is contained in:
parent
ec2c2c8c37
commit
b23028347f
2 changed files with 28 additions and 9 deletions
|
|
@ -59,9 +59,9 @@ namespace ztd
|
||||||
//! @brief Create a char defined option
|
//! @brief Create a char defined option
|
||||||
option(char c, bool arg=false, std::string helptext="", std::string argname="arg");
|
option(char c, bool arg=false, std::string helptext="", std::string argname="arg");
|
||||||
//! @brief Create a string defined option
|
//! @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
|
//! @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
|
//! @brief Create copy
|
||||||
option(const option& opt);
|
option(const option& opt);
|
||||||
|
|
||||||
|
|
@ -95,6 +95,9 @@ namespace ztd
|
||||||
//! @brief Option's argument
|
//! @brief Option's argument
|
||||||
std::string argument; // argument of the option
|
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
|
//! @brief Set of POSIX/GNU style options
|
||||||
|
|
@ -124,9 +127,13 @@ namespace ztd
|
||||||
|
|
||||||
/*QUERY FUNCTIONS*/
|
/*QUERY FUNCTIONS*/
|
||||||
//! @brief Find char option
|
//! @brief Find char option
|
||||||
option* find(char c);
|
/*! @return nullptr if no option found
|
||||||
|
*/
|
||||||
|
option* find(const char c);
|
||||||
//! @brief Find string option
|
//! @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)); }
|
inline option* find(const char* str) { return this->find(std::string(str)); }
|
||||||
|
|
||||||
/*PROCESSING FUNCTIONS*/
|
/*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); }
|
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
|
//! @brief Options in the set
|
||||||
std::vector<option> option_vec;
|
std::vector<option> option_vec;
|
||||||
|
|
||||||
//! @brief Ordered result of option processing
|
//! @brief Ordered result of option processing
|
||||||
std::vector<option> option_sequence;
|
std::vector<option> option_sequence;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -50,7 +50,7 @@ ztd::option::option(char c, bool arg, std::string helptext, std::string argname)
|
||||||
activated=false;
|
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
|
//char
|
||||||
shortDef=false;
|
shortDef=false;
|
||||||
|
|
@ -67,7 +67,7 @@ ztd::option::option(std::string const& str, bool arg, std::string helptext, std:
|
||||||
activated=false;
|
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
|
//char
|
||||||
shortDef=true;
|
shortDef=true;
|
||||||
|
|
@ -139,7 +139,7 @@ void ztd::option_set::print_help(int leftpad, int rightpad) const
|
||||||
it.print_help(leftpad,rightpad);
|
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++ )
|
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;
|
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++ )
|
for( auto it=option_vec.begin() ; it!=option_vec.end() ; it++ )
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue