Line data Source code
1 : /* Copyright (C) 2022 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/containers/Span.h"
23 : #include "ps/CStr.h"
24 :
25 : #include <utility>
26 : #include <vector>
27 :
28 10 : class CmdLineArgs
29 : {
30 : public:
31 2 : CmdLineArgs() {}
32 :
33 : /**
34 : * Parse the command-line options, for future processing.
35 : * All arguments are required to be of the form <tt>-name</tt> or
36 : * <tt>-name=value</tt> - anything else is ignored.
37 : *
38 : * @param argv span of arguments; argv[0] should be the program's name
39 : */
40 : CmdLineArgs(const PS::span<const char* const> argv);
41 :
42 : /**
43 : * Test whether the given name was specified, as either <tt>-name</tt> or
44 : * <tt>-name=value</tt>
45 : */
46 : bool Has(const CStr& name) const;
47 :
48 : /**
49 : * Get the value of the named parameter. If it was not specified, returns
50 : * the empty string. If it was specified multiple times, returns the value
51 : * from the first occurrence.
52 : */
53 : CStr Get(const CStr& name) const;
54 :
55 : /**
56 : * Get all the values given to the named parameter. Returns values in the
57 : * same order as they were given in argv.
58 : */
59 : std::vector<CStr> GetMultiple(const CStr& name) const;
60 :
61 : /**
62 : * Get the value of argv[0], which is typically meant to be the name/path of
63 : * the program (but the actual value is up to whoever executed the program).
64 : */
65 : OsPath GetArg0() const;
66 :
67 : /**
68 : * Returns all arguments that don't have a name (string started with '-').
69 : */
70 : std::vector<CStr> GetArgsWithoutName() const;
71 :
72 : private:
73 : typedef std::vector<std::pair<CStr, CStr> > ArgsT;
74 : ArgsT m_Args;
75 : OsPath m_Arg0;
76 : std::vector<CStr> m_ArgsWithoutName;
77 : };
78 :
79 : extern CmdLineArgs g_CmdLineArgs;
80 :
81 : #endif // INCLUDED_CMDLINEARGS
|