Line data Source code
1 : /* Copyright (C) 2025 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #ifndef INCLUDED_CMDLINEARGS 19 : #define INCLUDED_CMDLINEARGS 20 : 21 : #include "lib/os_path.h" 22 : #include "ps/CStr.h" 23 : 24 : #include <span> 25 : #include <utility> 26 : #include <vector> 27 : 28 : namespace PS { template <typename T> class span; } 29 : 30 : class CmdLineArgs 31 : { 32 : public: 33 : using ArgsT = std::vector<std::pair<CStr, CStr>>; 34 : 35 2 : CmdLineArgs() {} 36 : 37 : /** 38 : * Parse the command-line options, for future processing. 39 : * All arguments are required to be of the form <tt>-name</tt> or 40 : * <tt>-name=value</tt> - anything else is ignored. 41 : * 42 : * @param argv span of arguments; argv[0] should be the program's name 43 : */ 44 : CmdLineArgs(const std::span<const char* const> argv); 45 : 46 : /** 47 : * Test whether the given name was specified, as either <tt>-name</tt> or 48 : * <tt>-name=value</tt> 49 : */ 50 : bool Has(const CStr& name) const; 51 : 52 : /** 53 : * Get the value of the named parameter. If it was not specified, returns 54 : * the empty string. If it was specified multiple times, returns the value 55 : * from the first occurrence. 56 : */ 57 : CStr Get(const CStr& name) const; 58 : 59 : /** 60 : * Get all the values given to the named parameter. Returns values in the 61 : * same order as they were given in argv. 62 : */ 63 : std::vector<CStr> GetMultiple(const CStr& name) const; 64 : 65 : /** 66 : * Get the value of argv[0], which is typically meant to be the name/path of 67 : * the program (but the actual value is up to whoever executed the program). 68 : */ 69 : OsPath GetArg0() const; 70 : 71 : /** 72 : * Returns all arguments that don't have a name (string started with '-'). 73 : */ 74 : const ArgsT& GetArgs() const; 75 : 76 : /** 77 : * Returns all arguments that don't have a name (string started with '-'). 78 : */ 79 : std::vector<CStr> GetArgsWithoutName() const; 80 : 81 : private: 82 : ArgsT m_Args; 83 : OsPath m_Arg0; 84 : std::vector<CStr> m_ArgsWithoutName; 85 : }; 86 : 87 : extern CmdLineArgs g_CmdLineArgs; 88 : 89 : #endif // INCLUDED_CMDLINEARGS