options: added possibility to ignore negative numbers as options

This commit is contained in:
zawz 2019-10-04 02:47:42 +02:00
parent 03bf9107e4
commit ec2c2c8c37
2 changed files with 49 additions and 40 deletions

View file

@ -136,12 +136,12 @@ namespace ztd
@param arguments vector of string containing arguments and options @param arguments vector of string containing arguments and options
@return Leftover arguments that are not options\n @return Leftover arguments that are not options\n
*/ */
std::vector<std::string> process(std::vector<std::string> arguments); std::vector<std::string> process(std::vector<std::string> arguments, bool ignore_numbers=false);
//! @brief Process arguments through the option set //! @brief Process arguments through the option set
/*! /*!
calls process(std::vector<std::string> arguments) calls process(std::vector<std::string> arguments)
*/ */
inline std::vector<std::string> process(int argc, char** argv) { return this->process(ztd::argVector(argc, argv)); } inline std::vector<std::string> process(int argc, char** argv, bool ignore_numbers=false) { return this->process(ztd::argVector(argc, argv), ignore_numbers); }
//! @brief Options in the set //! @brief Options in the set
std::vector<option> option_vec; std::vector<option> option_vec;

View file

@ -159,7 +159,7 @@ ztd::option* ztd::option_set::find(std::string const& str)
return nullptr; return nullptr;
} }
std::vector<std::string> ztd::option_set::process(std::vector<std::string> arguments) std::vector<std::string> ztd::option_set::process(std::vector<std::string> arguments, bool ignore_numbers)
{ {
std::vector<std::string> out; std::vector<std::string> out;
unsigned int i=0; unsigned int i=0;
@ -213,46 +213,55 @@ std::vector<std::string> ztd::option_set::process(std::vector<std::string> argum
else else
{ {
i=1; i=1;
ztd::option* popt=nullptr; if( ignore_numbers && (*it)[i] >= '0' && (*it)[i] <= '9')
bool tstop=false;
while( !tstop && it!=arguments.end() && (*it).size()>i )
{ {
popt=this->find((*it)[i]); while(it!=arguments.end() && (*it).size()>i)
if(popt==nullptr) //not found: error
{
throw ztd::option_error(ztd::option_error::unknown_option, std::string("-") + (*it)[i] );
}
if(popt->takesArgument) //no argument
{
i++; i++;
if((*it).size()<=i) //finishes here out.push_back(*it);
{
if( ++it == arguments.end() )
{
throw ztd::option_error(ztd::option_error::missing_arg, std::string("-") + popt->charName );
}
popt->activated = true;
popt->argument = (*it);
option_sequence.push_back(*popt);
tstop = true;
}
else //continue
{
if( (*it)[i] == '=')
i++;
popt->argument = (*it).substr(i , (*it).size()-i );
popt->activated = true;
option_sequence.push_back(*popt);
tstop=true;
}
}
else //no argument
{
popt->activated = true;
option_sequence.push_back(*popt);
}
i++;
} }
else
{
ztd::option* popt=nullptr;
bool tstop=false;
while( !tstop && it!=arguments.end() && (*it).size()>i )
{
popt=this->find((*it)[i]);
if(popt==nullptr) //not found: error
{
throw ztd::option_error(ztd::option_error::unknown_option, std::string("-") + (*it)[i] );
}
if(popt->takesArgument) //no argument
{
i++;
if((*it).size()<=i) //finishes here
{
if( ++it == arguments.end() )
{
throw ztd::option_error(ztd::option_error::missing_arg, std::string("-") + popt->charName );
}
popt->activated = true;
popt->argument = (*it);
option_sequence.push_back(*popt);
tstop = true;
}
else //continue
{
if( (*it)[i] == '=')
i++;
popt->argument = (*it).substr(i , (*it).size()-i );
popt->activated = true;
option_sequence.push_back(*popt);
tstop=true;
}
}
else //no argument
{
popt->activated = true;
option_sequence.push_back(*popt);
}
i++;
} //while
} // if not number
} }
} }
else else