summaryrefslogtreecommitdiff
path: root/option.h
blob: c930fc09f5af8da9b6016561c99ada1fab345208 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#ifndef _OPTION_H
#define _OPTION_H

#include <map>
#include <list>
#include <vector>
#include <string>

struct option_handler;

struct option_t {
  char		   short_opt;
  std::string	   long_opt;
  bool		   wants_arg;
  option_handler * handler;

  option_t() : short_opt(0), wants_arg(false) {}
};

typedef std::map<const std::string, option_handler *>  option_handler_map;
typedef std::pair<const std::string, option_handler *> option_handler_pair;

struct option_handler {
  bool handled;

  static std::vector<option_t> options;
  static option_handler_map    handlers;

  option_handler(const std::string& label,
		 const std::string& opt_chars);

  virtual void handle_option(const char * arg = NULL) = 0;
};

bool process_option(const std::string& opt, const char * arg = NULL);
void process_arguments(int argc, char ** argv, const bool anywhere,
		       std::list<std::string>& args);
void process_environment(char ** envp, const std::string& tag);

#define DEF_OPT_HANDLERS()				\
  std::vector<option_t> option_handler::options;	\
  option_handler_map    option_handler::handlers

#define OPT_BEGIN(tag, chars)					\
  static struct tag ## _handler : public option_handler {	\
    tag ## _handler() : option_handler(#tag, chars) {}		\
    virtual void handle_option(const char * optarg)

#define OPT_END(tag) } tag ## _handler_obj

#endif // _OPTION_H