libassa  3.5.1
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Private Attributes | List of all members
ASSA::CmdLineOpts Class Reference

Class CmdLineOpts. More...

#include <CmdLineOpts.h>

Inheritance diagram for ASSA::CmdLineOpts:
ASSA::GenServer

Public Types

typedef void(* OPTS_FUNC) (void)
 
typedef void(* OPTS_FUNC_ONE) (const string &)
 
typedef vector< OptionOptionSet
 

Public Member Functions

 CmdLineOpts ()
 Default constructor. More...
 
virtual ~CmdLineOpts ()
 Do-nothing destructor. More...
 
bool add_flag_opt (const char c, const string &s, bool *f)
 Add binary flag option. More...
 
bool add_opt (const char c, const string &s, string *str)
 Add an option with STL string argument. More...
 
bool add_opt (const char c, const string &s, int *i)
 Add an option with integer argument. More...
 
bool add_opt (const char c, const string &s, unsigned int *ui)
 Add an option with unsigned integer argument. More...
 
bool add_opt (const char c, const string &s, long *l)
 Add an option with long argument. More...
 
bool add_opt (const char c, const string &s, unsigned long *ul)
 Add an option with unsigned long argument. More...
 
bool add_opt (const char c, const string &s, double *d)
 Add an option with double argument. More...
 
bool add_opt (const char c, const string &s, float *f)
 Add an option with float argument. More...
 
bool add_opt (const char c_, const string &s_, OPTS_FUNC f_)
 Add an option with static function argument. More...
 
bool add_opt (const char c_, const string &s_, OPTS_FUNC_ONE f_)
 Add an option with static function argument. More...
 
bool rm_opt (const char c_, const string &s_)
 Remove option for the option list. More...
 
bool parse_args (const char *argv[])
 Parse command line arguments based on installed options set. More...
 
int parse_config_file (IniFile &inifile_)
 Parse configuration parameters found in [options] section of the INI file. More...
 
const char * get_opt_error () const
 If previous call to one of member functions returned false, retrieve detailed error message. More...
 
void dump () const
 Write options set to the log file. More...
 

Static Public Member Functions

static void str_to_argv (const string &src_, int &argc_, char **&argv_)
 Static function. More...
 
static void free_argv (char **&argv_)
 Free up memory allocated by str_to_argv() function
More...
 

Protected Member Functions

bool is_valid (const char sopt_, const string &lopt_)
 Detect if supplied option is valid. More...
 
void set_error_none ()
 Reset error message to an empty string. More...
 
bool assign (Option *node_, const char *op_)
 Perform value assignment to the node. More...
 
Optionfind_option (const char *str_)
 Locate option in the options set. More...
 
Optionfind_option (const char letter_)
 Locate option in the options set. More...
 
virtual void pos_arg (const char *arg_)
 Process positional argument arg_. More...
 

Private Attributes

OptionSet m_opts_set
 Options set. More...
 
string m_error
 Last reported error. More...
 

Detailed Description

Class CmdLineOpts.

CmdLineOpts class parsers the command line arguments. It is a base class, and to use it, it has to be inherited from. See "ASSA Library User's Guide" for further details.

Definition at line 113 of file CmdLineOpts.h.

Member Typedef Documentation

◆ OptionSet

Definition at line 119 of file CmdLineOpts.h.

◆ OPTS_FUNC

typedef void(* ASSA::CmdLineOpts::OPTS_FUNC) (void)

Definition at line 116 of file CmdLineOpts.h.

◆ OPTS_FUNC_ONE

typedef void(* ASSA::CmdLineOpts::OPTS_FUNC_ONE) (const string &)

Definition at line 117 of file CmdLineOpts.h.

Constructor & Destructor Documentation

◆ CmdLineOpts()

ASSA::CmdLineOpts::CmdLineOpts ( )
inline

Default constructor.

Definition at line 295 of file CmdLineOpts.h.

295  : m_opts_set (), m_error ("")
296 {
297  trace_with_mask("CmdLineOpts::CmdLineOpts", CMDLINEOPTS);
298  set_error_none ();
299 }
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition: Logger.h:437
string m_error
Last reported error.
Definition: CmdLineOpts.h:287
void set_error_none()
Reset error message to an empty string.
Definition: CmdLineOpts.h:302
OptionSet m_opts_set
Options set.
Definition: CmdLineOpts.h:284
@ CMDLINEOPTS
Class CmdLineOpts messages
Definition: LogMask.h:36

References ASSA::CMDLINEOPTS, set_error_none(), and trace_with_mask.

◆ ~CmdLineOpts()

virtual ASSA::CmdLineOpts::~CmdLineOpts ( )
inlinevirtual

Do-nothing destructor.

Definition at line 125 of file CmdLineOpts.h.

125  {
126  trace_with_mask ("CmdLineOpts::~CmdLineOpts", CMDLINEOPTS);
127  }

References ASSA::CMDLINEOPTS, and trace_with_mask.

Member Function Documentation

◆ add_flag_opt()

bool CmdLineOpts::add_flag_opt ( const char  c,
const string &  s,
bool *  f 
)

Add binary flag option.

Parameters
cshort name
slong name
fpointer to bool flag variable
Returns
true on success, false on error

Definition at line 205 of file CmdLineOpts.cpp.

207 {
208  trace_with_mask ("CmdLineOpts::add_flag_opt", CMDLINEOPTS);
209 
210  if (!is_valid (sopt_, lopt_))
211  return (false);
212 
213  Option o (sopt_, lopt_, Option::flag_t, (void*) v_);
214  m_opts_set.push_back (o);
215  return (true);
216 }
bool is_valid(const char sopt_, const string &lopt_)
Detect if supplied option is valid.
Option class.
Definition: CmdLineOpts.h:39
@ flag_t
No argument; bool value is flipped.
Definition: CmdLineOpts.h:55

References ASSA::CMDLINEOPTS, ASSA::Option::flag_t, is_valid(), m_opts_set, and trace_with_mask.

Referenced by ASSA::GenServer::GenServer().

◆ add_opt() [1/9]

bool CmdLineOpts::add_opt ( const char  c,
const string &  s,
double *  d 
)

Add an option with double argument.

Parameters
cshort name
slong name
dpointer to double variable
Returns
true on success, false on error

Definition at line 289 of file CmdLineOpts.cpp.

291 {
292  trace_with_mask ("CmdLineOpts::add_opt(double*)", CMDLINEOPTS);
293 
294  if (!is_valid (sopt_, lopt_)) {
295  return (false);
296  }
297  Option o (sopt_, lopt_, Option::double_t, (void*) v_);
298  m_opts_set.push_back (o);
299  return (true);
300 }
@ double_t
Convert argument to double
Definition: CmdLineOpts.h:53

References ASSA::CMDLINEOPTS, ASSA::Option::double_t, is_valid(), m_opts_set, and trace_with_mask.

◆ add_opt() [2/9]

bool CmdLineOpts::add_opt ( const char  c,
const string &  s,
float *  f 
)

Add an option with float argument.

Parameters
cshort name
slong name
fpointer to float variable
Returns
true on success, false on error

Definition at line 303 of file CmdLineOpts.cpp.

305 {
306  trace_with_mask ("CmdLineOpts::add_opt(float*)", CMDLINEOPTS);
307 
308  if (!is_valid (sopt_, lopt_)) {
309  return (false);
310  }
311  Option o (sopt_, lopt_, Option::float_t, (void*) v_);
312  m_opts_set.push_back (o);
313  return (true);
314 }
@ float_t
Convert argument to float
Definition: CmdLineOpts.h:54

References ASSA::CMDLINEOPTS, ASSA::Option::float_t, is_valid(), m_opts_set, and trace_with_mask.

◆ add_opt() [3/9]

bool CmdLineOpts::add_opt ( const char  c,
const string &  s,
int *  i 
)

Add an option with integer argument.

Parameters
cshort name
slong name
ipointer to int variable
Returns
true on success, false on error

Definition at line 233 of file CmdLineOpts.cpp.

235 {
236  trace_with_mask ("CmdLineOpts::add_opt(int*)", CMDLINEOPTS);
237 
238  if (!is_valid (sopt_, lopt_)) {
239  return (false);
240  }
241  Option o (sopt_, lopt_, Option::int_t, (void*) v_);
242  m_opts_set.push_back (o);
243  return (true);
244 }
@ int_t
Convert argument to int
Definition: CmdLineOpts.h:49

References ASSA::CMDLINEOPTS, ASSA::Option::int_t, is_valid(), m_opts_set, and trace_with_mask.

◆ add_opt() [4/9]

bool CmdLineOpts::add_opt ( const char  c,
const string &  s,
long *  l 
)

Add an option with long argument.

Parameters
cshort name
slong name
lpointer to long variable
Returns
true on success, false on error

Definition at line 261 of file CmdLineOpts.cpp.

263 {
264  trace_with_mask ("CmdLineOpts::add_opt(long*)", CMDLINEOPTS);
265 
266  if (!is_valid (sopt_, lopt_)) {
267  return (false);
268  }
269  Option o (sopt_, lopt_, Option::long_t, (void*) v_);
270  m_opts_set.push_back (o);
271  return (true);
272 }
@ long_t
Convert argument to long
Definition: CmdLineOpts.h:51

References ASSA::CMDLINEOPTS, is_valid(), ASSA::Option::long_t, m_opts_set, and trace_with_mask.

◆ add_opt() [5/9]

bool CmdLineOpts::add_opt ( const char  c,
const string &  s,
string *  str 
)

Add an option with STL string argument.

Parameters
cshort name
slong name
strpointer to string variable
Returns
true on success, false on error

Definition at line 219 of file CmdLineOpts.cpp.

221 {
222  trace_with_mask ("CmdLineOpts::add_opt(string*)", CMDLINEOPTS);
223 
224  if (!is_valid (sopt_, lopt_))
225  return (false);
226 
227  Option o (sopt_, lopt_, Option::string_t, (void*) v_);
228  m_opts_set.push_back (o);
229  return (true);
230 }
@ string_t
Convert argument to STL string
Definition: CmdLineOpts.h:48

References ASSA::CMDLINEOPTS, is_valid(), m_opts_set, ASSA::Option::string_t, and trace_with_mask.

Referenced by ASSA::GenServer::GenServer().

◆ add_opt() [6/9]

bool CmdLineOpts::add_opt ( const char  c,
const string &  s,
unsigned int *  ui 
)

Add an option with unsigned integer argument.

Parameters
cshort name
slong name
uipointer to u_int variable
Returns
true on success, false on error

Definition at line 247 of file CmdLineOpts.cpp.

249 {
250  trace_with_mask ("CmdLineOpts::add_opt(u_int*)", CMDLINEOPTS);
251 
252  if (!is_valid (sopt_, lopt_)) {
253  return (false);
254  }
255  Option o (sopt_, lopt_, Option::uint_t, (void*) v_);
256  m_opts_set.push_back (o);
257  return (true);
258 }
@ uint_t
Convert argument to unsigned int
Definition: CmdLineOpts.h:50

References ASSA::CMDLINEOPTS, is_valid(), m_opts_set, trace_with_mask, and ASSA::Option::uint_t.

◆ add_opt() [7/9]

bool CmdLineOpts::add_opt ( const char  c,
const string &  s,
unsigned long *  ul 
)

Add an option with unsigned long argument.

Parameters
cshort name
slong name
ulpointer to unsigned long variable
Returns
true on success, false on error

Definition at line 275 of file CmdLineOpts.cpp.

277 {
278  trace_with_mask ("CmdLineOpts::add_opt(u_long*)", CMDLINEOPTS);
279 
280  if (!is_valid (sopt_, lopt_)) {
281  return (false);
282  }
283  Option o (sopt_, lopt_, Option::long_t, (void*) v_);
284  m_opts_set.push_back (o);
285  return (true);
286 }

References ASSA::CMDLINEOPTS, is_valid(), ASSA::Option::long_t, m_opts_set, and trace_with_mask.

◆ add_opt() [8/9]

bool CmdLineOpts::add_opt ( const char  c_,
const string &  s_,
OPTS_FUNC  f_ 
)

Add an option with static function argument.

This void function with no arguments will be called when command line option is processed. An option installed is treated as binary flag option.

Parameters
c_short name
s_long name
f_pointer to the static function
Returns
true on success, false on error

Definition at line 317 of file CmdLineOpts.cpp.

319 {
320  trace_with_mask ("CmdLineOpts::add_opt(OPTS_FUNC)", CMDLINEOPTS);
321 
322  if (!is_valid (sopt_, lopt_)) {
323  return (false);
324  }
325  Option o (sopt_, lopt_, Option::func_t, (void*) v_);
326  m_opts_set.push_back (o);
327  return (true);
328 }
@ func_t
Convert argument to function
Definition: CmdLineOpts.h:56

References ASSA::CMDLINEOPTS, ASSA::Option::func_t, is_valid(), m_opts_set, and trace_with_mask.

◆ add_opt() [9/9]

bool CmdLineOpts::add_opt ( const char  c_,
const string &  s_,
OPTS_FUNC_ONE  f_ 
)

Add an option with static function argument.

This void function with STL string arguments will be called when command line option is processed. The option value is delivered via function's argument.

Parameters
c_short name
s_long name
f_pointer to the static function
Returns
true on success, false on error

Definition at line 331 of file CmdLineOpts.cpp.

333 {
334  trace_with_mask ("CmdLineOpts::add_opt(OPTS_FUNC_ONE)", CMDLINEOPTS);
335 
336  if (!is_valid (sopt_, lopt_)) {
337  return (false);
338  }
339  Option o (sopt_, lopt_, Option::func_one_t, (void*) v_);
340  m_opts_set.push_back (o);
341  return (true);
342 }
@ func_one_t
Convert argument to function with one argument
Definition: CmdLineOpts.h:57

References ASSA::CMDLINEOPTS, ASSA::Option::func_one_t, is_valid(), m_opts_set, and trace_with_mask.

◆ assign()

bool CmdLineOpts::assign ( Option node_,
const char *  op_ 
)
protected

Perform value assignment to the node.

Data conversion happens here.

Definition at line 535 of file CmdLineOpts.cpp.

537 {
538  trace_with_mask ("CmdLineOpts::assign", CMDLINEOPTS);
539 
540  long l;
541  double d;
542 
543  if (node_ && op_) {
544  DL ((CMDLINEOPTS, "Assign '%s' to {-%c, --%s, t=%s}\n",
545  op_, node_->m_short_name, node_->m_long_name.c_str (),
546  node_->type_c_str ()));
547  }
548 
549  /*---
550  From strtol(3C) man page:
551 
552  "Because 0 is returned on error and is also a valid return on
553  success, an application wishing to check for error situations
554  should set 'errno' to 0, then call strtol(3C), then check 'errno'
555  and if it is non-zero, assume an error has occured."
556  ---*/
557 
558  switch (node_->m_type) {
559  case Option::string_t:
560  *(string*) node_->m_val = op_;
561  break;
562 
563  case Option::int_t:
564  case Option::long_t:
565  errno = 0;
566  l = strtol (op_, NULL, 0);
567 
568  if (errno != 0) {
569  m_error = "Error: '" + string (strerror (errno)) + "',";
570  m_error += " in converting to integer from '";
571  m_error += string (op_) + "'.";
572  return (false);
573  }
574 
575  if (node_->m_type == Option::int_t) {
576  *(int*) node_->m_val = int (l);
577  }
578  else {
579  *(long*) node_->m_val = l;
580  }
581  break;
582 
583  case Option::uint_t:
584  case Option::ulong_t:
585  errno = 0;
586  l = strtol (op_, NULL, 0);
587 
588  if (errno != 0) {
589  m_error = "Error: '" + string (strerror (errno)) + "',";
590  m_error += " in converting to unsigned integer from '";
591  m_error += string (op_) + "'.";
592  return (false);
593  }
594 
595  if (node_->m_type == Option::uint_t) {
596  *(unsigned int*) node_->m_val = int (l);
597  }
598  else {
599  *(unsigned long*) node_->m_val = l;
600  }
601  break;
602 
603  case Option::double_t:
604  case Option::float_t:
605  errno = 0;
606  d = strtod (op_, NULL);
607 
608  if (errno != 0) {
609  m_error = "Error: '" + string (strerror (errno)) + "',";
610  m_error += " in converting to double/float from '";
611  m_error += string (op_) + "'.";
612  return (false);
613  }
614 
615  if (node_->m_type == Option::double_t) {
616  *(double*) node_->m_val = d;
617  }
618  else {
619  *(float*) node_->m_val = float (d);
620  }
621  break;
622 
623  case Option::flag_t:
624  *(bool*) node_->m_val = true; // no more flipping!
625  break;
626 
627  case Option::func_t:
628  (*(OPTS_FUNC)(node_->m_val)) ();
629  break;
630 
631  case Option::func_one_t:
632  (*(OPTS_FUNC_ONE)(node_->m_val)) (op_);
633  break;
634 
635  case Option::none_t:
636  default:
637  m_error = "Undefined type for option '"+string (op_)+"'.";
638  return (false);
639  } /*-- switch () --*/
640 
641  return (true);
642 }
#define DL(X)
A macro for writing debug message to the Logger.
Definition: Logger.h:273
void(* OPTS_FUNC)(void)
Definition: CmdLineOpts.h:116
void(* OPTS_FUNC_ONE)(const string &)
Definition: CmdLineOpts.h:117
@ ulong_t
Convert argument to unsigned long
Definition: CmdLineOpts.h:52
type_t m_type
Option type.
Definition: CmdLineOpts.h:82
string m_long_name
Long option name.
Definition: CmdLineOpts.h:79
void * m_val
Pointer to the option value.
Definition: CmdLineOpts.h:85
const char * type_c_str()
Return the type of the Option object.
char m_short_name
One-letter option name.
Definition: CmdLineOpts.h:76

References ASSA::CMDLINEOPTS, DL, ASSA::Option::double_t, ASSA::Option::flag_t, ASSA::Option::float_t, ASSA::Option::func_one_t, ASSA::Option::func_t, ASSA::Option::int_t, ASSA::Option::long_t, m_error, ASSA::Option::m_long_name, ASSA::Option::m_short_name, ASSA::Option::m_type, ASSA::Option::m_val, ASSA::Option::none_t, ASSA::Option::string_t, trace_with_mask, ASSA::Option::type_c_str(), ASSA::Option::uint_t, and ASSA::Option::ulong_t.

Referenced by parse_args(), and parse_config_file().

◆ dump()

void CmdLineOpts::dump ( ) const

Write options set to the log file.

Definition at line 645 of file CmdLineOpts.cpp.

647 {
648  OptionSet::const_iterator i;
649 
650  for (i = m_opts_set.begin (); i != m_opts_set.end (); i++) {
651  i->dump ();
652  }
653 
654  if (!m_error.empty ()) {
655  DL((CMDLINEOPTS, "Last error: '%s'\n", m_error.c_str ()));
656  }
657 }

References ASSA::CMDLINEOPTS, DL, m_error, and m_opts_set.

Referenced by ASSA::GenServer::init_internals().

◆ find_option() [1/2]

Option * CmdLineOpts::find_option ( const char *  str_)
protected

Locate option in the options set.

Definition at line 172 of file CmdLineOpts.cpp.

174 {
175  trace_with_mask ("CmdLineOpts::find_option(char*)", CMDLINEOPTS);
176 
177  OptionSet::iterator i;
178 
179  for ( i = m_opts_set.begin (); i != m_opts_set.end (); i++)
180  {
181  if (i->m_long_name == str_) {
182  return &(*i);
183  }
184  }
185  return (NULL);
186 }

References ASSA::CMDLINEOPTS, m_opts_set, and trace_with_mask.

Referenced by parse_args().

◆ find_option() [2/2]

Option * CmdLineOpts::find_option ( const char  letter_)
protected

Locate option in the options set.

Definition at line 189 of file CmdLineOpts.cpp.

191 {
192  trace_with_mask ("CmdLineOpts::find_option(char)", CMDLINEOPTS);
193 
194  OptionSet::iterator i;
195 
196  for (i = m_opts_set.begin (); i != m_opts_set.end (); i++)
197  {
198  if (i->m_short_name == letter_)
199  return &(*i);
200  }
201  return (NULL);
202 }

References ASSA::CMDLINEOPTS, m_opts_set, and trace_with_mask.

◆ free_argv()

void CmdLineOpts::free_argv ( char **&  argv_)
static

Free up memory allocated by str_to_argv() function

Definition at line 692 of file CmdLineOpts.cpp.

694 {
695  trace_with_mask ("CmdLineOpts::free_argv", CMDLINEOPTS);
696 
697  /* If argument is empty (which should never be the case),
698  * then freeing the memory would core dump application.
699  */
700  if (argv_ == NULL) {
701  return;
702  }
703 
704  for (int i = 0; argv_[i]; i++) {
705  delete [] argv_[i];
706  }
707  delete [] argv_;
708  argv_ = NULL;
709 }

References ASSA::CMDLINEOPTS, and trace_with_mask.

◆ get_opt_error()

const char * ASSA::CmdLineOpts::get_opt_error ( ) const
inline

If previous call to one of member functions returned false, retrieve detailed error message.

Definition at line 309 of file CmdLineOpts.h.

310 {
311  return (m_error.c_str ());
312 }

References m_error.

Referenced by ASSA::GenServer::init().

◆ is_valid()

bool CmdLineOpts::is_valid ( const char  sopt_,
const string &  lopt_ 
)
protected

Detect if supplied option is valid.

Definition at line 144 of file CmdLineOpts.cpp.

146 {
147  trace_with_mask ("CmdLineOpts::is_valid", CMDLINEOPTS);
148 
149  set_error_none ();
150  OptionSet::const_iterator i;
151 
152  for (i = m_opts_set.begin (); i != m_opts_set.end (); i++) {
153  if (sopt_ == '\0' && lopt_.empty ()) {
154  m_error = "Ignore empty option";
155  return (false);
156  }
157  else if (sopt_ != '\0' && i->m_short_name == sopt_) {
158  m_error = "Ignored multiple option '-";
159  m_error += sopt_ + string ("'");
160  return (false);
161  }
162  else if (!lopt_.empty () && i->m_long_name == lopt_) {
163  m_error = "Ignore multiple option '--";
164  m_error += lopt_ + string ("'");
165  return (false);
166  }
167  }
168  return (true);
169 }

References ASSA::CMDLINEOPTS, m_error, m_opts_set, set_error_none(), and trace_with_mask.

Referenced by add_flag_opt(), and add_opt().

◆ parse_args()

bool CmdLineOpts::parse_args ( const char *  argv[])

Parse command line arguments based on installed options set.

Returns
true on success, false on error.

Definition at line 364 of file CmdLineOpts.cpp.

366 {
367  trace_with_mask ("CmdLineOpts::parse_args", CMDLINEOPTS);
368 
369  register int skip = 1;
370  bool pos_args_started = false;
371  string param ("");
372  string token ("");
373  set_error_none ();
374  Option* node = (Option*) NULL;
375 
376  for (argv_++; argv_[0]; argv_ += skip) {
377  if (skip != 0) {
378  token = argv_[0];
379  }
380 
381  DL((CMDLINEOPTS, "token: \"%s\"\n", token.c_str()));
382 
383  if (pos_args_started) {
384  DL((CMDLINEOPTS,"pos_args_started = true\n"));
385 
386  if (token[0] == '-' && token.size () != 1) {
387  m_error = "Invalid order of arguments: '";
388  m_error += token + "'.";
389  goto done;
390  }
391  pos_arg (token.c_str ());
392  continue;
393  }
394  skip = 1;
395 
396  if (token[0] == '-' && token.size () > 1 && token[1] != '-') {
397  if (token.size () == 1 && !pos_args_started) {
398  pos_arg (token.c_str ());
399  pos_args_started = true;
400  continue;
401  }
402 
403  if ((node = find_option (token[1])) != NULL) {
404  if (token.size () > 2) {
405  if (node->m_type == Option::flag_t ||
406  node->m_type == Option::func_t)
407  {
408  token.erase (1, 1);
409  skip = 0;
410  }
411  else {
412  param = token.substr (2);
413  }
414  } // if (token.size()>2)
415  } // if ((node = find_option ())
416  }
417  else {
418  if (token.size () > 1 && token[1] == '-') {
419  string op = token.substr (2);
420  size_t pos;
421 
422  if ((pos = op.find ("=")) != (size_t)-1) {
423  param = op.substr (pos+1, op.length ());
424  op.replace (pos, op.length() - pos, "");
425  }
426  node = find_option (op.c_str ());
427  }
428  else {
429  pos_arg (token.c_str ());
430  pos_args_started = true;
431  continue;
432  }
433  } // if (token[0] == '-' && token[1] != '-')
434 
435  if (!node) {
436  m_error = "Invalid option '" + token + "'.";
437  goto done;
438  }
439 
440  if (node->m_type != Option::flag_t &&
441  node->m_type != Option::func_t)
442  {
443  if (param.empty ()) {
444  if (!argv_[1]) {
445  m_error = "Expecting parameter after '"
446  + string (argv_[0]) + "'.";
447  goto done;
448  }
449  param = argv_[1];
450  skip = 2;
451  }
452  }
453  /*---
454  * if positional arguments only
455  ---*/
456  if (!node) {
457  goto done;
458  }
459 
460  if (param.empty ()) {
461  if (!assign (node, argv_[1])) {
462  return (false);
463  }
464  }
465  else {
466  const char* str = param.c_str ();
467  if (!assign (node, str)) {
468  return (false);
469  }
470  param = "";
471  }
472  } // for (argv_++; argv_[0]; argv_ += skip)
473 
474  done:
475  return !m_error.empty () ? false : true;
476 }
Option * find_option(const char *str_)
Locate option in the options set.
bool assign(Option *node_, const char *op_)
Perform value assignment to the node.
virtual void pos_arg(const char *arg_)
Process positional argument arg_.
Definition: CmdLineOpts.h:292

References assign(), ASSA::CMDLINEOPTS, DL, find_option(), ASSA::Option::flag_t, ASSA::Option::func_t, m_error, ASSA::Option::m_type, pos_arg(), set_error_none(), and trace_with_mask.

Referenced by ASSA::GenServer::init().

◆ parse_config_file()

int CmdLineOpts::parse_config_file ( IniFile inifile_)

Parse configuration parameters found in [options] section of the INI file.

An Option object keeps long names the way they were assigned by the add_opt() call (i.e.

File should be already loaded with load().

Parameters
inifile_The INI file to parse.
Returns
The number of options parsed or -1 if [options] section is missing

"mac-address"). IniFile has same options in their normalized format (i.e. "mac_address").

Check to see if [options] section is present. Try all possible matches of the section name.

Iterate throught the set of all registered Options. For each in the set, normalize its name and lookup in IniFile's [option] section for the value.

Definition at line 491 of file CmdLineOpts.cpp.

493 {
494  trace_with_mask ("CmdLineOpts::parse_config_file", CMDLINEOPTS);
495 
496  unsigned int count = 0;
497  string v;
498  string s;
499  string optsect_name ("options");
500  OptionSet::iterator pos = m_opts_set.begin ();
501 
502  if (inifile_.find_section (optsect_name) == inifile_.sect_end ())
503  {
504  optsect_name = "Options";
505  if (inifile_.find_section (optsect_name) == inifile_.sect_end ())
506  {
507  optsect_name = "OPTIONS";
508  if (inifile_.find_section (optsect_name) == inifile_.sect_end ())
509  {
510  m_error = "Missing [options] section in INI file!";
511  return -1;
512  }
513  }
514  }
515 
516  while (pos != m_opts_set.end ()) {
517  if (pos->m_long_name.size ()) {
518  s = pos->m_long_name;
520  DL ((CMDLINEOPTS, "trying option \"%s\"\n", s.c_str ()));
521  v = inifile_.get_value (optsect_name, s);
522  if (v.size ()) {
523  if (assign (&(*pos), v.c_str ())) {
524  count++;
525  }
526  }
527  }
528  pos++;
529  }
530 
531  return (count);
532 }
config_iterator find_section(const string &section_)
Find section by its name.
config_iterator sect_end()
Return iterator past the last section.
Definition: IniFile.h:174
string get_value(const string &section_, const string &name_) const
Find and return a value of the name/value pair in the section section_.
void find_and_replace_char(std::string &text_, char src_, char dest_)
Find and relpace all instances of src_ character with dest_ character in a string text_.

References assign(), ASSA::CMDLINEOPTS, DL, ASSA::Utils::find_and_replace_char(), ASSA::IniFile::find_section(), ASSA::IniFile::get_value(), m_error, m_opts_set, ASSA::IniFile::sect_end(), and trace_with_mask.

◆ pos_arg()

void ASSA::CmdLineOpts::pos_arg ( const char *  arg_)
inlineprotectedvirtual

Process positional argument arg_.

This method must be overloaded by the derived class to take advantage of it.

Parameters
arg_positional argument value

Definition at line 292 of file CmdLineOpts.h.

292 { /* no-opt*/ }

Referenced by parse_args().

◆ rm_opt()

bool CmdLineOpts::rm_opt ( const char  c_,
const string &  s_ 
)

Remove option for the option list.

Parameters
c_short name
s_long name
Returns
true if found, false if not

Definition at line 345 of file CmdLineOpts.cpp.

347 {
348  trace_with_mask ("CmdLineOpts::rm_opt(string&)", CMDLINEOPTS);
349 
350  OptionSet::iterator i;
351 
352  for (i = m_opts_set.begin (); i != m_opts_set.end (); i++)
353  {
354  if (i->m_short_name == sopt_ || i->m_long_name == lopt_)
355  {
356  m_opts_set.erase (i);
357  return (true);
358  }
359  }
360  return (false);
361 }

References ASSA::CMDLINEOPTS, m_opts_set, and trace_with_mask.

◆ set_error_none()

void ASSA::CmdLineOpts::set_error_none ( )
inlineprotected

Reset error message to an empty string.

Definition at line 302 of file CmdLineOpts.h.

303 {
304  trace_with_mask("CmdLineOpts::set_error_none", CMDLINEOPTS);
305  m_error = "";
306 }

References ASSA::CMDLINEOPTS, m_error, and trace_with_mask.

Referenced by CmdLineOpts(), is_valid(), and parse_args().

◆ str_to_argv()

void CmdLineOpts::str_to_argv ( const string &  src_,
int &  argc_,
char **&  argv_ 
)
static

Static function.

Convert string list of command line options into dynamically allocated argv-like array. The array is terminated with NULL. This memory must be freed after it has been used. Remember that the first parameter is process name.

Parameters
src_command line option string
argc_number of options found in the source string
argv_returns a pointer to the heap-allocated memory

Definition at line 660 of file CmdLineOpts.cpp.

662 {
663  trace_with_mask ("CmdLineOpts::str_to_argv", CMDLINEOPTS);
664 
665  std::vector<string> vs;
666  std::istringstream input (src_);
667  std::string token;
668 
669  while (input >> token) {
670  vs.push_back (token);
671  token = "";
672  }
673  int i = 0;
674  char* p;
675 
676  if (vs.size ()) {
677  argv_ = new char* [vs.size() + 1];
678  std::vector<string>::iterator it;
679 
680  for (it = vs.begin (); it != vs.end (); it++, i++) {
681  p = new char [it->size() + 1];
682  strcpy (p, it->c_str ());
683  p[it->size()] = '\0';
684  argv_[i] = p;
685  }
686  argv_[i] = NULL;
687  }
688  argc_ = i;
689 }

References ASSA::CMDLINEOPTS, and trace_with_mask.

Referenced by ASSA::Fork::fork_exec().

Member Data Documentation

◆ m_error

string ASSA::CmdLineOpts::m_error
private

Last reported error.

Definition at line 287 of file CmdLineOpts.h.

Referenced by assign(), dump(), get_opt_error(), is_valid(), parse_args(), parse_config_file(), and set_error_none().

◆ m_opts_set

OptionSet ASSA::CmdLineOpts::m_opts_set
private

Options set.

Definition at line 284 of file CmdLineOpts.h.

Referenced by add_flag_opt(), add_opt(), dump(), find_option(), is_valid(), parse_config_file(), and rm_opt().


The documentation for this class was generated from the following files: