56 static void removeArg(
int& argc,
char* argv[],
int pos,
int n)
58 for ( ; pos < argc - n; ++pos)
59 argv[pos] = argv[pos + n];
68 class ArgBaseT :
public ArgBase
71 explicit ArgBaseT(
const T& def)
76 bool extract(
const char* str,
int& argc,
char* argv[],
int i,
int n)
78 std::istringstream s(str);
83 removeArg(argc, argv, i, n);
101 class ArgBaseT<const char*> :
public ArgBase
104 explicit ArgBaseT(
const char* def)
109 bool extract(
const char* str,
int& argc,
char* argv[],
int i,
int n)
113 removeArg(argc, argv, i, n);
119 const char*
get()
const
128 class ArgBaseT<std::string> :
public ArgBase
131 explicit ArgBaseT(
const std::string& def)
136 bool extract(
const char* str,
int& argc,
char* argv[],
int i,
int n)
140 removeArg(argc, argv, i, n);
146 const std::string&
get()
const
198 template <
typename T>
199 class Arg :
public ArgBaseT<T>
221 Arg(
int& argc,
char* argv[],
char ch,
const T& def = T())
238 Arg(
int& argc,
char* argv[],
const char* str,
const T& def = T())
241 this->m_isset =
set(argc, argv, str);
246 Arg(
int& argc,
char* argv[])
249 this->m_isset =
set(argc, argv);
265 bool set(
int& argc,
char* argv[],
char ch)
271 for(
int i = 1; i < argc; ++i)
273 if (argv[i] && argv[i][0] ==
'-' && argv[i][1] == ch)
275 if(argv[i][2] ==
'\0' && i < argc - 1)
278 if (this->extract(argv[i + 1], argc, argv, i, 2))
283 if( this->extract(argv[i] + 2, argc, argv, i, 1) )
304 bool set(
int& argc,
char* argv[],
const char* str)
310 unsigned n = std::strlen(str);
311 for (
int i = 1; i < argc; ++i)
313 if(argv[i] && (std::strncmp(argv[i], str, n) == 0))
315 if (i < argc - 1 && argv[i][n] ==
'\0')
318 if (this->extract(argv[i + 1], argc, argv, i, 2))
322 if (argv[i][n] ==
'=')
325 if (this->extract(argv[i] + n + 1, argc, argv, i, 1))
335 bool set(
int& argc,
char* argv[])
342 this->extract(argv[1], argc, argv, 1, 1);
344 return this->m_isset;
350 class Arg<bool> :
public ArgBase
353 Arg(
bool def =
false)
357 Arg(
int& argc,
char* argv[],
char ch,
bool def =
false)
360 m_isset =
set(argc, argv, ch);
363 Arg(
int& argc,
char* argv[],
const char* str,
bool def =
false)
366 m_isset =
set(argc, argv, str);
369 bool set(
int& argc,
char* argv[],
char ch)
375 for (
int i = 1; i < argc; ++i)
377 if (argv[i][0] ==
'-' && argv[i][1] !=
'-')
380 if (argv[i][1] == ch && argv[i][2] ==
'\0')
385 removeArg(argc, argv, i, 1);
388 else if(argv[i][1] == ch &&
395 removeArg(argc, argv, i, 1);
401 for (
char* p = argv[i] + 1; *p !=
'\0'; ++p)
412 while (*p++ !=
'\0');
424 bool set(
int& argc,
char* argv[],
const char* str)
430 for (
int i = 1; i < argc; ++i)
432 if (std::strcmp(argv[i], str) == 0)
436 removeArg(argc, argv, i, 1);
447 operator bool()
const
458 template <
typename T>
459 inline std::ostream& operator<<(std::ostream& out, const Arg<T>& arg)
461 return out << arg.get();
Arg(int &argc, char *argv[], const char *str, const T &def=T())
Extract long option.
Definition: Arg.h:238
Arg(int &argc, char *argv[], char ch, const T &def=T())
Extract short option.
Definition: Arg.h:221
Arg(int &argc, char *argv[])
Extracts the next parameter.
Definition: Arg.h:246
Arg(const T &def=T())
Constructor with initial value.
Definition: Arg.h:204
bool set(int &argc, char *argv[])
Reads next parameter and removes it.
Definition: Arg.h:335
Read and extract command-line options.
Definition: Arg.h:199
bool set(int &argc, char *argv[], char ch)
Extract short option.
Definition: Arg.h:265
bool set(int &argc, char *argv[], const char *str)
Extract long option.
Definition: Arg.h:304