add option process structure
This commit is contained in:
parent
5d2ee5e001
commit
617067fda9
3 changed files with 37 additions and 11 deletions
2
Makefile
2
Makefile
|
|
@ -6,7 +6,7 @@ ODIR_SHARED=obj_so
|
|||
NAME = libztd
|
||||
|
||||
CC=g++
|
||||
CXXFLAGS= -I$(IDIR) -Wall -std=c++17 -O2
|
||||
CXXFLAGS= -I$(IDIR) -Wall -std=c++20 -O2
|
||||
|
||||
$(shell mkdir -p $(ODIR))
|
||||
$(shell mkdir -p $(ODIR_SHARED))
|
||||
|
|
|
|||
|
|
@ -99,7 +99,6 @@ namespace ztd
|
|||
inline operator std::string() const { return argument; }
|
||||
|
||||
};
|
||||
|
||||
//! @brief Set of POSIX/GNU style options
|
||||
/*!
|
||||
Process arguments through it to extract options
|
||||
|
|
@ -139,7 +138,29 @@ namespace ztd
|
|||
option* find(const std::string& str);
|
||||
inline option* find(const char* str) { return this->find(std::string(str)); }
|
||||
|
||||
/*PROCESSING FUNCTIONS*/
|
||||
/*PROCESSING*/
|
||||
//! @brief behavior for option processing
|
||||
struct process_arguments {
|
||||
bool ignore_numbers=false;
|
||||
bool stop_on_argument=false;
|
||||
bool ignore_unknown=false;
|
||||
bool stop_on_doubledash=true;
|
||||
bool output_doubledash=false;
|
||||
};
|
||||
//! @brief Process arguments through the option set
|
||||
/*!
|
||||
If errors are encountered, exceptions option_error are thrown
|
||||
@see struct option_process_arguments
|
||||
@param arguments vector of string containing arguments and options
|
||||
@param behavior behavioral changes for option processing
|
||||
@return if @a stop_on_argument unprocessed arguments\n else leftover arguments that are not options\n
|
||||
*/
|
||||
std::vector<std::string> process(std::vector<std::string> arguments, struct process_arguments behavior);
|
||||
//! @brief Process arguments through the option set
|
||||
/*!
|
||||
@see process(std::vector<std::string> arguments, struct option_process_arguments behavior)
|
||||
*/
|
||||
inline std::vector<std::string> process(int argc, char** argv, struct process_arguments behavior) { return this->process(ztd::argVector(argc, argv), behavior); }
|
||||
//! @brief Process arguments through the option set
|
||||
/*!
|
||||
If errors are encountered, exceptions option_error are thrown
|
||||
|
|
|
|||
|
|
@ -161,6 +161,10 @@ ztd::option* ztd::option_set::find(const std::string& str)
|
|||
}
|
||||
|
||||
std::vector<std::string> ztd::option_set::process(std::vector<std::string> arguments, bool ignore_numbers, bool stop_on_argument, bool ignore_unknown)
|
||||
{
|
||||
return this->process(arguments, {.ignore_numbers=ignore_numbers, .stop_on_argument=stop_on_argument, .ignore_unknown=ignore_unknown});
|
||||
}
|
||||
std::vector<std::string> ztd::option_set::process(std::vector<std::string> arguments, struct ztd::option_set::process_arguments behavior)
|
||||
{
|
||||
std::vector<std::string> out;
|
||||
unsigned int i=0;
|
||||
|
|
@ -177,7 +181,7 @@ std::vector<std::string> ztd::option_set::process(std::vector<std::string> argum
|
|||
ztd::option* popt = this->find( it->substr(2,eqn-2) );
|
||||
if(popt == nullptr) // unknown -- opt
|
||||
{
|
||||
if(!ignore_unknown)
|
||||
if(!behavior.ignore_unknown)
|
||||
throw ztd::option_error(ztd::option_error::unknown_option, "--" + it->substr(2,eqn-2));
|
||||
// add to ret if ignore
|
||||
out.push_back(*it);
|
||||
|
|
@ -202,7 +206,7 @@ std::vector<std::string> ztd::option_set::process(std::vector<std::string> argum
|
|||
ztd::option* popt = this->find( it->substr(2,eqn-2) ); // get option
|
||||
if(popt == nullptr) // unknown -- opt
|
||||
{
|
||||
if(!ignore_unknown)
|
||||
if(!behavior.ignore_unknown)
|
||||
throw ztd::option_error(ztd::option_error::unknown_option, "--" +it->substr(2,eqn-2));
|
||||
// add to ret if ignore
|
||||
out.push_back(*it);
|
||||
|
|
@ -222,11 +226,11 @@ std::vector<std::string> ztd::option_set::process(std::vector<std::string> argum
|
|||
else if(it->size()>1 && (*it)[0] == '-' && (*it)[1] != '-')
|
||||
{
|
||||
i=1;
|
||||
if( ignore_numbers && (*it)[i] >= '0' && (*it)[i] <= '9') // ignore numbers : add as ret arg
|
||||
if( behavior.ignore_numbers && (*it)[i] >= '0' && (*it)[i] <= '9') // ignore numbers : add as ret arg
|
||||
{
|
||||
while(it!=arguments.end() && it->size()>i)
|
||||
i++;
|
||||
if(stop_on_argument)
|
||||
if(behavior.stop_on_argument)
|
||||
return std::vector<std::string>(it, arguments.end());
|
||||
out.push_back(*it);
|
||||
}
|
||||
|
|
@ -240,7 +244,7 @@ std::vector<std::string> ztd::option_set::process(std::vector<std::string> argum
|
|||
popt=this->find((*it)[i]);
|
||||
if(popt==nullptr) // unknown opt
|
||||
{
|
||||
if(!ignore_unknown)
|
||||
if(!behavior.ignore_unknown)
|
||||
throw ztd::option_error(ztd::option_error::unknown_option, std::string("-") + (*it)[i] );
|
||||
// add to ret if ignore
|
||||
ropt += (*it)[i];
|
||||
|
|
@ -283,11 +287,12 @@ std::vector<std::string> ztd::option_set::process(std::vector<std::string> argum
|
|||
} // if opt
|
||||
else
|
||||
{
|
||||
if(stop_on_argument)
|
||||
if(behavior.stop_on_argument)
|
||||
return std::vector<std::string>(it, arguments.end());
|
||||
if( *it == "--" ) // empty -- : stop here
|
||||
if( behavior.stop_on_doubledash && *it == "--" ) // empty -- : stop here
|
||||
{
|
||||
out.insert(out.end(), ++it, arguments.end());
|
||||
if(behavior.output_doubledash)
|
||||
out.insert(out.end(), ++it, arguments.end());
|
||||
return out;
|
||||
}
|
||||
out.push_back(*it);
|
||||
|
|
|
|||
Loading…
Reference in a new issue