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"
|
#include "argv_functs.h"
|
||||||
|
|
||||||
|
|
||||||
namespace CPPCOMMON
|
namespace CPPCOMMON
|
||||||
{
|
{
|
||||||
bool getArgvMap(int argc, const char* const * argv, map<string,string>& mpss)
|
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++)
|
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];
|
_mpss[argv[i]] = argv[i+1];
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
break;
|
_sset.insert(argv[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -52,10 +53,9 @@ namespace CPPCOMMON
|
|||||||
|
|
||||||
string ArgvContext::toString()
|
string ArgvContext::toString()
|
||||||
{
|
{
|
||||||
string res;
|
stringstream ss;
|
||||||
res += string_format("[%s]\n", joinStr(_args, ", ").c_str());
|
ss<<vecToString<string>(_args)<<mapToString<string, string>(_mpss)<<setToString<string>(_sset);
|
||||||
res += mapToString<string, string>(_mpss);
|
return ss.str();
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
string ArgvContext::operator [](uint i)
|
string ArgvContext::operator [](uint i)
|
||||||
@ -77,6 +77,14 @@ namespace CPPCOMMON
|
|||||||
return "";
|
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[1]<<endl;
|
||||||
cout<<arg["--hehe"]<<endl;
|
cout<<arg["--hehe"]<<endl;
|
||||||
cout<<pairToString<int,double>(pair<int, double>(1,1.2))<<endl;
|
cout<<pairToString<int,double>(pair<int, double>(1,1.2))<<endl;
|
||||||
|
cout<<arg.isKeyExist("-help")<<endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,8 +6,11 @@
|
|||||||
#ifndef CPPCOMMON_ARGV_FUNCTS_H
|
#ifndef CPPCOMMON_ARGV_FUNCTS_H
|
||||||
#define CPPCOMMON_ARGV_FUNCTS_H
|
#define CPPCOMMON_ARGV_FUNCTS_H
|
||||||
|
|
||||||
|
#include <set>
|
||||||
|
#include <sstream>
|
||||||
#include "str_functs.h"
|
#include "str_functs.h"
|
||||||
#include "map_functs.h"
|
#include "map_functs.h"
|
||||||
|
#include "vec_functs.h"
|
||||||
|
|
||||||
namespace CPPCOMMON
|
namespace CPPCOMMON
|
||||||
{
|
{
|
||||||
@ -22,9 +25,12 @@ namespace CPPCOMMON
|
|||||||
string toString();
|
string toString();
|
||||||
string operator [](uint i);
|
string operator [](uint i);
|
||||||
string operator [](const string& key);
|
string operator [](const string& key);
|
||||||
|
public:
|
||||||
|
bool isKeyExist(const string& key);
|
||||||
private:
|
private:
|
||||||
vector<string> _args;
|
vector<string> _args;
|
||||||
map<string, string> _mpss;
|
map<string, string> _mpss;
|
||||||
|
set<string> _sset;
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
#define CPPCOMMON_MAP_FUNCTS_H
|
#define CPPCOMMON_MAP_FUNCTS_H
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <set>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
@ -15,6 +16,27 @@ namespace CPPCOMMON
|
|||||||
{
|
{
|
||||||
using namespace std;
|
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>
|
template<typename T1, typename T2>
|
||||||
string mapToString(const map<T1, T2>& mp)
|
string mapToString(const map<T1, T2>& mp)
|
||||||
{
|
{
|
||||||
@ -32,7 +54,7 @@ namespace CPPCOMMON
|
|||||||
ss<<", "<<it->first<<": "<<it->second;
|
ss<<", "<<it->first<<": "<<it->second;
|
||||||
it++;
|
it++;
|
||||||
}
|
}
|
||||||
ss<<"}";
|
ss<<'}';
|
||||||
return ss.str();
|
return ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,9 +11,26 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <sstream>
|
||||||
namespace CPPCOMMON
|
namespace CPPCOMMON
|
||||||
{
|
{
|
||||||
using namespace std;
|
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>
|
template<typename T>
|
||||||
bool isInVec(const vector<T>& vec, const T& item)
|
bool isInVec(const vector<T>& vec, const T& item)
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user