From 617067fda9cb744972f7989e61adecb64c2396f4 Mon Sep 17 00:00:00 2001 From: zawz Date: Thu, 18 Mar 2021 15:39:48 +0100 Subject: [PATCH] add option process structure --- Makefile | 2 +- include/options.hpp | 25 +++++++++++++++++++++++-- src/options.cpp | 21 +++++++++++++-------- 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 31ecafd..8bc94fa 100644 --- a/Makefile +++ b/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)) diff --git a/include/options.hpp b/include/options.hpp index 3e46513..3867e51 100644 --- a/include/options.hpp +++ b/include/options.hpp @@ -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 process(std::vector arguments, struct process_arguments behavior); + //! @brief Process arguments through the option set + /*! + @see process(std::vector arguments, struct option_process_arguments behavior) + */ + inline std::vector 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 diff --git a/src/options.cpp b/src/options.cpp index c193312..d0f4833 100644 --- a/src/options.cpp +++ b/src/options.cpp @@ -161,6 +161,10 @@ ztd::option* ztd::option_set::find(const std::string& str) } std::vector ztd::option_set::process(std::vector 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 ztd::option_set::process(std::vector arguments, struct ztd::option_set::process_arguments behavior) { std::vector out; unsigned int i=0; @@ -177,7 +181,7 @@ std::vector ztd::option_set::process(std::vector 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 ztd::option_set::process(std::vector 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 ztd::option_set::process(std::vector 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(it, arguments.end()); out.push_back(*it); } @@ -240,7 +244,7 @@ std::vector ztd::option_set::process(std::vector 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 ztd::option_set::process(std::vector argum } // if opt else { - if(stop_on_argument) + if(behavior.stop_on_argument) return std::vector(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);