/
commandline.h
221 lines (187 loc) · 6.34 KB
/
commandline.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
* The Doomsday Engine Project -- libdeng2
*
* Copyright © 2009-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#ifndef LIBDENG2_COMMANDLINE_H
#define LIBDENG2_COMMANDLINE_H
#include <string>
#include <vector>
#include <map>
#include <QStringList>
#include "../libdeng2.h"
#include "../String"
#include "../NativePath"
namespace de {
/**
* Stores and provides access to the command line arguments passed
* to an application at launch.
*
* @ingroup core
*/
class DENG2_PUBLIC CommandLine
{
public:
/// Tried to access an argument that does not exist. @ingroup errors
DENG2_ERROR(OutOfRangeError);
/// Execution of the command line failed. @ingroup errors
DENG2_ERROR(ExecuteError);
struct ArgWithParams {
dint pos; ///< Position of the argument.
String arg;
QList<String> params;
ArgWithParams() : pos(0) {}
operator dint () const { return pos; }
dint size() const { return params.size(); }
};
public:
CommandLine();
/**
* Constructs a CommandLine out of a list of strings. The argument
* strings that begin with a @@ character are parsed as response files
* the rest are used without modification.
*
* @param args Arguments to use.
*/
CommandLine(QStringList const &args);
CommandLine(CommandLine const &other);
/**
* Returns the native path where the command line was started in.
* @return Native startup location.
*/
NativePath startupPath();
/**
* Returns the number of arguments. This includes the program name, which
* is the first argument in the list.
*/
dint count() const;
void clear();
/**
* Appends a new argument to the list of arguments.
*
* @param arg Argument to append.
*/
void append(String const &arg);
/**
* Inserts a new argument to the list of arguments at index @a pos.
*
* @param pos Index at which the new argument will be at.
* @param arg Argument to insert.
*/
void insert(duint pos, String const &arg);
/**
* Removes an argument by index.
*
* @param pos Index of argument to remove.
*/
void remove(duint pos);
/**
* Checks whether @a arg is in the arguments. Since the first argument is
* the program name, it is not included in the search.
*
* @param arg Argument to look for. Don't use aliases here.
* @param count Number of parameters (non-option arguments) that follow
* the located argument.
*
* @see isOption()
*
* @return Index of the argument, if found. Otherwise zero.
*/
ArgWithParams check(String const &arg, dint count = 0) const;
/**
* Gets the parameter for an argument.
*
* @param arg Argument to look for. Don't use aliases here. Defines
* aliases will be checked for matches.
* @param param The parameter is returned here, if found.
*
* @return @c true, if parameter was successfully returned.
* Otherwise @c false, in which case @a param is not modified.
*/
bool getParameter(String const &arg, String ¶m) const;
/**
* Determines whether @a arg exists in the list of arguments.
*
* @param arg Argument to look for. Don't use aliases here.
*
* @return Number of times @a arg is found in the arguments.
*/
dint has(String const &arg) const;
/**
* Determines whether an argument is an option, i.e., it begins with a hyphen.
*/
bool isOption(duint pos) const;
/**
* Determines whether an argument is an option, i.e., it begins with a hyphen.
*/
static bool isOption(String const &arg);
String at(duint pos) const;
/**
* Returns a list of pointers to the arguments. The list contains
* count() strings and is NULL-terminated.
*/
char const *const *argv() const;
/**
* Converts the argument at position @a pos into an absolute native path.
* Relative paths are converted relative to the directory that was
* current at the time the CommandLine was created.
*
* @param pos Argument index.
*/
void makeAbsolutePath(duint pos);
/**
* Reads a native file and parses its contents using parse().
*
* @param nativePath File to parse.
*/
void parseResponseFile(NativePath const &nativePath);
/**
* Breaks down a single string containing arguments.
*
* Examples of behavior:
* - -cmd "echo ""this is a command""" => [-cmd] [echo "this is a command"]
* - Hello" My"Friend => [Hello MyFriend]
* - @@test.rsp [reads contents of test.rsp]
* - <tt>@@\\"Program Files"\\test.rsp</tt> [reads contents of <tt>"\Program Files\test.rsp"</tt>]
*
* @param cmdLine String containing the arguments.
*/
void parse(String const &cmdLine);
/**
* Defines a new alias for a full argument.
*
* @param full The full argument, e.g., "--help"
* @param alias Alias for the full argument, e.g., "-h"
*/
void alias(String const &full, String const &alias);
/**
* @return @c true, iff the two parameters are equivalent according to
* the abbreviations.
*/
bool matches(String const &full, String const &fullOrAlias) const;
/**
* Spawns a new process using the command line. The first argument
* specifies the file name of the executable. Returns immediately
* after the process has been started.
*
* @return @c true if successful, otherwise @c false.
*/
bool execute() const;
private:
DENG2_PRIVATE(d)
};
} // namespace de
#endif /* LIBDENG2_COMMANDLINE_H */