30 typedef std::pair<std::string,std::string>
arg;
34 mem::increment(s_class().c_str());
37 args(
int a_argc,
char* a_argv[]){
39 mem::increment(s_class().c_str());
41 for(
int index=0;index<a_argc;index++) {
42 std::string s(a_argv[index]);
43 std::string::size_type pos = s.find(
'=');
44 if(pos==std::string::npos) {
47 std::string key = s.substr(0,pos);
49 std::string
value = s.substr(pos,s.size()-pos);
54 args(
const std::vector<std::string>& a_args,
bool a_strip =
false){
56 mem::increment(s_class().c_str());
62 mem::increment(s_class().c_str());
65 args(
const std::string& a_args,
const std::string& a_sep,
bool a_strip){
67 mem::increment(s_class().c_str());
69 std::vector<std::string> _args;
70 words(a_args,a_sep,
false,_args);
75 mem::decrement(s_class().c_str());
81 mem::increment(s_class().c_str());
92 bool is_arg(
const std::string& a_string)
const {
94 if((*it).first==a_string)
return true;
101 bool find(
const std::string& a_key,std::string& a_value,
const std::string& a_def = std::string())
const {
103 if((*it).first==a_key) {
104 a_value = (*it).second;
111 #ifdef TOOLS_DEPRECATED
112 std::vector<std::string>
find(
const std::string& a_key)
const {
113 std::vector<std::string> vals;
115 if((*it).first==a_key) vals.push_back((*it).second);
120 void find(
const std::string& a_key,std::vector<std::string>& a_vals,
bool a_clear=
true)
const {
121 if(a_clear) a_vals.clear();
123 if((*it).first==a_key) a_vals.push_back((*it).second);
127 bool find(
const std::string& a_string,
bool& a_value,
const bool& a_def =
false)
const {
129 if(!
find(a_string,s)) {a_value = a_def;
return false;}
130 return to(s,a_value,a_def);
133 bool find(
const std::string& a_string,aT& a_value,
const aT& a_def = aT())
const {
135 if(!
find(a_string,_s)) {a_value = a_def;
return false;}
136 return to<aT>(_s,a_value,a_def);
139 #ifdef TOOLS_DEPRECATED
140 std::vector<std::string> tovector()
const {
142 std::vector<std::string> vec;
145 if((*it).second.empty()) {
163 if((*it).second.size()) {
171 bool add(
const std::string& a_key,
const std::string& a_value = std::string(),
bool a_override =
true){
174 if((*it).first==a_key) {
175 (*it).second = a_value;
180 if(a_key.empty())
return false;
185 bool insert_begin(
const std::string& a_key,
const std::string& a_value = std::string(),
bool a_override =
true){
188 if((*it).first==a_key) {
189 (*it).second = a_value;
194 if(a_key.empty())
return false;
199 void add(
const std::vector<std::string>& a_args,
bool a_strip =
false) {
201 const std::string& sarg = *it;
202 std::string::size_type pos = sarg.find(
'=');
203 if(pos==std::string::npos) {
205 std::string
left = sarg;
212 std::string
left = sarg.substr(0,pos);
213 std::string
right = sarg.substr((pos+1),sarg.size()-(pos+1));
223 void add_keyvals(
const std::vector<std::string>& a_args,
bool a_strip =
false) {
225 size_t sz_half = a_args.size()/2;
226 if((2*sz_half)!=a_args.size())
return;
227 for(std::vector<std::string>::const_iterator it = a_args.begin();it!=a_args.end();it+=2) {
229 std::string key = *it;
231 std::string val = *(it+1);
240 void add(
const std::vector<arg>& a_args){
250 size_t nbeg =
m_args.size();
251 for(std::vector<arg>::iterator it =
m_args.begin();it!=
m_args.end();) {
252 if(a_key==(*it).first) {
258 return int(nbeg) - int(
m_args.size());
264 bool last(std::string& a_key,std::string& a_value)
const {
267 if(
m_args.empty())
return false;
268 a_key =
m_args.back().first;
269 a_value =
m_args.back().second;
274 if(
m_args.empty()) {a_value.clear();
return false;}
275 if(
m_args[0].second.size()) {a_value.clear();
return false;}
276 a_value =
m_args[0].first;
280 bool file(std::string& a_file)
const {
285 && (slast.find(
'-')!=0)
295 bool file(std::string& a_file,
bool a_remove) {
300 && (slast.find(
'-')!=0)
311 void not_hyphens(std::vector<std::string>& a_not_hyphens,
bool a_skip_first =
false)
const {
312 a_not_hyphens.clear();
317 if(
m_args.empty())
return;
318 std::vector<arg>::const_iterator it =
m_args.begin();
319 if(a_skip_first) it++;
320 for(;it!=
m_args.end();++it) {
321 if( ((*it).first.find(
'-')==0) || (*it).second.size() ) {
322 a_not_hyphens.clear();
324 a_not_hyphens.push_back((*it).first);
329 void files(std::vector<std::string>& a_files,
bool a_skip_first =
true)
const {
334 std::vector<std::string> _ss;
336 if(_ss.empty()) {a_first.clear();
return false;}
343 bool argcv(
int& a_argc,
char**& a_argv)
const {
350 if(
m_args.empty()) {a_argc = 0;a_argv = 0;
return true;}
351 typedef char* _cstr_t;
352 _cstr_t* av =
new _cstr_t[
m_args.size()+1];
353 if(!av) {a_argc = 0;a_argv = 0;
return false;}
355 for(std::vector<arg>::const_iterator it =
m_args.begin();it!=
m_args.end();++it,av++) {
356 std::string::size_type lf = (*it).first.length();
357 std::string::size_type ls = (*it).second.length();
358 std::string::size_type sz = lf;
360 char* p =
new char[sz+1];
361 if(!p) {a_argc = 0;a_argv = 0;
return false;}
363 {
char* pf = (
char*)(*it).first.c_str();
364 for(std::string::size_type i=0;i<lf;i++,p++,pf++) {*p = *pf;}
366 if(ls) {*p =
'=';p++;}
367 {
char* ps = (
char*)(*it).second.c_str();
368 for(std::string::size_type i=0;i<ls;i++,p++,ps++) {*p = *ps;}
371 *(a_argv+
m_args.size()) = 0;
372 a_argc = (int)
m_args.size();
376 for(
int index=0;index<a_argc;index++)
delete [] a_argv[index];
384 if((*it).first.find(
'-')==0) {
387 if((*it).first==(*it2)) {
392 if(!
found)
return false;
400 if((*it).first.find(
'-')==0) {
401 if((*it).first!=a_known)
return false;
421 if(
m_args.empty())
return;
422 std::vector<arg> _args;
423 if(a_skip_first) _args.push_back(*(
m_args.begin()));
425 {std::vector<arg>::const_iterator it =
m_args.begin();
426 if(a_skip_first) it++;
427 for(;it!=
m_args.end();++it) {
428 if( ((*it).first.find(
'-')==0) || (*it).second.size() ) {
429 _args.push_back(*it);
433 {std::vector<arg>::const_iterator it =
m_args.begin();
434 if(a_skip_first) it++;
435 for(;it!=
m_args.end();++it) {
436 if( ((*it).first.find(
'-')==0) || (*it).second.size() ) {
438 _args.push_back(*it);
445 void dump(std::ostream& a_out,
const std::string& a_comment = std::string(),
const std::string& a_prefix = std::string())
const {
446 if(a_comment.size()) a_out << a_comment << std::endl;
448 a_out << a_prefix <<
"key = " <<
sout((*it).first) <<
", value = " <<
sout((*it).second) << std::endl;
458 inline bool check_args(
const std::vector<std::string>& a_args,
unsigned int a_number,std::ostream& a_out){
459 if(a_args.size()==a_number)
return true;
460 a_out <<
"bad argument number."
461 <<
" Given " << (
unsigned int)a_args.size()
462 <<
" whilst " << a_number <<
" expected."
467 inline bool check_min(
const std::vector<std::string>& a_args,
unsigned int a_number,std::string& a_last,std::ostream& a_out){
468 if(a_args.size()>=a_number) {
474 for(
size_t index=1;index<a_args.size();index++) a_last +=
" " + a_args[index];
477 a_last = a_args[a_number-1];
478 for(
size_t index=a_number;index<a_args.size();index++) a_last +=
" " + a_args[index];
482 a_out <<
"bad argument number."
483 <<
" Given " << (
unsigned int)a_args.size()
484 <<
" whilst at least " << a_number <<
" expected."
489 inline bool check_min_args(
const std::vector<std::string>& aArgs,
unsigned int a_number,std::ostream& a_out){
490 if(aArgs.size()>=a_number)
return true;
491 a_out <<
"bad argument number."
492 <<
" Given " << (
unsigned int)aArgs.size()
493 <<
" whilst at least " << a_number <<
" expected."
498 inline bool check_or_args(
const std::vector<std::string>& aArgs,
unsigned int a_1,
unsigned int a_2,std::ostream& a_out){
499 if((aArgs.size()==a_1)||(aArgs.size()==a_2))
return true;
500 a_out <<
"bad argument number."
501 <<
" Given " << (
unsigned int)aArgs.size()
502 <<
" whilst " << a_1 <<
" or " << a_2 <<
" expected."
507 inline std::vector<std::string>
to(
int a_argc,
char** a_argv) {
508 std::vector<std::string> v;
509 for(
int index=0;index<a_argc;index++) v.push_back(a_argv[index]);