Loading...
Searching...
No Matches
ASSA::CmdLineOpts Class Reference

Class CmdLineOpts. More...

`#include <CmdLineOpts.h>`

Inheritance diagram for ASSA::CmdLineOpts:

## Public Types

typedef void(* | OPTS_FUNC) (void)
typedef void(* | OPTS_FUNC_ONE) (const string &)
typedef vector< Option > | OptionSet

## Public Member Functions

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

## Static Public Member Functions

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

## Protected Member Functions

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

## Private Attributes

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

## 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

typedef vector<Option> ASSA::CmdLineOpts::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.

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

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

## ~CmdLineOpts()

virtual ASSA::CmdLineOpts::~CmdLineOpts | ( |  | ) |

inline virtual

Do-nothing destructor.

Definition at line 125 of file CmdLineOpts.h.

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

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

  c | short name
  s | long name
  f | pointer to bool flag variable

Returns
  true on success, false on error

Definition at line 205 of file CmdLineOpts.cpp.

{
trace_with_mask ("CmdLineOpts::add_flag_opt", CMDLINEOPTS);
if (!is_valid (sopt_, lopt_))
return (false);
Option o (sopt_, lopt_, Option::flag_t, (void*) v_);
m_opts_set.push_back (o);
return (true);
}
ASSA::CmdLineOpts::is_valid
bool is_valid(const char sopt_, const string &lopt_)
Detect if supplied option is valid.
Definition CmdLineOpts.cpp:145
ASSA::Option::flag_t
@ 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

  c | short name
  s | long name
  d | pointer to double variable

Returns
  true on success, false on error

Definition at line 289 of file CmdLineOpts.cpp.

{
trace_with_mask ("CmdLineOpts::add_opt(double*)", CMDLINEOPTS);
if (!is_valid (sopt_, lopt_)) {
return (false);
}
Option o (sopt_, lopt_, Option::double_t, (void*) v_);
m_opts_set.push_back (o);
return (true);
}
ASSA::Option::double_t
@ 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

  c | short name
  s | long name
  f | pointer to float variable

Returns
  true on success, false on error

Definition at line 303 of file CmdLineOpts.cpp.

{
trace_with_mask ("CmdLineOpts::add_opt(float*)", CMDLINEOPTS);
if (!is_valid (sopt_, lopt_)) {
return (false);
}
Option o (sopt_, lopt_, Option::float_t, (void*) v_);
m_opts_set.push_back (o);
return (true);
}
ASSA::Option::float_t
@ 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

  c | short name
  s | long name
  i | pointer to int variable

Returns
  true on success, false on error

Definition at line 233 of file CmdLineOpts.cpp.

{
trace_with_mask ("CmdLineOpts::add_opt(int*)", CMDLINEOPTS);
if (!is_valid (sopt_, lopt_)) {
return (false);
}
Option o (sopt_, lopt_, Option::int_t, (void*) v_);
m_opts_set.push_back (o);
return (true);
}
ASSA::Option::int_t
@ 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

  c | short name
  s | long name
  l | pointer to long variable

Returns
  true on success, false on error

Definition at line 261 of file CmdLineOpts.cpp.

{
trace_with_mask ("CmdLineOpts::add_opt(long*)", CMDLINEOPTS);
if (!is_valid (sopt_, lopt_)) {
return (false);
}
Option o (sopt_, lopt_, Option::long_t, (void*) v_);
m_opts_set.push_back (o);
return (true);
}
ASSA::Option::long_t
@ 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

  c | short name
  s | long name
  str | pointer to string variable

Returns
  true on success, false on error

Definition at line 219 of file CmdLineOpts.cpp.

{
trace_with_mask ("CmdLineOpts::add_opt(string*)", CMDLINEOPTS);
if (!is_valid (sopt_, lopt_))
return (false);
Option o (sopt_, lopt_, Option::string_t, (void*) v_);
m_opts_set.push_back (o);
return (true);
}
ASSA::Option::string_t
@ 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

  c | short name
  s | long name
  ui | pointer to u_int variable

Returns
  true on success, false on error

Definition at line 247 of file CmdLineOpts.cpp.

{
trace_with_mask ("CmdLineOpts::add_opt(u_int*)", CMDLINEOPTS);
if (!is_valid (sopt_, lopt_)) {
return (false);
}
Option o (sopt_, lopt_, Option::uint_t, (void*) v_);
m_opts_set.push_back (o);
return (true);
}
ASSA::Option::uint_t
@ 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

  c | short name
  s | long name
  ul | pointer to unsigned long variable

Returns
  true on success, false on error

Definition at line 275 of file CmdLineOpts.cpp.

{
trace_with_mask ("CmdLineOpts::add_opt(u_long*)", CMDLINEOPTS);
if (!is_valid (sopt_, lopt_)) {
return (false);
}
Option o (sopt_, lopt_, Option::long_t, (void*) v_);
m_opts_set.push_back (o);
return (true);
}

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.

{
trace_with_mask ("CmdLineOpts::add_opt(OPTS_FUNC)", CMDLINEOPTS);
if (!is_valid (sopt_, lopt_)) {
return (false);
}
Option o (sopt_, lopt_, Option::func_t, (void*) v_);
m_opts_set.push_back (o);
return (true);
}
ASSA::Option::func_t
@ 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.

{
trace_with_mask ("CmdLineOpts::add_opt(OPTS_FUNC_ONE)", CMDLINEOPTS);
if (!is_valid (sopt_, lopt_)) {
return (false);
}
Option o (sopt_, lopt_, Option::func_one_t, (void*) v_);
m_opts_set.push_back (o);
return (true);
}
ASSA::Option::func_one_t
@ 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.

{
trace_with_mask ("CmdLineOpts::assign", CMDLINEOPTS);
long l;
double d;
if (node_ && op_) {
DL ((CMDLINEOPTS, "Assign '%s' to {-%c, --%s, t=%s}\n",
op_, node_->m_short_name, node_->m_long_name.c_str (),
node_->type_c_str ()));
}
/*---
From strtol(3C) man page:
"Because 0 is returned on error and is also a valid return on
success, an application wishing to check for error situations
should set 'errno' to 0, then call strtol(3C), then check 'errno'
and if it is non-zero, assume an error has occured."
---*/
switch (node_->m_type) {
case Option::string_t:
*(string*) node_->m_val = op_;
break;
case Option::int_t:
case Option::long_t:
errno = 0;
l = strtol (op_, NULL, 0);
if (errno != 0) {
m_error = "Error: '" + string (strerror (errno)) + "',";
m_error += " in converting to integer from '";
m_error += string (op_) + "'.";
return (false);
}
if (node_->m_type == Option::int_t) {
*(int*) node_->m_val = int (l);
}
else {
*(long*) node_->m_val = l;
}
break;
case Option::uint_t:
case Option::ulong_t:
errno = 0;
l = strtol (op_, NULL, 0);
if (errno != 0) {
m_error = "Error: '" + string (strerror (errno)) + "',";
m_error += " in converting to unsigned integer from '";
m_error += string (op_) + "'.";
return (false);
}
if (node_->m_type == Option::uint_t) {
*(unsigned int*) node_->m_val = int (l);
}
else {
*(unsigned long*) node_->m_val = l;
}
break;
case Option::double_t:
case Option::float_t:
errno = 0;
d = strtod (op_, NULL);
if (errno != 0) {
m_error = "Error: '" + string (strerror (errno)) + "',";
m_error += " in converting to double/float from '";
m_error += string (op_) + "'.";
return (false);
}
if (node_->m_type == Option::double_t) {
*(double*) node_->m_val = d;
}
else {
*(float*) node_->m_val = float (d);
}
break;
case Option::flag_t:
*(bool*) node_->m_val = true; // no more flipping!
break;
case Option::func_t:
(*(OPTS_FUNC)(node_->m_val)) ();
break;
case Option::func_one_t:
(*(OPTS_FUNC_ONE)(node_->m_val)) (op_);
break;
case Option::none_t:
default:
m_error = "Undefined type for option '"+string (op_)+"'.";
return (false);
} /*-- switch () --*/
return (true);
}
DL
#define DL(X)
A macro for writing debug message to the Logger.
Definition Logger.h:273
ASSA::CmdLineOpts::OPTS_FUNC
void(* OPTS_FUNC)(void)
Definition CmdLineOpts.h:116
ASSA::CmdLineOpts::OPTS_FUNC_ONE
void(* OPTS_FUNC_ONE)(const string &)
Definition CmdLineOpts.h:117
ASSA::Option::ulong_t
@ ulong_t
Convert argument to unsigned long.
Definition CmdLineOpts.h:52
ASSA::Option::none_t
@ none_t
Definition CmdLineOpts.h:58
ASSA::Option::m_type
type_t m_type
Option type.
Definition CmdLineOpts.h:82
ASSA::Option::m_long_name
string m_long_name
Long option name.
Definition CmdLineOpts.h:79
ASSA::Option::m_val
void * m_val
Pointer to the option value.
Definition CmdLineOpts.h:85
ASSA::Option::type_c_str
const char * type_c_str()
Return the type of the Option object.
Definition CmdLineOpts.cpp:120
ASSA::Option::m_short_name
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.

{
OptionSet::const_iterator i;
for (i = m_opts_set.begin (); i != m_opts_set.end (); i++) {
i->dump ();
}
if (!m_error.empty ()) {
DL((CMDLINEOPTS, "Last error: '%s'\n", m_error.c_str ()));
}
}

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.

{
trace_with_mask ("CmdLineOpts::find_option(char*)", CMDLINEOPTS);
OptionSet::iterator i;
for ( i = m_opts_set.begin (); i != m_opts_set.end (); i++)
{
if (i->m_long_name == str_) {
return &(*i);
}
}
return (NULL);
}

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.

{
trace_with_mask ("CmdLineOpts::find_option(char)", CMDLINEOPTS);
OptionSet::iterator i;
for (i = m_opts_set.begin (); i != m_opts_set.end (); i++)
{
if (i->m_short_name == letter_)
return &(*i);
}
return (NULL);
}

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.

{
trace_with_mask ("CmdLineOpts::free_argv", CMDLINEOPTS);
/* If argument is empty (which should never be the case),
* then freeing the memory would core dump application.
*/
if (argv_ == NULL) {
return;
}
for (int i = 0; argv_[i]; i++) {
delete [] argv_[i];
}
delete [] argv_;
argv_ = NULL;
}

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.

{
return (m_error.c_str ());
}

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.

{
trace_with_mask ("CmdLineOpts::is_valid", CMDLINEOPTS);
set_error_none ();
OptionSet::const_iterator i;
for (i = m_opts_set.begin (); i != m_opts_set.end (); i++) {
if (sopt_ == '\0' && lopt_.empty ()) {
m_error = "Ignore empty option";
return (false);
}
else if (sopt_ != '\0' && i->m_short_name == sopt_) {
m_error = "Ignored multiple option '-";
m_error += sopt_ + string ("'");
return (false);
}
else if (!lopt_.empty () && i->m_long_name == lopt_) {
m_error = "Ignore multiple option '--";
m_error += lopt_ + string ("'");
return (false);
}
}
return (true);
}

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

Referenced by add_flag_opt(), add_opt(), add_opt(), add_opt(), add_opt(), add_opt(), add_opt(), add_opt(), add_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.

{
trace_with_mask ("CmdLineOpts::parse_args", CMDLINEOPTS);
register int skip = 1;
bool pos_args_started = false;
string param ("");
string token ("");
set_error_none ();
Option* node = (Option*) NULL;
for (argv_++; argv_[0]; argv_ += skip) {
if (skip != 0) {
token = argv_[0];
}
DL((CMDLINEOPTS, "token: \"%s\"\n", token.c_str()));
if (pos_args_started) {
DL((CMDLINEOPTS,"pos_args_started = true\n"));
if (token[0] == '-' && token.size () != 1) {
m_error = "Invalid order of arguments: '";
m_error += token + "'.";
goto done;
}
pos_arg (token.c_str ());
continue;
}
skip = 1;
if (token[0] == '-' && token.size () > 1 && token[1] != '-') {
if (token.size () == 1 && !pos_args_started) {
pos_arg (token.c_str ());
pos_args_started = true;
continue;
}
if ((node = find_option (token[1])) != NULL) {
if (token.size () > 2) {
if (node->m_type == Option::flag_t ||
node->m_type == Option::func_t)
{
token.erase (1, 1);
skip = 0;
}
else {
param = token.substr (2);
}
} // if (token.size()>2)
} // if ((node = find_option ())
}
else {
if (token.size () > 1 && token[1] == '-') {
string op = token.substr (2);
size_t pos;
if ((pos = op.find ("=")) != (size_t)-1) {
param = op.substr (pos+1, op.length ());
op.replace (pos, op.length() - pos, "");
}
node = find_option (op.c_str ());
}
else {
pos_arg (token.c_str ());
pos_args_started = true;
continue;
}
} // if (token[0] == '-' && token[1] != '-')
if (!node) {
m_error = "Invalid option '" + token + "'.";
goto done;
}
if (node->m_type != Option::flag_t &&
node->m_type != Option::func_t)
{
if (param.empty ()) {
if (!argv_[1]) {
m_error = "Expecting parameter after '"
+ string (argv_[0]) + "'.";
goto done;
}
param = argv_[1];
skip = 2;
}
}
/*---
* if positional arguments only
---*/
if (!node) {
goto done;
}
if (param.empty ()) {
if (!assign (node, argv_[1])) {
return (false);
}
}
else {
const char* str = param.c_str ();
if (!assign (node, str)) {
return (false);
}
param = "";
}
} // for (argv_++; argv_[0]; argv_ += skip)
done:
return !m_error.empty () ? false : true;
}
ASSA::CmdLineOpts::find_option
Option * find_option(const char *str_)
Locate option in the options set.
Definition CmdLineOpts.cpp:173
ASSA::CmdLineOpts::assign
bool assign(Option *node_, const char *op_)
Perform value assignment to the node.
Definition CmdLineOpts.cpp:536
ASSA::CmdLineOpts::pos_arg
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.

{
trace_with_mask ("CmdLineOpts::parse_config_file", CMDLINEOPTS);
unsigned int count = 0;
string v;
string s;
string optsect_name ("options");
OptionSet::iterator pos = m_opts_set.begin ();
if (inifile_.find_section (optsect_name) == inifile_.sect_end ())
{
optsect_name = "Options";
if (inifile_.find_section (optsect_name) == inifile_.sect_end ())
{
optsect_name = "OPTIONS";
if (inifile_.find_section (optsect_name) == inifile_.sect_end ())
{
m_error = "Missing [options] section in INI file!";
return -1;
}
}
}
while (pos != m_opts_set.end ()) {
if (pos->m_long_name.size ()) {
s = pos->m_long_name;
ASSA::Utils::find_and_replace_char (s, '-', '_');
DL ((CMDLINEOPTS, "trying option \"%s\"\n", s.c_str ()));
v = inifile_.get_value (optsect_name, s);
if (v.size ()) {
if (assign (&(*pos), v.c_str ())) {
count++;
}
}
}
pos++;
}
return (count);
}
ASSA::IniFile::find_section
config_iterator find_section(const string &section_)
Find section by its name.
Definition IniFile.cpp:199
ASSA::IniFile::get_value
string get_value(const string &section_, const string &name_) const
Find and return a value of the name/value pair in the section section_.
Definition IniFile.cpp:165
ASSA::IniFile::sect_end
config_iterator sect_end()
Return iterator past the last section.
Definition IniFile.h:174
ASSA::Utils::find_and_replace_char
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_.
Definition CommonUtils.cpp:110

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_ | ) |

inline protected virtual

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.

{ /* 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.

{
trace_with_mask ("CmdLineOpts::rm_opt(string&)", CMDLINEOPTS);
OptionSet::iterator i;
for (i = m_opts_set.begin (); i != m_opts_set.end (); i++)
{
if (i->m_short_name == sopt_ || i->m_long_name == lopt_)
{
m_opts_set.erase (i);
return (true);
}
}
return (false);
}

References ASSA::CMDLINEOPTS, m_opts_set, and trace_with_mask.

## set_error_none()

void ASSA::CmdLineOpts::set_error_none | ( |  | ) |

inline protected

Reset error message to an empty string.

Definition at line 302 of file CmdLineOpts.h.

{
trace_with_mask("CmdLineOpts::set_error_none", CMDLINEOPTS);
m_error = "";
}

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.

{
trace_with_mask ("CmdLineOpts::str_to_argv", CMDLINEOPTS);
std::vector<string> vs;
std::istringstream input (src_);
std::string token;
while (input >> token) {
vs.push_back (token);
token = "";
}
int i = 0;
char* p;
if (vs.size ()) {
argv_ = new char* [vs.size() + 1];
std::vector<string>::iterator it;
for (it = vs.begin (); it != vs.end (); it++, i++) {
p = new char [it->size() + 1];
strcpy (p, it->c_str ());
p[it->size()] = '\0';
argv_[i] = p;
}
argv_[i] = NULL;
}
argc_ = i;
}

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(), CmdLineOpts(), 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(), add_opt(), add_opt(), add_opt(), add_opt(), add_opt(), add_opt(), add_opt(), add_opt(), CmdLineOpts(), dump(), find_option(), find_option(), is_valid(), parse_config_file(), and rm_opt().

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

- CmdLineOpts.h
- CmdLineOpts.cpp