#include "iclProgArg.h" #include "iclMacros.h" #include #include #include #include #include using namespace std; namespace icl{ namespace { typedef vector svec; typedef map svecmap; typedef map smap; static svec s_oArgs; // contains all arguments that were actually given static svecmap s_oArgMap; // contains all args with their actually given sub-args static smap s_oExplanations; // optinally contains explanations for some or all args static bool s_bCheckParams; static string s_sProgName; static unsigned int s_iArgCount; static std::map s_oARG_LUT; static std::vector s_vecDanglingArgs; inline const char &last(const string &s){ // {{{ open static const char _def(0); if(s=="") return _def; return s[s.length()-1]; } // }}} inline int extract_arg_count(const string &s){ // {{{ open size_t n = StrTok(s,"(").allTokens().size(); string nr = (StrTok(s,"(").allTokens())[n-1]; nr = nr.substr(0,nr.length()-1); return atoi(nr.c_str()); } // }}} } void pa_init(int nArgs, char **ppcArg, std::string allowedParams, bool skipUnknownArgs){ // {{{ open bool showUsage = false; s_sProgName = *ppcArg++; s_iArgCount = --nArgs; // create the list of params for(int i=0;i(0); i++; //for(int sa=0;sa::iterator it = s_oARG_LUT.begin(); it != s_oARG_LUT.end();++it){ const std::string &arg = (*it).first; int &n = (*it).second; std::string &ex = s_oExplanations[arg]; string line = "\t"+arg; if(n){ line += string("(")+str(n)+") : "; }else{ line += " : "; } int len = (int)line.length(); std::vector exLines = StrTok(ex,"\n").allTokens(); if(!exLines.size()){ printf("%s\n",line.c_str()); }else{ for(unsigned int i=0;i &pa_dangling_args(){ // {{{ open return s_vecDanglingArgs; } // }}} const std::string &pa_arg_internal(unsigned int index) throw (ICLException){ // {{{ open if(s_oArgs.size() <= index) throw ICLException(str(__FUNCTION__)+": invalid argument index"); return s_oArgs[index]; } // }}} const std::string &pa_subarg_internal(const std::string &arg,unsigned int idx) throw (ICLException){ // {{{ open svecmap::const_iterator it = s_oArgMap.find(arg); if(it == s_oArgMap.end()) throw ICLException(str(__FUNCTION__)+": invalid argument index"); const svec &subargs = it->second; if(subargs.size()<=idx) throw ICLException(str(__FUNCTION__)+": invalid sub argument index"); return subargs[idx]; } // }}} void pa_explain(const std::string &arg, const std::string &explanation){ // {{{ open if(s_oExplanations.find(arg) != s_oExplanations.end()){ WARNING_LOG("Arg \"" << arg << "\" was already explained by " << std::endl << "\"" << explanation << "\""); } s_oExplanations[arg] = explanation; } // }}} }