libassa  3.5.1
CmdLineOpts.h
Go to the documentation of this file.
1 // -*- c++ -*-
2 //------------------------------------------------------------------------------
3 // $Id: CmdLineOpts.h,v 1.7 2005/10/12 02:28:58 vlg Exp $
4 //------------------------------------------------------------------------------
5 // CmdLineOpts.h
6 //------------------------------------------------------------------------------
7 // Copyright (C) 2000,2005 Vladislav Grinchenko
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Library General Public
11 // License as published by the Free Software Foundation; either
12 // version 2 of the License, or (at your option) any later version.
13 //------------------------------------------------------------------------------
14 
15 #ifndef CMD_LINE_OPTS_H
16 #define CMD_LINE_OPTS_H
17 
18 #include "assa/Assure.h"
19 
20 #include <string>
21 #include <vector>
22 using std::string;
23 using std::vector;
24 
25 namespace ASSA {
26 
27 class CmdLineOpts;
28 
39 class Option {
40 public:
41  friend class CmdLineOpts;
42 
47  enum type_t {
58  none_t
59  };
60 
61 private:
63  Option ();
64 
66  Option (char shopt_, const string& lopt_, type_t type_, void* val_);
67 
69  void dump () const;
70 
72  const char* type_c_str ();
73 
74 private:
77 
79  string m_long_name;
80 
83 
85  void* m_val;
86 };
87 
88 inline
90  m_short_name (' '), m_long_name (""), m_type (none_t), m_val (NULL)
91 {
92  /* empty */
93 }
94 
95 inline
96 Option::Option (char shopt_, const string& lopt_, type_t type_, void* val_) :
97  m_short_name (shopt_), m_long_name (lopt_),
98  m_type (type_), m_val (val_)
99 {
100  trace_with_mask("Option::Option", CMDLINEOPTS);
101 }
102 
103 /*----------------------------------------------------------------------------*/
104 class IniFile;
105 
114 {
115 public:
116  typedef void (* OPTS_FUNC) (void);
117  typedef void (* OPTS_FUNC_ONE) (const string&);
118 
119  typedef vector<Option> OptionSet;
120 
122  CmdLineOpts ();
123 
125  virtual ~CmdLineOpts () {
126  trace_with_mask ("CmdLineOpts::~CmdLineOpts", CMDLINEOPTS);
127  }
128 
135  bool add_flag_opt (const char c, const string& s, bool* f);
136 
143  bool add_opt (const char c, const string& s, string* str);
144 
151  bool add_opt (const char c, const string& s, int* i);
152 
159  bool add_opt (const char c, const string& s, unsigned int* ui);
160 
167  bool add_opt (const char c, const string& s, long* l);
168 
175  bool add_opt (const char c, const string& s, unsigned long* ul);
176 
183  bool add_opt (const char c, const string& s, double* d);
184 
191  bool add_opt (const char c, const string& s, float* f);
192 
204  bool add_opt (const char c_, const string& s_, OPTS_FUNC f_);
205 
216  bool add_opt (const char c_, const string& s_, OPTS_FUNC_ONE f_);
217 
223  bool rm_opt (const char c_, const string& s_);
224 
228  bool parse_args (const char* argv[]);
229 
237  int parse_config_file (IniFile& inifile_);
238 
242  const char* get_opt_error () const;
243 
252  static void str_to_argv (const string& src_, int& argc_, char**& argv_);
253 
255  static void free_argv (char**& argv_);
256 
258  void dump () const;
259 
260 protected:
262  bool is_valid (const char sopt_, const string& lopt_);
263 
265  void set_error_none ();
266 
268  bool assign (Option* node_, const char* op_);
269 
271  Option* find_option (const char* str_);
272 
274  Option* find_option (const char letter_);
275 
280  virtual void pos_arg (const char* arg_);
281 
282 private:
285 
287  string m_error;
288 };
289 
290 
291 inline void
292 CmdLineOpts::pos_arg (const char* /*arg_*/) { /* no-opt*/ }
293 
294 inline
295 CmdLineOpts::CmdLineOpts () : m_opts_set (), m_error ("")
296 {
297  trace_with_mask("CmdLineOpts::CmdLineOpts", CMDLINEOPTS);
298  set_error_none ();
299 }
300 
301 inline void
303 {
304  trace_with_mask("CmdLineOpts::set_error_none", CMDLINEOPTS);
305  m_error = "";
306 }
307 
308 inline const char*
310 {
311  return (m_error.c_str ());
312 }
313 
314 
315 } // end namespace ASSA
316 
317 #endif /* CMD_LINE_OPTS_H */
A collection of assert function wrappers.
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition: Logger.h:437
Class CmdLineOpts.
Definition: CmdLineOpts.h:114
Option * find_option(const char *str_)
Locate option in the options set.
string m_error
Last reported error.
Definition: CmdLineOpts.h:287
bool parse_args(const char *argv[])
Parse command line arguments based on installed options set.
void(* OPTS_FUNC)(void)
Definition: CmdLineOpts.h:116
void(* OPTS_FUNC_ONE)(const string &)
Definition: CmdLineOpts.h:117
void set_error_none()
Reset error message to an empty string.
Definition: CmdLineOpts.h:302
virtual ~CmdLineOpts()
Do-nothing destructor.
Definition: CmdLineOpts.h:125
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.
Definition: CmdLineOpts.h:284
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_.
Definition: CmdLineOpts.h:292
static void str_to_argv(const string &src_, int &argc_, char **&argv_)
Static function.
vector< Option > OptionSet
Definition: CmdLineOpts.h:119
void dump() const
Write options set to the log file.
CmdLineOpts()
Default constructor.
Definition: CmdLineOpts.h:295
const char * get_opt_error() const
If previous call to one of member functions returned false, retrieve detailed error message.
Definition: CmdLineOpts.h:309
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.
Option class.
Definition: CmdLineOpts.h:39
type_t
Option type.
Definition: CmdLineOpts.h:47
@ func_one_t
Convert argument to function with one argument
Definition: CmdLineOpts.h:57
@ func_t
Convert argument to function
Definition: CmdLineOpts.h:56
@ string_t
Convert argument to STL string
Definition: CmdLineOpts.h:48
@ ulong_t
Convert argument to unsigned long
Definition: CmdLineOpts.h:52
@ float_t
Convert argument to float
Definition: CmdLineOpts.h:54
@ long_t
Convert argument to long
Definition: CmdLineOpts.h:51
@ double_t
Convert argument to double
Definition: CmdLineOpts.h:53
@ flag_t
No argument; bool value is flipped.
Definition: CmdLineOpts.h:55
@ int_t
Convert argument to int
Definition: CmdLineOpts.h:49
@ uint_t
Convert argument to unsigned int
Definition: CmdLineOpts.h:50
type_t m_type
Option type.
Definition: CmdLineOpts.h:82
string m_long_name
Long option name.
Definition: CmdLineOpts.h:79
Option()
Private default constructor.
Definition: CmdLineOpts.h:89
void * m_val
Pointer to the option value.
Definition: CmdLineOpts.h:85
void dump() const
Write object state to the log file.
Definition: CmdLineOpts.cpp:30
const char * type_c_str()
Return the type of the Option object.
char m_short_name
One-letter option name.
Definition: CmdLineOpts.h:76
Definition: Acceptor.h:40
@ CMDLINEOPTS
Class CmdLineOpts messages
Definition: LogMask.h:36