Loading...
Searching...
No Matches
CmdLineOpts.h
Go to the documentation of this file.
// -*- c++ -*-
//------------------------------------------------------------------------------
// $Id: CmdLineOpts.h,v 1.7 2005/10/12 02:28:58 vlg Exp $
//------------------------------------------------------------------------------
// CmdLineOpts.h
//------------------------------------------------------------------------------
// Copyright (C) 2000,2005 Vladislav Grinchenko
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//------------------------------------------------------------------------------
#ifndef CMD_LINE_OPTS_H
#define CMD_LINE_OPTS_H
#include "assa/Assure.h"
#include <string>
#include <vector>
using std::string;
using std::vector;
namespace ASSA {
class CmdLineOpts;
class Option {
public:
friend class CmdLineOpts;
enum type_t {
string_t=0,
int_t,
uint_t,
long_t,
ulong_t,
double_t,
float_t,
flag_t,
func_t,
func_one_t,
none_t
};
private:
Option ();
Option (char shopt_, const string& lopt_, type_t type_, void* val_);
void dump () const;
const char* type_c_str ();
private:
char m_short_name;
string m_long_name;
type_t m_type;
void* m_val;
};
inline
Option::Option () :
m_short_name (' '), m_long_name (""), m_type (none_t), m_val (NULL)
{
/* empty */
}
inline
Option::Option (char shopt_, const string& lopt_, type_t type_, void* val_) :
m_short_name (shopt_), m_long_name (lopt_),
m_type (type_), m_val (val_)
{
trace_with_mask("Option::Option", CMDLINEOPTS);
}
/*----------------------------------------------------------------------------*/
class IniFile;
class CmdLineOpts
{
public:
typedef void (* OPTS_FUNC) (void);
typedef void (* OPTS_FUNC_ONE) (const string&);
typedef vector<Option> OptionSet;
CmdLineOpts ();
virtual ~CmdLineOpts () {
trace_with_mask ("CmdLineOpts::~CmdLineOpts", CMDLINEOPTS);
}
bool add_flag_opt (const char c, const string& s, bool* f);
bool add_opt (const char c, const string& s, string* str);
bool add_opt (const char c, const string& s, int* i);
bool add_opt (const char c, const string& s, unsigned int* ui);
bool add_opt (const char c, const string& s, long* l);
bool add_opt (const char c, const string& s, unsigned long* ul);
bool add_opt (const char c, const string& s, double* d);
bool add_opt (const char c, const string& s, float* f);
bool add_opt (const char c_, const string& s_, OPTS_FUNC f_);
bool add_opt (const char c_, const string& s_, OPTS_FUNC_ONE f_);
bool rm_opt (const char c_, const string& s_);
bool parse_args (const char* argv[]);
int parse_config_file (IniFile& inifile_);
const char* get_opt_error () const;
static void str_to_argv (const string& src_, int& argc_, char**& argv_);
static void free_argv (char**& argv_);
void dump () const;
protected:
bool is_valid (const char sopt_, const string& lopt_);
void set_error_none ();
bool assign (Option* node_, const char* op_);
Option* find_option (const char* str_);
Option* find_option (const char letter_);
virtual void pos_arg (const char* arg_);
private:
OptionSet m_opts_set;
string m_error;
};
inline void
CmdLineOpts::pos_arg (const char* /*arg_*/) { /* no-opt*/ }
inline
CmdLineOpts::CmdLineOpts () : m_opts_set (), m_error ("")
{
trace_with_mask("CmdLineOpts::CmdLineOpts", CMDLINEOPTS);
set_error_none ();
}
inline void
CmdLineOpts::set_error_none ()
{
trace_with_mask("CmdLineOpts::set_error_none", CMDLINEOPTS);
m_error = "";
}
inline const char*
CmdLineOpts::get_opt_error () const
{
return (m_error.c_str ());
}
} // end namespace ASSA
#endif /* CMD_LINE_OPTS_H */
Assure.h
A collection of assert function wrappers.
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::AutoPtrRef
A wrapper class to provide AutoPtr with reference semantics.
Definition AutoPtr.h:32
ASSA::CmdLineOpts
Class CmdLineOpts.
Definition CmdLineOpts.h:114
ASSA::CmdLineOpts::find_option
Option * find_option(const char *str_)
Locate option in the options set.
Definition CmdLineOpts.cpp:173
ASSA::CmdLineOpts::m_error
string m_error
Last reported error.
Definition CmdLineOpts.h:287
ASSA::CmdLineOpts::parse_args
bool parse_args(const char *argv[])
Parse command line arguments based on installed options set.
Definition CmdLineOpts.cpp:365
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::CmdLineOpts::set_error_none
void set_error_none()
Reset error message to an empty string.
Definition CmdLineOpts.h:302
ASSA::CmdLineOpts::~CmdLineOpts
virtual ~CmdLineOpts()
Do-nothing destructor.
Definition CmdLineOpts.h:125
ASSA::CmdLineOpts::add_opt
bool add_opt(const char c, const string &s, string *str)
Add an option with STL string argument.
Definition CmdLineOpts.cpp:220
ASSA::CmdLineOpts::rm_opt
bool rm_opt(const char c_, const string &s_)
Remove option for the option list.
Definition CmdLineOpts.cpp:346
ASSA::CmdLineOpts::m_opts_set
OptionSet m_opts_set
Options set.
Definition CmdLineOpts.h:284
ASSA::CmdLineOpts::add_flag_opt
bool add_flag_opt(const char c, const string &s, bool *f)
Add binary flag option.
Definition CmdLineOpts.cpp:206
ASSA::CmdLineOpts::assign
bool assign(Option *node_, const char *op_)
Perform value assignment to the node.
Definition CmdLineOpts.cpp:536
ASSA::CmdLineOpts::parse_config_file
int parse_config_file(IniFile &inifile_)
Parse configuration parameters found in [options] section of the INI file.
Definition CmdLineOpts.cpp:492
ASSA::CmdLineOpts::pos_arg
virtual void pos_arg(const char *arg_)
Process positional argument arg_.
Definition CmdLineOpts.h:292
ASSA::CmdLineOpts::str_to_argv
static void str_to_argv(const string &src_, int &argc_, char **&argv_)
Static function.
Definition CmdLineOpts.cpp:661
ASSA::CmdLineOpts::OptionSet
vector< Option > OptionSet
Definition CmdLineOpts.h:119
ASSA::CmdLineOpts::dump
void dump() const
Write options set to the log file.
Definition CmdLineOpts.cpp:646
ASSA::CmdLineOpts::CmdLineOpts
CmdLineOpts()
Default constructor.
Definition CmdLineOpts.h:295
ASSA::CmdLineOpts::get_opt_error
const char * get_opt_error() const
If previous call to one of member functions returned false, retrieve detailed error message.
Definition CmdLineOpts.h:309
ASSA::CmdLineOpts::free_argv
static void free_argv(char **&argv_)
Free up memory allocated by str_to_argv() function
Definition CmdLineOpts.cpp:693
ASSA::CmdLineOpts::is_valid
bool is_valid(const char sopt_, const string &lopt_)
Detect if supplied option is valid.
Definition CmdLineOpts.cpp:145
ASSA::IniFile
Definition IniFile.h:40
ASSA::Option
Option class.
Definition CmdLineOpts.h:39
ASSA::Option::type_t
type_t
Option type.
Definition CmdLineOpts.h:47
ASSA::Option::func_one_t
@ func_one_t
Convert argument to function with one argument
Definition CmdLineOpts.h:57
ASSA::Option::func_t
@ func_t
Convert argument to function
Definition CmdLineOpts.h:56
ASSA::Option::string_t
@ string_t
Convert argument to STL string
Definition CmdLineOpts.h:48
ASSA::Option::ulong_t
@ ulong_t
Convert argument to unsigned long
Definition CmdLineOpts.h:52
ASSA::Option::float_t
@ float_t
Convert argument to float
Definition CmdLineOpts.h:54
ASSA::Option::long_t
@ long_t
Convert argument to long
Definition CmdLineOpts.h:51
ASSA::Option::double_t
@ double_t
Convert argument to double
Definition CmdLineOpts.h:53
ASSA::Option::flag_t
@ flag_t
No argument; bool value is flipped.
Definition CmdLineOpts.h:55
ASSA::Option::int_t
@ int_t
Convert argument to int
Definition CmdLineOpts.h:49
ASSA::Option::uint_t
@ uint_t
Convert argument to unsigned int
Definition CmdLineOpts.h:50
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::Option
Option()
Private default constructor.
Definition CmdLineOpts.h:89
ASSA::Option::m_val
void * m_val
Pointer to the option value.
Definition CmdLineOpts.h:85
ASSA::Option::dump
void dump() const
Write object state to the log file.
Definition CmdLineOpts.cpp:30
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
ASSA
Definition Acceptor.h:40
ASSA::CMDLINEOPTS
@ CMDLINEOPTS
Class CmdLineOpts messages
Definition LogMask.h:36