From 9f8d6a18f68fb54b62e9f4a5cf1a1650282b3667 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 18 Jul 2012 19:08:58 +0300 Subject: [PATCH] Allow seen_count to be bound to a variable for dealing with optional arguments --- source/core/getopt.cpp | 11 +++++++++++ source/core/getopt.h | 4 ++++ 2 files changed, 15 insertions(+) diff --git a/source/core/getopt.cpp b/source/core/getopt.cpp index 766b858..34e5317 100644 --- a/source/core/getopt.cpp +++ b/source/core/getopt.cpp @@ -227,6 +227,7 @@ GetOpt::OptBase::OptBase(char s, const std::string &l, ArgType a): lng(l), arg_type(a), seen_count(0), + ext_seen_count(0), metavar("ARG") { if(lng.empty()) @@ -246,11 +247,19 @@ GetOpt::OptBase &GetOpt::OptBase::set_help(const string &h, const string &m) return *this; } +GetOpt::OptBase &GetOpt::OptBase::bind_seen_count(unsigned &c) +{ + ext_seen_count = &c; + return *this; +} + void GetOpt::OptBase::process() { if(arg_type==REQUIRED_ARG) throw usage_error("--"+lng+" requires an argument"); ++seen_count; + if(ext_seen_count) + *ext_seen_count = seen_count; store(); } @@ -260,6 +269,8 @@ void GetOpt::OptBase::process(const string &arg) if(arg_type==NO_ARG) throw usage_error("--"+lng+" takes no argument"); ++seen_count; + if(ext_seen_count) + *ext_seen_count = seen_count; store(arg); } diff --git a/source/core/getopt.h b/source/core/getopt.h index dbc0f9c..2aaceef 100644 --- a/source/core/getopt.h +++ b/source/core/getopt.h @@ -72,6 +72,8 @@ public: metavariable is used to denote the argument in the option list. */ virtual Option &set_help(const std::string &, const std::string &) = 0; + virtual Option &bind_seen_count(unsigned &) = 0; + /// Returns the number of times this option was seen on the command line. virtual unsigned get_seen_count() const = 0; }; @@ -84,6 +86,7 @@ private: std::string lng; ArgType arg_type; unsigned seen_count; + unsigned *ext_seen_count; std::string help; std::string metavar; @@ -93,6 +96,7 @@ private: virtual OptBase &set_help(const std::string &); virtual OptBase &set_help(const std::string &, const std::string &); + virtual OptBase &bind_seen_count(unsigned &); char get_short() const { return shrt; } const std::string &get_long() const { return lng; } ArgType get_arg_type() const { return arg_type; } -- 2.43.0