Parses a string as a command line flag. The string should have the format "--flag=value". When def_optional is true, the "=value" part can be omitted. Returns the value of the flag, or NULL if the parsing failed.
| 5861 | // |
| 5862 | // Returns the value of the flag, or NULL if the parsing failed. |
| 5863 | const char* ParseFlagValue(const char* str, |
| 5864 | const char* flag, |
| 5865 | bool def_optional) { |
| 5866 | // str and flag must not be NULL. |
| 5867 | if (str == NULL || flag == NULL) return NULL; |
| 5868 | |
| 5869 | // The flag must start with "--" followed by GTEST_FLAG_PREFIX_. |
| 5870 | const String flag_str = String::Format("--%s%s", GTEST_FLAG_PREFIX_, flag); |
| 5871 | const size_t flag_len = flag_str.length(); |
| 5872 | if (strncmp(str, flag_str.c_str(), flag_len) != 0) return NULL; |
| 5873 | |
| 5874 | // Skips the flag name. |
| 5875 | const char* flag_end = str + flag_len; |
| 5876 | |
| 5877 | // When def_optional is true, it's OK to not have a "=value" part. |
| 5878 | if (def_optional && (flag_end[0] == '\0')) { |
| 5879 | return flag_end; |
| 5880 | } |
| 5881 | |
| 5882 | // If def_optional is true and there are more characters after the |
| 5883 | // flag name, or if def_optional is false, there must be a '=' after |
| 5884 | // the flag name. |
| 5885 | if (flag_end[0] != '=') return NULL; |
| 5886 | |
| 5887 | // Returns the string after "=". |
| 5888 | return flag_end + 1; |
| 5889 | } |
| 5890 | |
| 5891 | // Parses a string for a bool flag, in the form of either |
| 5892 | // "--flag=value" or "--flag". |
no test coverage detected