Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
CmdLineArgs.h
Go to the documentation of this file.
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
28namespace PS { template <typename T> class span; }
29
31{
32public:
33 using ArgsT = std::vector<std::pair<CStr, CStr>>;
34
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
81private:
84 std::vector<CStr> m_ArgsWithoutName;
85};
86
88
89#endif // INCLUDED_CMDLINEARGS
CmdLineArgs g_CmdLineArgs
Definition: CmdLineArgs.cpp:35
Definition: CmdLineArgs.h:31
std::vector< CStr > GetMultiple(const CStr &name) const
Get all the values given to the named parameter.
Definition: CmdLineArgs.cpp:102
std::vector< CStr > GetArgsWithoutName() const
Returns all arguments that don't have a name (string started with '-').
Definition: CmdLineArgs.cpp:125
CStr Get(const CStr &name) const
Get the value of the named parameter.
Definition: CmdLineArgs.cpp:96
bool Has(const CStr &name) const
Test whether the given name was specified, as either -name or -name=value
Definition: CmdLineArgs.cpp:91
const ArgsT & GetArgs() const
Returns all arguments that don't have a name (string started with '-').
Definition: CmdLineArgs.cpp:120
CmdLineArgs()
Definition: CmdLineArgs.h:35
std::vector< CStr > m_ArgsWithoutName
Definition: CmdLineArgs.h:84
OsPath GetArg0() const
Get the value of argv[0], which is typically meant to be the name/path of the program (but the actual...
Definition: CmdLineArgs.cpp:115
std::vector< std::pair< CStr, CStr > > ArgsT
Definition: CmdLineArgs.h:33
ArgsT m_Args
Definition: CmdLineArgs.h:82
OsPath m_Arg0
Definition: CmdLineArgs.h:83
Definition: CmdLineArgs.h:28
Definition: path.h:84
Definition: GUIManager.h:45