diff options
Diffstat (limited to 'option.h')
-rw-r--r-- | option.h | 37 |
1 files changed, 8 insertions, 29 deletions
@@ -1,11 +1,14 @@ #ifndef _OPTION_H #define _OPTION_H -#include <map> #include <deque> #include <string> -struct option_handler; +struct option_handler { + bool handled; + option_handler() : handled(false) {} + virtual void operator()(const char * arg = NULL) = 0; +}; struct option_t { char short_opt; @@ -13,38 +16,14 @@ struct option_t { 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::deque<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; + option_t() : short_opt(0), wants_arg(false), handler(NULL) {} }; +void register_option(const std::string& label, + const std::string& opt_chars, option_handler& option); bool process_option(const std::string& opt, const char * arg = NULL); void process_arguments(int argc, char ** argv, const bool anywhere, std::deque<std::string>& args); void process_environment(char ** envp, const std::string& tag); -#define DEF_OPT_HANDLERS() \ - std::deque<option_t> option_handler::options; \ - option_handler_map option_handler::handlers - -#define OPT_BEGIN(tag, chars) \ - static struct opt_ ## tag ## _handler : public option_handler { \ - opt_ ## tag ## _handler() : option_handler(#tag, chars) {} \ - virtual void handle_option(const char * optarg) - -#define OPT_END(tag) } opt_ ## tag ## _handler_obj - #endif // _OPTION_H |