32 std::ostringstream msg;
42 msg <<
"--" << std::setiosflags (std::ios::left)
46 msg << std::setiosflags (std::ios::left) << std::setw (14) <<
" ";
53 msg << std::setiosflags (std::ios::left) << std::setw(7) <<
"string";
54 msg <<
"] = '" << *(
string*)
m_val <<
"'";
58 msg << std::setiosflags(std::ios::left) << std::setw(7) <<
"int";
59 msg <<
"] = " << *(
int*)
m_val;
63 msg << std::setiosflags(std::ios::left) << std::setw(7) <<
"u_int";
64 msg <<
"] = " << *(
int*)
m_val;
68 msg << std::setiosflags(std::ios::left) << std::setw(7) <<
"long";
69 msg <<
"] = " << *(
long*)
m_val;
73 msg << std::setiosflags(std::ios::left) << std::setw(7) <<
"u_long";
74 msg <<
"] = " << *(
long*)
m_val;
78 msg << std::setiosflags(std::ios::left) << std::setw(7) <<
"double";
79 msg <<
"] = " << *(
double*)
m_val;
83 msg << std::setiosflags(std::ios::left) << std::setw(7) <<
"float";
84 msg <<
"] = " << *(
float*)
m_val;
88 msg << std::setiosflags(std::ios::left) << std::setw(7) <<
"bool";
89 msg <<
"] = " << *(
bool*)
m_val ?
"true" :
"false";
93 msg << std::setiosflags(std::ios::left)
94 << std::setw(7) <<
"function ()";
99 msg << std::setiosflags(std::ios::left)
100 << std::setw(7) <<
"function (opt)";
105 msg << std::setiosflags(std::ios::left) << std::setw(7) <<
"none";
110 msg << std::setiosflags(std::ios::left)
111 << std::setw(7) <<
"--undef--";
137 default: ret =
"--undef--";
145 is_valid (
const char sopt_,
const string& lopt_)
150 OptionSet::const_iterator i;
153 if (sopt_ ==
'\0' && lopt_.empty ()) {
154 m_error =
"Ignore empty option";
157 else if (sopt_ !=
'\0' && i->m_short_name == sopt_) {
158 m_error =
"Ignored multiple option '-";
159 m_error += sopt_ + string (
"'");
162 else if (!lopt_.empty () && i->m_long_name == lopt_) {
163 m_error =
"Ignore multiple option '--";
164 m_error += lopt_ + string (
"'");
177 OptionSet::iterator i;
181 if (i->m_long_name == str_) {
194 OptionSet::iterator i;
198 if (i->m_short_name == letter_)
206 add_flag_opt (
const char sopt_,
const string& lopt_,
bool* v_)
220 add_opt (
const char sopt_,
const string& lopt_,
string* v_)
234 add_opt (
const char sopt_,
const string& lopt_,
int* v_)
248 add_opt (
const char sopt_,
const string& lopt_,
unsigned int* v_)
262 add_opt (
const char sopt_,
const string& lopt_,
long* v_)
276 add_opt (
const char sopt_,
const string& lopt_,
unsigned long* v_)
290 add_opt (
const char sopt_,
const string& lopt_,
double* v_)
304 add_opt (
const char sopt_,
const string& lopt_,
float* v_)
318 add_opt (
const char sopt_,
const string& lopt_, OPTS_FUNC v_)
332 add_opt (
const char sopt_,
const string& lopt_, OPTS_FUNC_ONE v_)
346 rm_opt (
const char sopt_,
const string& lopt_)
350 OptionSet::iterator i;
354 if (i->m_short_name == sopt_ || i->m_long_name == lopt_)
369 register int skip = 1;
370 bool pos_args_started =
false;
376 for (argv_++; argv_[0]; argv_ += skip) {
383 if (pos_args_started) {
386 if (token[0] ==
'-' && token.size () != 1) {
387 m_error =
"Invalid order of arguments: '";
396 if (token[0] ==
'-' && token.size () > 1 && token[1] !=
'-') {
397 if (token.size () == 1 && !pos_args_started) {
399 pos_args_started =
true;
404 if (token.size () > 2) {
412 param = token.substr (2);
418 if (token.size () > 1 && token[1] ==
'-') {
419 string op = token.substr (2);
422 if ((pos = op.find (
"=")) != (
size_t)-1) {
423 param = op.substr (pos+1, op.length ());
424 op.replace (pos, op.length() - pos,
"");
430 pos_args_started =
true;
436 m_error =
"Invalid option '" + token +
"'.";
443 if (param.empty ()) {
445 m_error =
"Expecting parameter after '"
446 + string (argv_[0]) +
"'.";
460 if (param.empty ()) {
461 if (!
assign (node, argv_[1])) {
466 const char* str = param.c_str ();
467 if (!
assign (node, str)) {
475 return !
m_error.empty () ? false :
true;
496 unsigned int count = 0;
499 string optsect_name (
"options");
500 OptionSet::iterator pos =
m_opts_set.begin ();
504 optsect_name =
"Options";
507 optsect_name =
"OPTIONS";
510 m_error =
"Missing [options] section in INI file!";
517 if (pos->m_long_name.size ()) {
518 s = pos->m_long_name;
521 v = inifile_.
get_value (optsect_name, s);
523 if (
assign (&(*pos), v.c_str ())) {
560 *(
string*) node_->
m_val = op_;
566 l = strtol (op_, NULL, 0);
569 m_error =
"Error: '" + string (strerror (errno)) +
"',";
570 m_error +=
" in converting to integer from '";
571 m_error += string (op_) +
"'.";
576 *(
int*) node_->
m_val = int (l);
579 *(
long*) node_->
m_val = l;
586 l = strtol (op_, NULL, 0);
589 m_error =
"Error: '" + string (strerror (errno)) +
"',";
590 m_error +=
" in converting to unsigned integer from '";
591 m_error += string (op_) +
"'.";
596 *(
unsigned int*) node_->
m_val = int (l);
599 *(
unsigned long*) node_->
m_val = l;
606 d = strtod (op_, NULL);
609 m_error =
"Error: '" + string (strerror (errno)) +
"',";
610 m_error +=
" in converting to double/float from '";
611 m_error += string (op_) +
"'.";
616 *(
double*) node_->
m_val = d;
619 *(
float*) node_->
m_val = float (d);
624 *(
bool*) node_->
m_val =
true;
637 m_error =
"Undefined type for option '"+string (op_)+
"'.";
648 OptionSet::const_iterator i;
661 str_to_argv (
const string& src_,
int& argc_,
char**& argv_)
665 std::vector<string> vs;
666 std::istringstream input (src_);
669 while (input >> token) {
670 vs.push_back (token);
677 argv_ =
new char* [vs.size() + 1];
678 std::vector<string>::iterator it;
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';
704 for (
int i = 0; argv_[i]; i++) {
Class to handle processing command-line options.
A Windows-style INI configuration file management class.
An abstraction to message logging facility.
#define DL(X)
A macro for writing debug message to the Logger.
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Option * find_option(const char *str_)
Locate option in the options set.
string m_error
Last reported error.
bool parse_args(const char *argv[])
Parse command line arguments based on installed options set.
void(* OPTS_FUNC_ONE)(const string &)
void set_error_none()
Reset error message to an empty string.
bool add_opt(const char c, const string &s, string *str)
Add an option with STL string argument.
bool rm_opt(const char c_, const string &s_)
Remove option for the option list.
OptionSet m_opts_set
Options set.
bool add_flag_opt(const char c, const string &s, bool *f)
Add binary flag option.
bool assign(Option *node_, const char *op_)
Perform value assignment to the node.
int parse_config_file(IniFile &inifile_)
Parse configuration parameters found in [options] section of the INI file.
virtual void pos_arg(const char *arg_)
Process positional argument arg_.
static void str_to_argv(const string &src_, int &argc_, char **&argv_)
Static function.
void dump() const
Write options set to the log file.
static void free_argv(char **&argv_)
Free up memory allocated by str_to_argv() function
bool is_valid(const char sopt_, const string &lopt_)
Detect if supplied option is valid.
config_iterator find_section(const string §ion_)
Find section by its name.
config_iterator sect_end()
Return iterator past the last section.
string get_value(const string §ion_, const string &name_) const
Find and return a value of the name/value pair in the section section_.
@ func_one_t
Convert argument to function with one argument
@ func_t
Convert argument to function
@ string_t
Convert argument to STL string
@ ulong_t
Convert argument to unsigned long
@ float_t
Convert argument to float
@ long_t
Convert argument to long
@ double_t
Convert argument to double
@ flag_t
No argument; bool value is flipped.
@ int_t
Convert argument to int
@ uint_t
Convert argument to unsigned int
type_t m_type
Option type.
string m_long_name
Long option name.
void * m_val
Pointer to the option value.
void dump() const
Write object state to the log file.
const char * type_c_str()
Return the type of the Option object.
char m_short_name
One-letter option name.
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_.
@ CMDLINEOPTS
Class CmdLineOpts messages
Socket & ends(Socket &os_)
ends manipulator.