'cl::opt' and range checking

Hi LLVM-Devs,

The LLVM ‘cl::opt’ templates are really useful, and provide a lot of flexibility. But I don’t see any support for ranged options. What I would like is to have an option which is an integer type, and to restrict the set of values provided to a simple upper and lower bound, for example between 10 and 2000.

Is there a way of expressing this with the ‘cl::opt’ templates, or do I have to explicitly range check the values in my own code and issue a diagnostic or clamp as appropriate?



I don’t think this is currently supported by cl::opt, but you can probably write your own adapter class so that you could write cl::opt<cl::range<int,3,7>>.

You’d just need to write a parser for cl::range and do the checking in there. Seems like a useful patch, I’ve needed this from time to time myself.

Thanks Zachary,

That’s seems like the best approach; it is certainly useful from time-to-time and makes sense that checking for out-of-range is in a shared reusable component.

All the best,