From ed8d2f5a3be2f99e3ecdfba840f87f589a0c0ac6 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Mon, 3 Jun 2019 13:24:11 +0300 Subject: [PATCH] Allow std::vector to be used for GetOpt value lists --- source/core/getopt.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/source/core/getopt.h b/source/core/getopt.h index 8d78a72..e60a226 100644 --- a/source/core/getopt.h +++ b/source/core/getopt.h @@ -228,6 +228,13 @@ public: Option &add_option(char s, const std::string &l, T &d, ArgType a = NO_ARG) { return add_option(s, l, SimpleStore(d), a); } + /** Adds an option with both short and long forms. The option may be + specified multiple times, and the argument from each occurrence is stored in + the list. The argument type must be REQUIRED_ARG. */ + template + Option &add_option(char s, const std::string &l, std::vector &d, ArgType a = REQUIRED_ARG) + { return add_option(s, l, ListStore >(d), a); } + /** Adds an option with both short and long forms. The option may be specified multiple times, and the argument from each occurrence is stored in the list. The argument type must be REQUIRED_ARG. */ @@ -246,6 +253,13 @@ public: Argument &add_argument(const std::string &n, T &d, ArgType a = REQUIRED_ARG) { return add_argument(n, SimpleStore(d), a); } + /** Adds a positional argument list. If the list is declared as required, + at least one element must be given; an optional list may be empty. Only one + list may be added, and optional fixed arguments can't be used with it. */ + template + Argument &add_argument(const std::string &n, std::vector &d, ArgType a = REQUIRED_ARG) + { return add_argument(n, ListStore >(d), a); } + /** Adds a positional argument list. If the list is declared as required, at least one element must be given; an optional list may be empty. Only one list may be added, and optional fixed arguments can't be used with it. */ @@ -292,6 +306,9 @@ template<> inline void GetOpt::SimpleStore::store() template<> inline void GetOpt::SimpleStore::store(const std::string &a) { data = a; } +template<> inline void GetOpt::ListStore >::store(const std::string &a) +{ data.push_back(a); } + template<> inline void GetOpt::ListStore >::store(const std::string &a) { data.push_back(a); } -- 2.43.0