I've literally spent 70 minutes trying to get this to work using
composing()
to allow repeats
- vector valued dummy options with
implicit_value({}, "")
custom notifiers (they only run once)
- custom
notifier()
- they only run once regardless of how often the option is present and successfully parsed
- getting a vector-valued option's size from the
variables_map
after store
/notify
. Sadly, the size is always 1, presumably because the "compose" doesn't actually compose within a single store operation (it only composes between several runs, so different sources of options, then?).
The sad conclusion is, there doesn't appear to be a way. Here, my usual mantra is confirmed: "Simplicity Trumps Omniscient Design", and I'd suggest doing the same but using https://github.com/adishavit/argh:
Live On Coliru
#include "argh.h"
#include <iostream>
namespace {
template <typename It>
size_t size(std::pair<It, It> const& range) { return std::distance(range.first, range.second); }
}
int main(int argc, char** argv) {
argh::parser p(argc, argv);
auto num_s = size(p.flags().equal_range("s"));
bool const variable = num_s % 2;
std::cout << "Repeated: " << num_s << ", effective " << std::boolalpha << variable;
std::cout << " (Command line was:";
while (*argv) std::cout << " " << *argv++;
std::cout << ")\n";
}
When run with various commandlines, prints:
Repeated: 0, effective false (Command line was: ../build/sotest)
Repeated: 1, effective true (Command line was: ../build/sotest -s)
Repeated: 2, effective false (Command line was: ../build/sotest -s -s)
Repeated: 3, effective true (Command line was: ../build/sotest -s -s -s)
Repeated: 4, effective false (Command line was: ../build/sotest -s -s -s -s)
Repeated: 3, effective true (Command line was: ../build/sotest -s -s -s --other bogus)