diff --git a/include/popl.hpp b/include/popl.hpp index 8fd668b..fb302b0 100644 --- a/include/popl.hpp +++ b/include/popl.hpp @@ -298,7 +298,40 @@ class Switch : public Value void parse(OptionName what_name, const char* value) override; }; +/// Bounded value option +/** + * Bounded value option + * Checks if the value meets boundary predicate, + * for example ( x > 3 && x < 99 ) + */ +template +class BoundedValue : public Value +{ +public: + using Predicate = bool(*)(T); + + /// Construct a BoundedValue Option + /// @param short_name the option's short name. Must be empty or one character. + /// @param long_name the option's long name. Can be empty. + /// @param description the Option's description that will be shown in the help message + /// @param predicate the option's correctness predicate, returns true if option is correct + BoundedValue(const std::string& short_name, const std::string& long_name, const std::string& description, Predicate predicate); + + /// Construct a BoundedValue Option + /// @param short_name the option's short name. Must be empty or one character. + /// @param long_name the option's long name. Can be empty. + /// @param description the Option's description that will be shown in the help message + /// @param predicate the option's correctness predicate, returns true if option is correct + /// @param default_val the Option's default value + /// @param assign_to pointer to a variable to assign the parsed command line value to + BoundedValue(const std::string& short_name, const std::string& long_name, const std::string& description, Predicate predicate, const T& default_val, T* assign_to = nullptr); + +protected: + void parse(OptionName what_name, const char* value) override; +private: + Predicate predicate_; +}; using Option_ptr = std::shared_ptr