]> git.tdb.fi Git - libs/core.git/blobdiff - source/core/getopt.cpp
Allow seen_count to be bound to a variable for dealing with optional arguments
[libs/core.git] / source / core / getopt.cpp
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),
+       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);
 }