|
| 1 | +/* |
| 2 | + sarge.cpp - Implementation file for the Sarge command line argument parser project. |
| 3 | + |
| 4 | + Revision 0 |
| 5 | + |
| 6 | + Features: |
| 7 | + - |
| 8 | + |
| 9 | + Notes: |
| 10 | + - |
| 11 | + |
| 12 | + 2019/03/16, Maya Posch |
| 13 | + |
| 14 | +*/ |
| 15 | + |
| 16 | + |
| 17 | +#include "sarge.h" |
| 18 | + |
| 19 | +#include <iostream> |
| 20 | + |
| 21 | + |
| 22 | +// --- SET ARGUMENT --- |
| 23 | +void Sarge::setArgument(std::string arg_short, std::string arg_long, std::string desc, bool hasVal) { |
| 24 | + Argument arg; |
| 25 | + arg.arg_short = arg_short; |
| 26 | + arg.arg_long = arg_long; |
| 27 | + arg.description = desc; |
| 28 | + arg.hasValue = hasVal; |
| 29 | + arg.parsed = false; |
| 30 | + args.push_back(std::move(arg)); |
| 31 | + |
| 32 | + // Set up links. |
| 33 | + if (!arg_short.empty()) { |
| 34 | + argNames.insert(std::pair<std::string, Argument*>(arg_short, &(args.back()))); |
| 35 | + } |
| 36 | + |
| 37 | + if (!arg_long.empty()) { |
| 38 | + argNames.insert(std::pair<std::string, Argument*>(arg_long, &(args.back()))); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | + |
| 43 | +// --- SET ARGUMENTS --- |
| 44 | +void Sarge::setArguments(std::vector<Argument> args) { |
| 45 | + for (Argument a : args) { |
| 46 | + setArgument(a.arg_short, a.arg_long, a.description, a.hasValue); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +// --- PARSE ARGUMENTS --- |
| 52 | +bool Sarge::parseArguments(int argc, char** argv) { |
| 53 | + // The first argument is the name of the executable. After it we loop through the remaining |
| 54 | + // arguments, linking flags and values. |
| 55 | + execName = std::string(argv[0]); |
| 56 | + bool expectValue = false; |
| 57 | + std::map<std::string, Argument*>::const_iterator flag_it; |
| 58 | + for (int i = 1; i < argc; ++i) { |
| 59 | + // Each flag will start with a '-' character. Multiple flags can be joined together in the |
| 60 | + // same string if they're the short form flag type (one character per flag). |
| 61 | + std::string entry(argv[i]); |
| 62 | + |
| 63 | + if (entry.compare(0, 1, "-") == 0) { |
| 64 | + // Parse flag. |
| 65 | + // First check for the long form. |
| 66 | + if (entry.compare(0, 2, "--") == 0) { |
| 67 | + // Long form of flag. |
| 68 | + entry.erase(0, 2); // Erase the double dash since we no longer need it. |
| 69 | + |
| 70 | + flag_it = argNames.find(entry); |
| 71 | + if (flag_it == argNames.end()) { |
| 72 | + // Flag wasn't found. Abort. |
| 73 | + std::cerr << "Long flag " << entry << " wasn't found." << std::endl; |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | + // Mark as found. |
| 78 | + flag_it->second->parsed = true; |
| 79 | + ++flagCounter; |
| 80 | + |
| 81 | + if (flag_it->second->hasValue) { |
| 82 | + expectValue = true; // Next argument has to be a value string. |
| 83 | + } |
| 84 | + } |
| 85 | + else { |
| 86 | + // Parse short form flag. Parse all of them sequentially. Only the last one |
| 87 | + // is allowed to have an additional value following it. |
| 88 | + entry.erase(0, 1); // Erase the dash. |
| 89 | + for (int i = 0; i < entry.length(); ++i) { |
| 90 | + std::string k(&(entry[i]), 1); |
| 91 | + flag_it = argNames.find(k); |
| 92 | + if (flag_it == argNames.end()) { |
| 93 | + // Flag wasn't found. Abort. |
| 94 | + std::cerr << "Short flag " << k << " wasn't found." << std::endl; |
| 95 | + return false; |
| 96 | + } |
| 97 | + |
| 98 | + // Mark as found. |
| 99 | + flag_it->second->parsed = true; |
| 100 | + ++flagCounter; |
| 101 | + |
| 102 | + if (i != (entry.length() - 1) && flag_it->second->hasValue) { |
| 103 | + // Flag isn't at end, thus cannot have value. |
| 104 | + std::cerr << "Flag " << k << " needs to be followed by a value string." |
| 105 | + << std::endl; |
| 106 | + return false; |
| 107 | + } |
| 108 | + else { |
| 109 | + expectValue = true; // Next argument has to be a value string. |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + else if (expectValue) { |
| 115 | + // Copy value. |
| 116 | + flag_it->second->value = entry; |
| 117 | + |
| 118 | + expectValue = false; |
| 119 | + } |
| 120 | + else { |
| 121 | + std::cerr << "Expected flag, not value." << std::endl; |
| 122 | + return false; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + parsed = true; |
| 127 | + |
| 128 | + return true; |
| 129 | +} |
| 130 | + |
| 131 | + |
| 132 | +// --- GET FLAG --- |
| 133 | +// Returns whether the flag was found, along with the value if relevant. |
| 134 | +bool Sarge::getFlag(std::string arg_flag, std::string &arg_value) { |
| 135 | + if (!parsed) { return false; } |
| 136 | + |
| 137 | + std::map<std::string, Argument*>::const_iterator it = argNames.find(arg_flag); |
| 138 | + if (it == argNames.end()) { return false; } |
| 139 | + if (!it->second->parsed) { return false; } |
| 140 | + |
| 141 | + if (it->second->hasValue) { |
| 142 | + arg_value = it->second->value; |
| 143 | + } |
| 144 | + |
| 145 | + return true; |
| 146 | +} |
| 147 | + |
| 148 | + |
| 149 | +// --- EXISTS --- |
| 150 | +// Returns whether the flag was found. |
| 151 | +bool Sarge::exists(std::string arg_flag) { |
| 152 | + if (!parsed) { return false; } |
| 153 | + |
| 154 | + std::map<std::string, Argument*>::const_iterator it = argNames.find(arg_flag); |
| 155 | + if (it == argNames.end()) { return false; } |
| 156 | + if (!it->second->parsed) { return false; } |
| 157 | + |
| 158 | + return true; |
| 159 | +} |
| 160 | + |
| 161 | + |
| 162 | +// --- PRINT HELP --- |
| 163 | +// Prints out the application description, usage and available options. |
| 164 | +void Sarge::printHelp() { |
| 165 | + std::cout << std::endl << description << std::endl; |
| 166 | + std::cout << std::endl << "Usage:" << std::endl; |
| 167 | + std::cout << "\t" << usage << std::endl; |
| 168 | + std::cout << std::endl; |
| 169 | + std::cout << "Options: " << std::endl; |
| 170 | + |
| 171 | + // Print out the options. |
| 172 | + for (Argument arg : args) { |
| 173 | + std::cout << "-" << arg.arg_short << "\t--" << arg.arg_long << "\t\t" << arg.description << std::endl; |
| 174 | + } |
| 175 | +} |
0 commit comments