yaze 0.2.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
flag.cc
Go to the documentation of this file.
1#include "flag.h"
2
3namespace yaze {
4namespace util {
5
6void FlagParser::Parse(std::vector<std::string>* tokens) {
7 std::vector<std::string> leftover;
8 leftover.reserve(tokens->size());
9
10 for (size_t i = 0; i < tokens->size(); i++) {
11 const std::string& token = (*tokens)[i];
12 if (token.rfind("--", 0) == 0) {
13 // Found a token that starts with "--".
14 std::string flag_name;
15 std::string value_string;
16 if (!ExtractFlagAndValue(token, &flag_name, &value_string)) {
17 // If no value found after '=', see if next token is a value.
18 if ((i + 1) < tokens->size()) {
19 const std::string& next_token = (*tokens)[i + 1];
20 // If next token is NOT another flag, treat it as the value.
21 if (next_token.rfind("--", 0) != 0) {
22 value_string = next_token;
23 i++;
24 } else {
25 // If no explicit value, treat it as boolean 'true'.
26 value_string = "true";
27 }
28 } else {
29 value_string = "true";
30 }
31 flag_name = token;
32 }
33
34 // Attempt to parse the flag (strip leading dashes in the registry).
35 IFlag* flag_ptr = registry_->GetFlag(flag_name);
36 if (!flag_ptr) {
37 throw std::runtime_error("Unrecognized flag: " + flag_name);
38 }
39
40 // Set the parsed value on the matching flag.
41 flag_ptr->ParseValue(value_string);
42 } else {
43 leftover.push_back(token);
44 }
45 }
46 *tokens = leftover;
47}
48
49} // namespace util
50} // namespace yaze
FlagRegistry * registry_
Definition flag.h:135
bool ExtractFlagAndValue(const std::string &token, std::string *flag_name, std::string *value_string)
Definition flag.h:140
void Parse(int argc, char **argv)
Definition flag.h:121
virtual void ParseValue(const std::string &text)=0
Main namespace for the application.
Definition controller.cc:18