MCPcopy Create free account
hub / github.com/MayaPosch/NymphCast / parseArguments

Method parseArguments

src/server/sarge.cpp:51–132  ·  view source on GitHub ↗

--- PARSE ARGUMENTS ---

Source from the content-addressed store, hash-verified

49
50// --- PARSE ARGUMENTS ---
51bool Sarge::parseArguments(int argc, char** argv) {
52 // The first argument is the name of the executable. After it we loop through the remaining
53 // arguments, linking flags and values.
54 execName = std::string(argv[0]);
55 bool expectValue = false;
56 std::map<std::string, Argument*>::const_iterator flag_it;
57 for (int i = 1; i < argc; ++i) {
58 // Each flag will start with a '-' character. Multiple flags can be joined together in the
59 // same string if they're the short form flag type (one character per flag).
60 std::string entry(argv[i]);
61
62 if (expectValue) {
63 // Copy value.
64 flag_it->second->value = entry;
65 expectValue = false;
66 }
67 else if (entry.compare(0, 1, "-") == 0) {
68 if (textArguments.size() > 0) {
69 std::cerr << "Flags not allowed after text arguments." << std::endl;
70 }
71
72 // Parse flag.
73 // First check for the long form.
74 if (entry.compare(0, 2, "--") == 0) {
75 // Long form of flag.
76 entry.erase(0, 2); // Erase the double dash since we no longer need it.
77
78 flag_it = argNames.find(entry);
79 if (flag_it == argNames.end()) {
80 // Flag wasn't found. Abort.
81 std::cerr << "Long flag " << entry << " wasn't found." << std::endl;
82 return false;
83 }
84
85 // Mark as found.
86 flag_it->second->parsed = true;
87 ++flagCounter;
88
89 if (flag_it->second->hasValue) {
90 expectValue = true; // Next argument has to be a value string.
91 }
92 }
93 else {
94 // Parse short form flag. Parse all of them sequentially. Only the last one
95 // is allowed to have an additional value following it.
96 entry.erase(0, 1); // Erase the dash.
97 for (int i = 0; i < entry.length(); ++i) {
98 std::string k(&(entry[i]), 1);
99 flag_it = argNames.find(k);
100 if (flag_it == argNames.end()) {
101 // Flag wasn't found. Abort.
102 std::cerr << "Short flag " << k << " wasn't found." << std::endl;
103 return false;
104 }
105
106 // Mark as found.
107 flag_it->second->parsed = true;
108 ++flagCounter;

Callers 1

mainFunction · 0.45

Calls 5

lengthMethod · 0.80
push_backMethod · 0.80
sizeMethod · 0.45
findMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected