]> git.tdb.fi Git - libs/core.git/commitdiff
Allow seen_count to be bound to a variable for dealing with optional arguments
authorMikko Rasa <tdb@tdb.fi>
Wed, 18 Jul 2012 16:08:58 +0000 (19:08 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 18 Jul 2012 16:08:58 +0000 (19:08 +0300)
source/core/getopt.cpp
source/core/getopt.h

index 766b858e8d4aa4b2212b94f51756cad0a07a964b..34e53172aeb194d1cebc29c7df4de8719add8c27 100644 (file)
@@ -227,6 +227,7 @@ GetOpt::OptBase::OptBase(char s, const std::string &l, ArgType a):
        lng(l),
        arg_type(a),
        seen_count(0),
        lng(l),
        arg_type(a),
        seen_count(0),
+       ext_seen_count(0),
        metavar("ARG")
 {
        if(lng.empty())
        metavar("ARG")
 {
        if(lng.empty())
@@ -246,11 +247,19 @@ GetOpt::OptBase &GetOpt::OptBase::set_help(const string &h, const string &m)
        return *this;
 }
 
        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;
 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();
 }
 
        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(arg_type==NO_ARG)
                throw usage_error("--"+lng+" takes no argument");
        ++seen_count;
+       if(ext_seen_count)
+               *ext_seen_count = seen_count;
 
        store(arg);
 }
 
        store(arg);
 }
index dbc0f9cef0284e94d4f5ec66b890e94700515e4d..2aaceef836730aa24a966c3cfaa20bb9864e23a7 100644 (file)
@@ -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;
 
                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;
        };
                /// 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;
                std::string lng;
                ArgType arg_type;
                unsigned seen_count;
+               unsigned *ext_seen_count;
                std::string help;
                std::string metavar;
 
                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 &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; }
                char get_short() const { return shrt; }
                const std::string &get_long() const { return lng; }
                ArgType get_arg_type() const { return arg_type; }