From 736d15fbdee0ea47b38a4d5fcd321e86eb21b465 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 14 Jul 2012 17:55:14 +0300 Subject: [PATCH] Replace earlier options sharing the same short or long name --- source/core/getopt.cpp | 17 +++++++++++++++++ source/core/getopt.h | 6 ++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/source/core/getopt.cpp b/source/core/getopt.cpp index cfc9d7a..911b673 100644 --- a/source/core/getopt.cpp +++ b/source/core/getopt.cpp @@ -17,6 +17,23 @@ GetOpt::~GetOpt() delete *i; } +GetOpt::OptBase &GetOpt::add_option(OptBase *opt) +{ + for(list::iterator i=opts.begin(); i!=opts.end(); ) + { + if((*i)->get_short()==opt->get_short() || (*i)->get_long()==opt->get_long()) + { + delete *i; + opts.erase(i++); + } + else + ++i; + } + + opts.push_back(opt); + return *opts.back(); +} + GetOpt::OptBase &GetOpt::get_option(char s) { for(list::iterator i=opts.begin(); i!=opts.end(); ++i) diff --git a/source/core/getopt.h b/source/core/getopt.h index ea1f7d3..a3b7ae4 100644 --- a/source/core/getopt.h +++ b/source/core/getopt.h @@ -123,17 +123,19 @@ public: template OptBase &add_option(char s, const std::string &l, T &d, ArgType a = NO_ARG) - { opts.push_back(new Option(s, l, d, a)); return *opts.back(); } + { return add_option(new Option(s, l, d, a)); } template OptBase &add_option(char s, const std::string &l, std::list &d, ArgType a = REQUIRED_ARG) - { opts.push_back(new ListOption >(s, l, d, a)); return *opts.back(); } + { return add_option(new ListOption >(s, l, d, a)); } template OptBase &add_option(const std::string &l, T &d, ArgType a) { return add_option(0, l, d, a); } private: + OptBase &add_option(OptBase *); + OptBase &get_option(char); OptBase &get_option(const std::string &); -- 2.43.0