options: add stop_on_argument in process

This commit is contained in:
zawz 2020-01-14 11:02:42 +01:00
parent 94f35a4d9b
commit 1311bd7a64
2 changed files with 11 additions and 5 deletions

View file

@ -141,14 +141,16 @@ namespace ztd
/*! /*!
If errors are encountered, exceptions option_error are thrown If errors are encountered, exceptions option_error are thrown
@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 @param ignore_numbers negative numbers are not considered as options
@param stop_on_argument stop processing when encountering a non-option argument
@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, bool ignore_numbers=false); std::vector<std::string> process(std::vector<std::string> arguments, bool ignore_numbers=false, bool stop_on_argument=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, bool ignore_numbers, bool stop_on_argument)
*/ */
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, bool stop_on_argument=false) { return this->process(ztd::argVector(argc, argv), ignore_numbers, stop_on_argument); }
//! @brief Get option with char name //! @brief Get option with char name
/*! @see option* find(char c) /*! @see option* find(char c)

View file

@ -159,7 +159,7 @@ ztd::option* ztd::option_set::find(const std::string& str)
return nullptr; return nullptr;
} }
std::vector<std::string> ztd::option_set::process(std::vector<std::string> arguments, bool ignore_numbers) std::vector<std::string> ztd::option_set::process(std::vector<std::string> arguments, bool ignore_numbers, bool stop_on_argument)
{ {
std::vector<std::string> out; std::vector<std::string> out;
unsigned int i=0; unsigned int i=0;
@ -217,6 +217,8 @@ std::vector<std::string> ztd::option_set::process(std::vector<std::string> argum
{ {
while(it!=arguments.end() && (*it).size()>i) while(it!=arguments.end() && (*it).size()>i)
i++; i++;
if(stop_on_argument)
return std::vector<std::string>(it, arguments.end());
out.push_back(*it); out.push_back(*it);
} }
else else
@ -266,6 +268,8 @@ std::vector<std::string> ztd::option_set::process(std::vector<std::string> argum
} }
else else
{ {
if(stop_on_argument)
return std::vector<std::string>(it, arguments.end());
out.push_back(*it); out.push_back(*it);
} }
if(it == arguments.end()) if(it == arguments.end())