g4tools  5.4.0
Public Types | Public Member Functions | Static Public Member Functions | Protected Attributes | List of all members
tools::args Class Reference

Public Types

typedef std::pair< std::string, std::string > arg
 

Public Member Functions

 args ()
 
 args (int a_argc, char *a_argv[])
 
 args (const std::vector< std::string > &a_args, bool a_strip=false)
 
 args (const std::vector< arg > &a_args)
 
 args (const std::string &a_args, const std::string &a_sep, bool a_strip)
 
virtual ~args ()
 
 args (const args &a_from)
 
argsoperator= (const args &a_from)
 
const std::vector< arg > & get_args () const
 
bool is_arg (const std::string &a_string) const
 
bool is_empty () const
 
size_t size () const
 
size_t number () const
 
bool find (const std::string &a_key, std::string &a_value, const std::string &a_def=std::string()) const
 
void find (const std::string &a_key, std::vector< std::string > &a_vals, bool a_clear=true) const
 
bool find (const std::string &a_string, bool &a_value, const bool &a_def=false) const
 
template<class aT >
bool find (const std::string &a_string, aT &a_value, const aT &a_def=aT()) const
 
void to_vector (std::vector< std::string > &a_vec) const
 
bool add (const std::string &a_key, const std::string &a_value=std::string(), bool a_override=true)
 
bool insert_begin (const std::string &a_key, const std::string &a_value=std::string(), bool a_override=true)
 
void add (const std::vector< std::string > &a_args, bool a_strip=false)
 
void add_keyvals (const std::vector< std::string > &a_args, bool a_strip=false)
 
void add (const std::vector< arg > &a_args)
 
void add (const args &a_from)
 
int remove (const std::string &a_key)
 
void remove_first ()
 
void remove_last ()
 
bool last (std::string &a_key, std::string &a_value) const
 
bool prog_name (std::string &a_value) const
 
bool file (std::string &a_file) const
 
bool file (std::string &a_file, bool a_remove)
 
void not_hyphens (std::vector< std::string > &a_not_hyphens, bool a_skip_first=false) const
 
void files (std::vector< std::string > &a_files, bool a_skip_first=true) const
 
bool first_not_hyphen (std::string &a_first, bool a_skip_first=false) const
 
bool argcv (int &a_argc, char **&a_argv) const
 
bool known_options (const std::vector< std::string > &a_knowns) const
 
bool known_options (const std::string &a_known) const
 
void files_at_end (bool a_skip_first=true)
 
void dump (std::ostream &a_out, const std::string &a_comment=std::string(), const std::string &a_prefix=std::string()) const
 
bool isAnArgument (const std::string &a_key) const
 

Static Public Member Functions

static void delete_argcv (int &a_argc, char **&a_argv)
 

Protected Attributes

std::vector< argm_args
 

Detailed Description

Definition at line 24 of file args.

Member Typedef Documentation

◆ arg

typedef std::pair<std::string,std::string> tools::args::arg

Definition at line 30 of file args.

Constructor & Destructor Documentation

◆ args() [1/6]

tools::args::args ( )
inline

Definition at line 32 of file args.

32  {
33 #ifdef TOOLS_MEM
34  mem::increment(s_class().c_str());
35 #endif
36  }

◆ args() [2/6]

tools::args::args ( int  a_argc,
char *  a_argv[] 
)
inline

Definition at line 37 of file args.

37  {
38 #ifdef TOOLS_MEM
39  mem::increment(s_class().c_str());
40 #endif
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) {
45  m_args.push_back(arg(s,""));
46  } else {
47  std::string key = s.substr(0,pos);
48  pos++;
49  std::string value = s.substr(pos,s.size()-pos);
50  m_args.push_back(arg(key,value));
51  }
52  }
53  }

◆ args() [3/6]

tools::args::args ( const std::vector< std::string > &  a_args,
bool  a_strip = false 
)
inline

Definition at line 54 of file args.

54  {
55 #ifdef TOOLS_MEM
56  mem::increment(s_class().c_str());
57 #endif
58  add(a_args,a_strip);
59  }

◆ args() [4/6]

tools::args::args ( const std::vector< arg > &  a_args)
inline

Definition at line 60 of file args.

60  :m_args(a_args){
61 #ifdef TOOLS_MEM
62  mem::increment(s_class().c_str());
63 #endif
64  }

◆ args() [5/6]

tools::args::args ( const std::string &  a_args,
const std::string &  a_sep,
bool  a_strip 
)
inline

Definition at line 65 of file args.

65  {
66 #ifdef TOOLS_MEM
67  mem::increment(s_class().c_str());
68 #endif
69  std::vector<std::string> _args;
70  words(a_args,a_sep,false,_args);
71  add(_args,a_strip);
72  }

◆ ~args()

virtual tools::args::~args ( )
inlinevirtual

Definition at line 73 of file args.

73  {
74 #ifdef TOOLS_MEM
75  mem::decrement(s_class().c_str());
76 #endif
77  }

◆ args() [6/6]

tools::args::args ( const args a_from)
inline

Definition at line 79 of file args.

79  :m_args(a_from.m_args){
80 #ifdef TOOLS_MEM
81  mem::increment(s_class().c_str());
82 #endif
83  }

Member Function Documentation

◆ add() [1/4]

void tools::args::add ( const args a_from)
inline

Definition at line 244 of file args.

244  {
245  tools_vforcit(arg,a_from.m_args,it) m_args.push_back(*it);
246  }

◆ add() [2/4]

bool tools::args::add ( const std::string &  a_key,
const std::string &  a_value = std::string(),
bool  a_override = true 
)
inline

Definition at line 171 of file args.

171  {
172  if(a_override) {
173  tools_vforit(arg,m_args,it) {
174  if((*it).first==a_key) {
175  (*it).second = a_value;
176  return true;
177  }
178  }
179  }
180  if(a_key.empty()) return false;
181  m_args.push_back(arg(a_key,a_value));
182  return true;
183  }

◆ add() [3/4]

void tools::args::add ( const std::vector< arg > &  a_args)
inline

Definition at line 240 of file args.

240  {
241  tools_vforcit(arg,a_args,it) m_args.push_back(*it);
242  }

◆ add() [4/4]

void tools::args::add ( const std::vector< std::string > &  a_args,
bool  a_strip = false 
)
inline

Definition at line 199 of file args.

199  {
200  tools_vforcit(std::string,a_args,it) {
201  const std::string& sarg = *it;
202  std::string::size_type pos = sarg.find('=');
203  if(pos==std::string::npos) {
204  if(a_strip) {
205  std::string left = sarg;
206  strip(left,both,' ');
207  m_args.push_back(arg(left,""));
208  } else {
209  m_args.push_back(arg(sarg,""));
210  }
211  } else {
212  std::string left = sarg.substr(0,pos);
213  std::string right = sarg.substr((pos+1),sarg.size()-(pos+1));
214  if(a_strip) {
215  strip(left,both,' ');
216  strip(right,both,' ');
217  }
218  m_args.push_back(arg(left,right));
219  }
220  }
221  }

◆ add_keyvals()

void tools::args::add_keyvals ( const std::vector< std::string > &  a_args,
bool  a_strip = false 
)
inline

Definition at line 223 of file args.

223  {
224  //a_args must contain an even number of strings.
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) {
228  if(a_strip) {
229  std::string key = *it;
230  strip(key,both,' ');
231  std::string val = *(it+1);
232  strip(val,both,' ');
233  m_args.push_back(arg(key,val));
234  } else {
235  m_args.push_back(arg(*it,*(it+1)));
236  }
237  }
238  }

◆ argcv()

bool tools::args::argcv ( int &  a_argc,
char **&  a_argv 
) const
inline

Definition at line 343 of file args.

343  {
344  // If using with :
345  // int argc;
346  // char** argv;
347  // args.argcv(argc,argv);
348  // you can delete with :
349  // args::delete_argcv(argc,argv);
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;}
354  a_argv = av;
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;
359  if(ls) sz += 1 + ls;
360  char* p = new char[sz+1];
361  if(!p) {a_argc = 0;a_argv = 0;return false;} //some delete are lacking.
362  *av = p;
363  {char* pf = (char*)(*it).first.c_str();
364  for(std::string::size_type i=0;i<lf;i++,p++,pf++) {*p = *pf;}
365  *p = 0;}
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;}
369  *p = 0;}
370  }
371  *(a_argv+m_args.size()) = 0;
372  a_argc = (int)m_args.size();
373  return true;
374  }

◆ delete_argcv()

static void tools::args::delete_argcv ( int &  a_argc,
char **&  a_argv 
)
inlinestatic

Definition at line 375 of file args.

375  {
376  for(int index=0;index<a_argc;index++) delete [] a_argv[index];
377  delete [] a_argv;
378  a_argc = 0;
379  a_argv = 0;
380  }

◆ dump()

void tools::args::dump ( std::ostream &  a_out,
const std::string &  a_comment = std::string(),
const std::string &  a_prefix = std::string() 
) const
inline

Definition at line 445 of file args.

445  {
446  if(a_comment.size()) a_out << a_comment << std::endl;
447  tools_vforcit(arg,m_args,it) {
448  a_out << a_prefix << "key = " << sout((*it).first) << ", value = " << sout((*it).second) << std::endl;
449  }
450  }

◆ file() [1/2]

bool tools::args::file ( std::string &  a_file) const
inline

Definition at line 280 of file args.

280  {
281  std::string slast;
282  std::string s;
283  if((m_args.size()>1) //first arg is the program name !
284  && last(slast,s)
285  && (slast.find('-')!=0)
286  && (s.empty()) ) {
287  a_file = slast; //Last argument is not an option.
288  return true;
289  } else {
290  a_file.clear();
291  return false;
292  }
293  }

◆ file() [2/2]

bool tools::args::file ( std::string &  a_file,
bool  a_remove 
)
inline

Definition at line 295 of file args.

295  {
296  std::string slast;
297  std::string s;
298  if((m_args.size()>1) //first arg is the program name !
299  && last(slast,s)
300  && (slast.find('-')!=0)
301  && (s.empty()) ) {
302  a_file = slast; //Last argument is not an option.
303  if(a_remove) m_args.erase(m_args.end()-1);
304  return true;
305  } else {
306  a_file.clear();
307  return false;
308  }
309  }

◆ files()

void tools::args::files ( std::vector< std::string > &  a_files,
bool  a_skip_first = true 
) const
inline

Definition at line 329 of file args.

329  { //true and not false as in the upper.
330  return not_hyphens(a_files,a_skip_first);
331  }

◆ files_at_end()

void tools::args::files_at_end ( bool  a_skip_first = true)
inline

Definition at line 419 of file args.

419  {
420  // reorder to have "file" arguments at end.
421  if(m_args.empty()) return;
422  std::vector<arg> _args;
423  if(a_skip_first) _args.push_back(*(m_args.begin()));
424  //first pass :
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);
430  }
431  }}
432  //second pass :
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() ) {
437  } else {
438  _args.push_back(*it);
439  }
440  }}
441  m_args = _args;
442  }

◆ find() [1/4]

bool tools::args::find ( const std::string &  a_key,
std::string &  a_value,
const std::string &  a_def = std::string() 
) const
inline

Definition at line 101 of file args.

101  {
102  tools_vforcit(arg,m_args,it) {
103  if((*it).first==a_key) {
104  a_value = (*it).second;
105  return true;
106  }
107  }
108  a_value = a_def;
109  return false;
110  }

◆ find() [2/4]

void tools::args::find ( const std::string &  a_key,
std::vector< std::string > &  a_vals,
bool  a_clear = true 
) const
inline

Definition at line 120 of file args.

120  {
121  if(a_clear) a_vals.clear();
122  tools_vforcit(arg,m_args,it) {
123  if((*it).first==a_key) a_vals.push_back((*it).second);
124  }
125  }

◆ find() [3/4]

template<class aT >
bool tools::args::find ( const std::string &  a_string,
aT &  a_value,
const aT &  a_def = aT() 
) const
inline

Definition at line 133 of file args.

133  {
134  std::string _s;
135  if(!find(a_string,_s)) {a_value = a_def;return false;}
136  return to<aT>(_s,a_value,a_def);
137  }

◆ find() [4/4]

bool tools::args::find ( const std::string &  a_string,
bool &  a_value,
const bool &  a_def = false 
) const
inline

Definition at line 127 of file args.

127  {
128  std::string s;
129  if(!find(a_string,s)) {a_value = a_def;return false;}
130  return to(s,a_value,a_def);
131  }

◆ first_not_hyphen()

bool tools::args::first_not_hyphen ( std::string &  a_first,
bool  a_skip_first = false 
) const
inline

Definition at line 333 of file args.

333  {
334  std::vector<std::string> _ss;
335  not_hyphens(_ss,a_skip_first);
336  if(_ss.empty()) {a_first.clear();return false;}
337  a_first = _ss[0];
338  return true;
339  }

◆ get_args()

const std::vector<arg>& tools::args::get_args ( ) const
inline

Definition at line 89 of file args.

89 {return m_args;}

◆ insert_begin()

bool tools::args::insert_begin ( const std::string &  a_key,
const std::string &  a_value = std::string(),
bool  a_override = true 
)
inline

Definition at line 185 of file args.

185  {
186  if(a_override) {
187  tools_vforit(arg,m_args,it) {
188  if((*it).first==a_key) {
189  (*it).second = a_value;
190  return true;
191  }
192  }
193  }
194  if(a_key.empty()) return false;
195  m_args.insert(m_args.begin(),arg(a_key,a_value));
196  return true;
197  }

◆ is_arg()

bool tools::args::is_arg ( const std::string &  a_string) const
inline

Definition at line 92 of file args.

92  {
94  if((*it).first==a_string) return true;
95  }
96  return false;
97  }

◆ is_empty()

bool tools::args::is_empty ( ) const
inline

Definition at line 98 of file args.

98 {return m_args.size()?false:true;}

◆ isAnArgument()

bool tools::args::isAnArgument ( const std::string &  a_key) const
inline

Definition at line 453 of file args.

453 {return is_arg(a_key);}

◆ known_options() [1/2]

bool tools::args::known_options ( const std::string &  a_known) const
inline

Definition at line 398 of file args.

398  {
399  tools_vforcit(arg,m_args,it) {
400  if((*it).first.find('-')==0) { //find '-' at first pos.
401  if((*it).first!=a_known) return false; //one option not a_known.
402  }
403  }
404  return true; //all options are a_known.
405  }

◆ known_options() [2/2]

bool tools::args::known_options ( const std::vector< std::string > &  a_knowns) const
inline

Definition at line 382 of file args.

382  {
383  tools_vforcit(arg,m_args,it) {
384  if((*it).first.find('-')==0) { //find '-' at first pos.
385  bool found = false;
386  tools_vforcit(std::string,a_knowns,it2) {
387  if((*it).first==(*it2)) {
388  found = true;
389  break;
390  }
391  }
392  if(!found) return false; //one option not in a_knowns.
393  }
394  }
395  return true; //all options are in a_knowns.
396  }

◆ last()

bool tools::args::last ( std::string &  a_key,
std::string &  a_value 
) const
inline

Definition at line 264 of file args.

264  {
265  a_key.clear();
266  a_value.clear();
267  if(m_args.empty()) return false;
268  a_key = m_args.back().first;
269  a_value = m_args.back().second;
270  return true;
271  }

◆ not_hyphens()

void tools::args::not_hyphens ( std::vector< std::string > &  a_not_hyphens,
bool  a_skip_first = false 
) const
inline

Definition at line 311 of file args.

311  {
312  a_not_hyphens.clear();
313  // Get the serie of trailing args not beginning with '-'
314  // and without a value (not of the form [-]xxx=yyy).
315  // Note that an argument like that in between arguments
316  // is NOT taken into account.
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();
323  } else {
324  a_not_hyphens.push_back((*it).first);
325  }
326  }
327  }

◆ number()

size_t tools::args::number ( ) const
inline

Definition at line 100 of file args.

100 {return m_args.size();} //back comp.

◆ operator=()

args& tools::args::operator= ( const args a_from)
inline

Definition at line 84 of file args.

84  {
85  m_args = a_from.m_args;
86  return *this;
87  }

◆ prog_name()

bool tools::args::prog_name ( std::string &  a_value) const
inline

Definition at line 273 of file args.

273  {
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;
277  return true;
278  }

◆ remove()

int tools::args::remove ( const std::string &  a_key)
inline

Definition at line 249 of file args.

249  {
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) {
253  it = m_args.erase(it);
254  } else {
255  ++it;
256  }
257  }
258  return int(nbeg) - int(m_args.size());
259  }

◆ remove_first()

void tools::args::remove_first ( )
inline

Definition at line 261 of file args.

261 {if(m_args.size()) m_args.erase(m_args.begin());}

◆ remove_last()

void tools::args::remove_last ( )
inline

Definition at line 262 of file args.

262 {if(m_args.size()) m_args.erase(m_args.end()-1);}

◆ size()

size_t tools::args::size ( ) const
inline

Definition at line 99 of file args.

99 {return m_args.size();}

◆ to_vector()

void tools::args::to_vector ( std::vector< std::string > &  a_vec) const
inline

Definition at line 157 of file args.

157  {
158  // Return a vector of string <name=value>
159  a_vec.clear();
160  std::string s;
161  tools_vforcit(arg,m_args,it) {
162  s = (*it).first;
163  if((*it).second.size()) {
164  s += "=";
165  s += (*it).second;
166  }
167  a_vec.push_back(s);
168  }
169  }

Member Data Documentation

◆ m_args

std::vector<arg> tools::args::m_args
protected

Definition at line 455 of file args.


The documentation for this class was generated from the following file:
tools::args::is_arg
bool is_arg(const std::string &a_string) const
Definition: args:92
tools::args::not_hyphens
void not_hyphens(std::vector< std::string > &a_not_hyphens, bool a_skip_first=false) const
Definition: args:311
tools::sg::right
@ right
Definition: enums:76
tools::args::last
bool last(std::string &a_key, std::string &a_value) const
Definition: args:264
tools::args::arg
std::pair< std::string, std::string > arg
Definition: args:30
tools::args::find
bool find(const std::string &a_key, std::string &a_value, const std::string &a_def=std::string()) const
Definition: args:101
tools::sg::left
@ left
Definition: enums:74
tools::both
@ both
Definition: strip:12
tools::to
std::vector< std::string > to(int a_argc, char **a_argv)
Definition: args:507
tools_vforit
#define tools_vforit(a__T, a__v, a__it)
Definition: forit:13
tools::words
void words(const std::string &a_string, const std::string &a_sep, bool a_take_empty, std::vector< std::string > &a_words, bool a_clear=true)
Definition: words:12
tools::file::found
bool found(const std::string &a_file, const std::string &a_what, std::vector< std::string > &a_found)
Definition: file:507
tools::args::add
bool add(const std::string &a_key, const std::string &a_value=std::string(), bool a_override=true)
Definition: args:171
tools::args::m_args
std::vector< arg > m_args
Definition: args:455
tools::strip
bool strip(std::string &a_string, what a_type=both, char a_char=' ')
Definition: strip:14
tools_vforcit
#define tools_vforcit(a__T, a__v, a__it)
Definition: forit:7