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
3#include <iostream>
4
5#include "yaze_config.h"
6
7namespace yaze {
8namespace util {
9
10void FlagParser::Parse(std::vector<std::string>* tokens) {
11 std::vector<std::string> leftover;
12 leftover.reserve(tokens->size());
13
14 for (size_t i = 0; i < tokens->size(); i++) {
15 const std::string& token = (*tokens)[i];
16 if (token.rfind("--", 0) == 0) {
17 // Found a token that starts with "--".
18 std::string flag_name;
19 std::string value_string;
20 if (!ExtractFlagAndValue(token, &flag_name, &value_string)) {
21 // If no value found after '=', see if next token is a value.
22 if ((i + 1) < tokens->size()) {
23 const std::string& next_token = (*tokens)[i + 1];
24 // If next token is NOT another flag, treat it as the value.
25 if (next_token.rfind("--", 0) != 0) {
26 value_string = next_token;
27 i++;
28 } else {
29 // If no explicit value, treat it as boolean 'true'.
30 value_string = "true";
31 }
32 } else {
33 value_string = "true";
34 }
35 flag_name = token;
36 }
37
38 // Attempt to parse the flag (strip leading dashes in the registry).
39 IFlag* flag_ptr = registry_->GetFlag(flag_name);
40 if (!flag_ptr) {
41 throw std::runtime_error("Unrecognized flag: " + flag_name);
42 }
43
44 // Set the parsed value on the matching flag.
45 flag_ptr->ParseValue(value_string);
46 } else if (token.rfind("-", 0) == 0) {
47 if (token == "-v" || token == "-version") {
48 std::cout << "Version: " << YAZE_VERSION_MAJOR << "."
49 << YAZE_VERSION_MINOR << "." << YAZE_VERSION_PATCH << "\n";
50 exit(0);
51 }
52
53 // Check for -h or -help
54 if (token == "-h" || token == "-help") {
55 std::cout << "Available flags:\n";
56 for (const auto& flag :
58 std::cout << flag->name() << ": " << flag->help() << "\n";
59 }
60 exit(0);
61 }
62
63 std::string flag_name;
64 if (!ExtractFlag(token, &flag_name)) {
65 throw std::runtime_error("Unrecognized flag: " + token);
66 }
67
68 } else {
69 leftover.push_back(token);
70 }
71 }
72 *tokens = leftover;
73}
74
75bool FlagParser::ExtractFlagAndValue(const std::string& token,
76 std::string* flag_name,
77 std::string* value_string) {
78 const size_t eq_pos = token.find('=');
79 if (eq_pos == std::string::npos) {
80 return false;
81 }
82 *flag_name = token.substr(0, eq_pos);
83 *value_string = token.substr(eq_pos + 1);
84 return true;
85}
86
87bool FlagParser::ExtractFlag(const std::string& token, std::string* flag_name) {
88 if (token.rfind("-", 0) == 0) {
89 *flag_name = token;
90 return true;
91 }
92 return false;
93}
94
95} // namespace util
96} // namespace yaze
FlagRegistry * registry_
Definition flag.h:133
bool ExtractFlagAndValue(const std::string &token, std::string *flag_name, std::string *value_string)
Definition flag.cc:75
void Parse(int argc, char **argv)
Definition flag.h:119
bool ExtractFlag(const std::string &token, std::string *flag_name)
Definition flag.cc:87
virtual void ParseValue(const std::string &text)=0
FlagRegistry * global_flag_registry()
Definition flag.h:99
Main namespace for the application.
Definition controller.cc:18