A description of a single command line flag, holding its name, type, usage text, and a pointer to the corresponding variable.
| 63 | // A description of a single command line flag, holding its name, type, usage |
| 64 | // text, and a pointer to the corresponding variable. |
| 65 | class Flag { |
| 66 | public: |
| 67 | template <typename T> |
| 68 | static Flag CreateFlag(const char* name, T* val, const char* usage) { |
| 69 | return Flag(name, [val](const T& v) { *val = v; }, *val, usage); |
| 70 | } |
| 71 | |
| 72 | Flag(const char* name, const std::function<void(const int32_t&)>& hook, |
| 73 | int32_t default_value, const std::string& usage_text); |
| 74 | Flag(const char* name, const std::function<void(const int64_t&)>& hook, |
| 75 | int64_t default_value, const std::string& usage_text); |
| 76 | Flag(const char* name, const std::function<void(const float&)>& hook, |
| 77 | float default_value, const std::string& usage_text); |
| 78 | Flag(const char* name, const std::function<void(const bool&)>& hook, |
| 79 | bool default_value, const std::string& usage_text); |
| 80 | Flag(const char* name, const std::function<void(const std::string&)>& hook, |
| 81 | const std::string& default_value, const std::string& usage_text); |
| 82 | |
| 83 | private: |
| 84 | friend class Flags; |
| 85 | |
| 86 | bool Parse(const std::string& arg, bool* value_parsing_ok) const; |
| 87 | |
| 88 | std::string name_; |
| 89 | enum { |
| 90 | TYPE_INT32, |
| 91 | TYPE_INT64, |
| 92 | TYPE_BOOL, |
| 93 | TYPE_STRING, |
| 94 | TYPE_FLOAT, |
| 95 | } type_; |
| 96 | |
| 97 | std::string GetTypeName() const; |
| 98 | |
| 99 | std::function<bool(const std::string&)> value_hook_; |
| 100 | std::string default_for_display_; |
| 101 | |
| 102 | std::string usage_text_; |
| 103 | }; |
| 104 | |
| 105 | class Flags { |
| 106 | public: |
no outgoing calls