config.cc (1819B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 #include "config.h" 7 8 #include <cstdlib> 9 #include <queue> 10 #include <string> 11 12 bool ConfigEntryBase::ParseInternal(std::queue<const char *> &args, 13 std::vector<int> &out) { 14 if (args.empty()) return false; 15 16 char *endptr; 17 out.push_back(strtol(args.front(), &endptr, 10)); 18 args.pop(); 19 20 return !*endptr; 21 } 22 23 bool ConfigEntryBase::ParseInternal(std::queue<const char *> &args, 24 std::string &out) { 25 if (args.empty()) return false; 26 out = args.front(); 27 args.pop(); 28 return true; 29 } 30 31 bool ConfigEntryBase::ParseInternal(std::queue<const char *> &args, int &out) { 32 if (args.empty()) return false; 33 34 char *endptr; 35 out = strtol(args.front(), &endptr, 10); 36 args.pop(); 37 38 return !*endptr; 39 } 40 41 bool ConfigEntryBase::ParseInternal(std::queue<const char *> &args, bool &out) { 42 out = true; 43 return true; 44 } 45 46 std::string Config::XformFlag(const std::string &arg) { 47 if (arg.empty()) return ""; 48 49 if (arg[0] != '-') return ""; 50 51 return arg.substr(1); 52 } 53 54 Config::Status Config::ParseArgs(int argc, char **argv) { 55 std::queue<const char *> args; 56 for (int i = 1; i < argc; ++i) { 57 args.push(argv[i]); 58 } 59 while (!args.empty()) { 60 auto e = entries_.find(XformFlag(args.front())); 61 if (e == entries_.end()) { 62 std::cerr << "Unimplemented shim flag: " << args.front() << std::endl; 63 return kUnknownFlag; 64 } 65 args.pop(); 66 if (!e->second->Parse(args)) return kMalformedArgument; 67 } 68 69 return kOK; 70 }