bfgroup/Lyra

Support for boolean flags with cardinality?

lubgr opened this issue · 1 comments

lubgr commented

If I am not mistaken, it is currently impossible to have more than one identical bool flag. More precisely, it is ok to assign a cardinality to a flag,

bool verbose = false;

lyra::opt(verbose)["-v"]["--verbose"]("Verbosity").cardinality(0, 3)

but this doesn't have any effect as the underlying type is obviously binary. However, the above example is sometimes desirable (take ssh -vvv for example). Would it make sense (and be within reasonable bounds w.r.t the effort) to implement this, or is it too feature-creepy?

Sorry for the late reply. Been busy with work projects.. That is already possible two different ways. The wasteful way is to use an std::vector<bool> as the value. The nicer way is to use a lambda to count the verbosity. I'm adding that as an example in the next version. But here is how that would work:

#include <iostream>
#include <lyra/lyra.hpp>

int main(int argc, const char ** argv)
{
	int verbose = 0;
	return lyra::main()
		(lyra::opt([&](bool v){ verbose += 1; })["-v"].cardinality(0,3))
		(argc, argv, [&](lyra::main & m)
		{
			std::cout << verbose << "\n";
			return 0;
		});
}