mirror of
https://github.com/yanyiwu/cppjieba.git
synced 2025-07-18 00:00:12 +08:00
update cppcommon for argv
This commit is contained in:
parent
56b4f1c372
commit
8b3a192037
@ -1,5 +1,6 @@
|
||||
#include "argv_functs.h"
|
||||
|
||||
|
||||
namespace CPPCOMMON
|
||||
{
|
||||
bool getArgvMap(int argc, const char* const * argv, map<string,string>& mpss)
|
||||
@ -27,16 +28,16 @@ namespace CPPCOMMON
|
||||
{
|
||||
for(int i = 0; i < argc; i++)
|
||||
{
|
||||
if(strStartsWith(argv[i], "--"))
|
||||
if(strStartsWith(argv[i], "-"))
|
||||
{
|
||||
if(i + 1 < argc && !strStartsWith(argv[i+1], "--"))
|
||||
if(i + 1 < argc && !strStartsWith(argv[i + 1], "-"))
|
||||
{
|
||||
_mpss[argv[i]] = argv[i+1];
|
||||
i++;
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
_sset.insert(argv[i]);
|
||||
}
|
||||
}
|
||||
else
|
||||
@ -52,10 +53,9 @@ namespace CPPCOMMON
|
||||
|
||||
string ArgvContext::toString()
|
||||
{
|
||||
string res;
|
||||
res += string_format("[%s]\n", joinStr(_args, ", ").c_str());
|
||||
res += mapToString<string, string>(_mpss);
|
||||
return res;
|
||||
stringstream ss;
|
||||
ss<<vecToString<string>(_args)<<mapToString<string, string>(_mpss)<<setToString<string>(_sset);
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
string ArgvContext::operator [](uint i)
|
||||
@ -77,6 +77,14 @@ namespace CPPCOMMON
|
||||
return "";
|
||||
}
|
||||
|
||||
bool ArgvContext::isKeyExist(const string& key)
|
||||
{
|
||||
if(_mpss.find(key) != _mpss.end() || _sset.find(key) != _sset.end())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -94,6 +102,7 @@ int main(int argc, char** argv)
|
||||
cout<<arg[1]<<endl;
|
||||
cout<<arg["--hehe"]<<endl;
|
||||
cout<<pairToString<int,double>(pair<int, double>(1,1.2))<<endl;
|
||||
cout<<arg.isKeyExist("-help")<<endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -6,8 +6,11 @@
|
||||
#ifndef CPPCOMMON_ARGV_FUNCTS_H
|
||||
#define CPPCOMMON_ARGV_FUNCTS_H
|
||||
|
||||
#include <set>
|
||||
#include <sstream>
|
||||
#include "str_functs.h"
|
||||
#include "map_functs.h"
|
||||
#include "vec_functs.h"
|
||||
|
||||
namespace CPPCOMMON
|
||||
{
|
||||
@ -22,9 +25,12 @@ namespace CPPCOMMON
|
||||
string toString();
|
||||
string operator [](uint i);
|
||||
string operator [](const string& key);
|
||||
public:
|
||||
bool isKeyExist(const string& key);
|
||||
private:
|
||||
vector<string> _args;
|
||||
map<string, string> _mpss;
|
||||
set<string> _sset;
|
||||
|
||||
};
|
||||
}
|
||||
|
@ -8,6 +8,7 @@
|
||||
#define CPPCOMMON_MAP_FUNCTS_H
|
||||
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
@ -15,6 +16,27 @@ namespace CPPCOMMON
|
||||
{
|
||||
using namespace std;
|
||||
|
||||
template <typename T>
|
||||
string setToString(const set<T>& st)
|
||||
{
|
||||
if(st.empty())
|
||||
{
|
||||
return "{}";
|
||||
}
|
||||
stringstream ss;
|
||||
ss<<'{';
|
||||
typename set<T>::const_iterator it = st.begin();
|
||||
ss<<*it;
|
||||
it++;
|
||||
while(it != st.end())
|
||||
{
|
||||
ss<<", "<<*it;
|
||||
it++;
|
||||
}
|
||||
ss<<'}';
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
template<typename T1, typename T2>
|
||||
string mapToString(const map<T1, T2>& mp)
|
||||
{
|
||||
@ -32,7 +54,7 @@ namespace CPPCOMMON
|
||||
ss<<", "<<it->first<<": "<<it->second;
|
||||
it++;
|
||||
}
|
||||
ss<<"}";
|
||||
ss<<'}';
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
|
@ -11,9 +11,26 @@
|
||||
#include <vector>
|
||||
#include <utility>
|
||||
#include <algorithm>
|
||||
#include <sstream>
|
||||
namespace CPPCOMMON
|
||||
{
|
||||
using namespace std;
|
||||
template<typename T>
|
||||
string vecToString(const vector<T>& vec)
|
||||
{
|
||||
if(vec.empty())
|
||||
{
|
||||
return "[]";
|
||||
}
|
||||
stringstream ss;
|
||||
ss<<"["<<vec[0];
|
||||
for(unsigned int i = 1; i < vec.size(); i++)
|
||||
{
|
||||
ss<<","<<vec[i];
|
||||
}
|
||||
ss<<"]";
|
||||
return ss.str();
|
||||
}
|
||||
template<typename T>
|
||||
bool isInVec(const vector<T>& vec, const T& item)
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user