A description of a single command line flag, holding its name, type, usage text, and a pointer to the corresponding variable.
| 61 | // A description of a single command line flag, holding its name, type, usage |
| 62 | // text, and a pointer to the corresponding variable. |
| 63 | class Flag { |
| 64 | public: |
| 65 | Flag(const char* name, int32* dst, const string& usage_text); |
| 66 | Flag(const char* name, int64* dst, const string& usage_text); |
| 67 | Flag(const char* name, bool* dst, const string& usage_text); |
| 68 | Flag(const char* name, string* dst, const string& usage_text); |
| 69 | Flag(const char* name, float* dst, const string& usage_text); |
| 70 | |
| 71 | // These constructors invoke a hook on a match instead of writing to a |
| 72 | // specific memory location. The hook may return false to signal a malformed |
| 73 | // or illegal value, which will then fail the command line parse. |
| 74 | // |
| 75 | // "default_value_for_display" is shown as the default value of this flag in |
| 76 | // Flags::Usage(). |
| 77 | Flag(const char* name, std::function<bool(int32)> int32_hook, |
| 78 | int32 default_value_for_display, const string& usage_text); |
| 79 | Flag(const char* name, std::function<bool(int64)> int64_hook, |
| 80 | int64 default_value_for_display, const string& usage_text); |
| 81 | Flag(const char* name, std::function<bool(float)> float_hook, |
| 82 | float default_value_for_display, const string& usage_text); |
| 83 | Flag(const char* name, std::function<bool(bool)> bool_hook, |
| 84 | bool default_value_for_display, const string& usage_text); |
| 85 | Flag(const char* name, std::function<bool(string)> string_hook, |
| 86 | string default_value_for_display, const string& usage_text); |
| 87 | |
| 88 | private: |
| 89 | friend class Flags; |
| 90 | |
| 91 | bool Parse(string arg, bool* value_parsing_ok) const; |
| 92 | |
| 93 | string name_; |
| 94 | enum { |
| 95 | TYPE_INT32, |
| 96 | TYPE_INT64, |
| 97 | TYPE_BOOL, |
| 98 | TYPE_STRING, |
| 99 | TYPE_FLOAT, |
| 100 | } type_; |
| 101 | |
| 102 | std::function<bool(int32)> int32_hook_; |
| 103 | int32 int32_default_for_display_; |
| 104 | |
| 105 | std::function<bool(int64)> int64_hook_; |
| 106 | int64 int64_default_for_display_; |
| 107 | |
| 108 | std::function<bool(float)> float_hook_; |
| 109 | float float_default_for_display_; |
| 110 | |
| 111 | std::function<bool(bool)> bool_hook_; |
| 112 | bool bool_default_for_display_; |
| 113 | |
| 114 | std::function<bool(string)> string_hook_; |
| 115 | string string_default_for_display_; |
| 116 | |
| 117 | string usage_text_; |
| 118 | }; |
| 119 | |
| 120 | class Flags { |
no outgoing calls