| 1 | /* |
| 2 | RailControl - Model Railway Control Software |
| 3 | |
| 4 | Copyright (c) 2017-2025 by Teddy / Dominik Mahrer - www.railcontrol.org |
| 5 | |
| 6 | RailControl is free software; you can redistribute it and/or modify it |
| 7 | under the terms of the GNU General Public License as published by the |
| 8 | Free Software Foundation; either version 3, or (at your option) any |
| 9 | later version. |
| 10 | |
| 11 | RailControl is distributed in the hope that it will be useful, but |
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 14 | See the GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with RailControl; see the file LICENCE. If not see |
| 18 | <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include <deque> |
| 22 | #include <iostream> |
| 23 | |
| 24 | #include "ArgumentHandler.h" |
| 25 | #include "Utils/Utils.h" |
| 26 | |
| 27 | ArgumentHandler::ArgumentHandler(const int argc, char* argv[], const std::map<std::string,char>& linkMap, const char defaultSwitch) |
| 28 | { |
| 29 | for (int i = 1; i < argc; ++i) |
| 30 | { |
| 31 | char* arg = argv[i]; |
| 32 | if (arg[0] != '-') |
| 33 | { |
| 34 | argumentMap[defaultSwitch] = arg; |
| 35 | continue; |
| 36 | } |
| 37 | |
| 38 | if (arg[1] != '-') |
| 39 | { |
| 40 | argumentMap[arg[1]] = ""; |
| 41 | continue; |
| 42 | } |
| 43 | |
| 44 | std::string argString = arg + 2; |
| 45 | std::deque<std::string> parts; |
| 46 | Utils::Utils::SplitString(argString, "=", parts); |
| 47 | if (parts.size() < 1) |
| 48 | { |
| 49 | continue; |
| 50 | } |
| 51 | std::string& argumentLong = parts[0]; |
| 52 | if (linkMap.count(argumentLong) == 0) |
| 53 | { |
| 54 | std::cout << "Unknown argument " << argumentLong << std::endl; |
| 55 | continue; |
| 56 | } |
| 57 | char argumentShort = linkMap.at(argumentLong); |
| 58 | argumentMap[argumentShort] = (parts.size() == 1 ? "" : parts[1]); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | std::string ArgumentHandler::GetArgumentString(const char argument, const std::string& defaultValue) |
| 63 | { |
| 64 | if (GetArgumentBool(argument) == false) |
| 65 | { |
| 66 | return defaultValue; |
| 67 | } |
| 68 | return argumentMap.at(argument); |
| 69 | } |
| 70 |