Repository navigation
Expand file tree
/
Copy pathCliParser.cpp
More file actions
242 lines (206 loc) · 8.08 KB
/
Copy pathCliParser.cpp
File metadata and controls
242 lines (206 loc) · 8.08 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include "CliParser.h"
#include <iostream>
namespace modrinth_cli::cli {
namespace {
constexpr char kDefaultStateFilename[] = ".modrinth-cli-update.json";
} // namespace
void CliParser::printUsage(const char* executable_name) {
std::cout
<< "Usage:\n"
<< " " << executable_name
<< " update [mods-path] [--game-version <version>] [--loader <loader>]"
" [--state <file>]\n"
<< " " << executable_name
<< " install --only-upgrade <name> [mods-path] [--state <file>]"
" [--game-version <version>] [--loader <loader>] [--dry-run]\n"
<< " " << executable_name
<< " install --fix-broken <name> [mods-path] [--state <file>]"
" [--game-version <version>] [--loader <loader>] [--dry-run]\n"
<< " " << executable_name << " init <mods-path>\n"
<< " " << executable_name << " upgrade [mods-path] [--state <file>] [--dry-run]\n"
<< " " << executable_name
<< " deps [mods-path] [--game-version <version>] [--loader <loader>]\n"
<< " " << executable_name
<< " list [--upgradable] [--disabled] [--unknown] [--client-only] [--server-only]"
" [--client-server] [--side-unknown] [mods-path] [--state <file>]\n"
<< " " << executable_name << " config [set-api-key <key> | show-api-key]\n\n"
<< "Run 'init' once per mod folder to save default game version and loader.\n"
<< "Update/deps will use saved values unless --game-version/--loader are provided.\n\n"
<< "If no mods-path is provided, the CLI checks the ./mods folder in the current "
"working directory.\n";
}
CommandLineOptions CliParser::parseArguments(int argc, char** argv) {
if (argc < 2) {
throw CliError("Missing command and mods path.");
}
CommandLineOptions options;
options.command = "update";
options.mods_path = std::filesystem::absolute("mods");
options.mods_path_provided = false;
options.game_version.clear();
options.loader.clear();
bool state_file_set = false;
int index = 1;
const std::string first_arg = argv[index];
if (first_arg == "update" || first_arg == "upgrade" || first_arg == "deps" ||
first_arg == "list" || first_arg == "init" || first_arg == "install" ||
first_arg == "config") {
options.command = first_arg;
++index;
} else {
throw CliError("Unknown command: " + first_arg);
}
// For config command, collect all remaining args
if (options.command == "config") {
while (index < argc) {
options.args.push_back(argv[index]);
++index;
}
return options;
}
while (index < argc) {
const std::string arg = argv[index];
if (arg == "--state") {
if (index + 1 >= argc) {
throw CliError("Expected a file path after --state.");
}
options.state_file = std::filesystem::absolute(argv[index + 1]);
state_file_set = true;
index += 2;
continue;
}
if (arg == "--game-version") {
if (index + 1 >= argc) {
throw CliError("Expected a value after --game-version.");
}
options.game_version = argv[index + 1];
options.game_version_provided = true;
index += 2;
continue;
}
if (arg == "--loader") {
if (index + 1 >= argc) {
throw CliError("Expected a value after --loader.");
}
options.loader = argv[index + 1];
options.loader_provided = true;
index += 2;
continue;
}
if (arg == "--dry-run") {
options.dry_run = true;
++index;
continue;
}
if (arg == "--only-upgrade") {
if (index + 1 >= argc) {
throw CliError("Expected a mod name after --only-upgrade.");
}
options.only_upgrade_name = argv[index + 1];
index += 2;
continue;
}
if (arg == "--fix-broken" || arg == "-f") {
if (index + 1 >= argc) {
throw CliError("Expected a mod name after --fix-broken.");
}
options.fix_broken = true;
options.fix_broken_name = argv[index + 1];
index += 2;
continue;
}
if (arg == "--upgradable") {
options.list_upgradable = true;
++index;
continue;
}
if (arg == "--disabled") {
options.list_disabled = true;
++index;
continue;
}
if (arg == "--unknown") {
options.list_unknown = true;
++index;
continue;
}
if (arg == "--client-only") {
options.list_client_only = true;
++index;
continue;
}
if (arg == "--server-only") {
options.list_server_only = true;
++index;
continue;
}
if (arg == "--client-server") {
options.list_client_server = true;
++index;
continue;
}
if (arg == "--side-unknown") {
options.list_side_unknown = true;
++index;
continue;
}
if (arg.starts_with("--")) {
throw CliError("Unknown argument: " + arg);
}
if (options.mods_path_provided) {
throw CliError("Unexpected positional argument: " + arg);
}
options.mods_path = std::filesystem::absolute(arg);
options.mods_path_provided = true;
++index;
}
if (!state_file_set) {
options.state_file = options.mods_path / kDefaultStateFilename;
}
if (options.command == "list") {
const bool has_side_filter = options.list_client_only || options.list_server_only ||
options.list_client_server || options.list_side_unknown;
if (!options.list_upgradable && !options.list_disabled && !options.list_unknown &&
has_side_filter) {
options.list_upgradable = true;
}
if (!options.list_upgradable && !options.list_disabled && !options.list_unknown) {
throw CliError(
"The list command requires at least one of: --upgradable, --disabled, --unknown.");
}
if (has_side_filter && (options.list_disabled || options.list_unknown)) {
throw CliError(
"--client-only, --server-only, --client-server, and --side-unknown can only "
"be used with upgradable Modrinth entries.");
}
} else {
if (options.list_upgradable || options.list_disabled || options.list_unknown ||
options.list_client_only || options.list_server_only || options.list_client_server ||
options.list_side_unknown) {
throw CliError(
"--upgradable, --disabled, --unknown, --client-only, --server-only, "
"--client-server, and --side-unknown are only supported with the list "
"command.");
}
}
if (options.command != "upgrade" && options.command != "install" && options.dry_run) {
throw CliError("--dry-run is only supported with the upgrade command.");
}
if (options.command == "init" && (options.game_version_provided || options.loader_provided)) {
throw CliError(
"The init command is interactive and does not accept --game-version or --loader.");
}
if (options.command == "install") {
const bool has_only_upgrade = !options.only_upgrade_name.empty();
const bool has_fix_broken = options.fix_broken;
if (has_only_upgrade == has_fix_broken) {
throw CliError(
"install requires exactly one of --only-upgrade <name> or --fix-broken <name>.");
}
} else if (!options.only_upgrade_name.empty() || options.fix_broken) {
throw CliError(
"--only-upgrade and --fix-broken are only supported with the install command.");
}
return options;
}
} // namespace modrinth_cli::cli