Repository navigation
Expand file tree
/
Copy pathargs.cpp
More file actions
202 lines (179 loc) · 5.71 KB
/
Copy pathargs.cpp
File metadata and controls
202 lines (179 loc) · 5.71 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
#include "args.h"
#include <iostream>
#include <set>
#include <stdexcept>
namespace minitts::cli {
namespace {
// Every option name the CLI looked for, split by whether the lookup consumes the following
// argument. Populated by the lookups below, so a new flag is recognised the moment its lookup
// is added and there is no second list to keep in sync.
std::set<std::string> & value_names() {
static std::set<std::string> names;
return names;
}
std::set<std::string> & flag_names() {
static std::set<std::string> names;
return names;
}
void record_value_query(const std::string & name) {
value_names().insert(name);
}
void record_flag_query(const std::string & name) {
flag_names().insert(name);
}
std::vector<std::string> args_nobody_asked_for(int argc, char ** argv) {
const auto & values = value_names();
const auto & flags = flag_names();
std::vector<std::string> unknown;
for (int i = 1; i < argc; ++i) {
const std::string token = argv[i];
if (token.rfind("--", 0) != 0 || token == "--") {
continue;
}
// Only an option that takes a value consumes the next argument, so only then can the
// next argument be something that merely looks like an option.
if (values.count(token) != 0) {
++i;
continue;
}
if (flags.count(token) != 0) {
continue;
}
unknown.push_back(token);
}
return unknown;
}
// Set once the strict check has spoken for this run, so the warning below does not repeat it.
bool strict_check_ran = false;
} // namespace
void require_known_args(int argc, char ** argv) {
strict_check_ran = true;
const auto unknown = args_nobody_asked_for(argc, argv);
if (unknown.empty()) {
return;
}
std::string message = unknown.size() > 1 ? "unknown options:" : "unknown option:";
for (const auto & token : unknown) {
message += " " + token;
}
throw std::runtime_error(message);
}
void warn_ignored_args(int argc, char ** argv) {
if (strict_check_ran) {
return;
}
const auto ignored = args_nobody_asked_for(argc, argv);
if (ignored.empty()) {
return;
}
std::cerr << "audiocpp_cli warning: ignored option";
if (ignored.size() > 1) {
std::cerr << "s";
}
std::cerr << ":";
for (const auto & token : ignored) {
std::cerr << " " << token;
}
std::cerr << "\n";
}
std::optional<std::string> find_arg(int argc, char ** argv, const std::string & name) {
record_value_query(name);
for (int i = 1; i + 1 < argc; ++i) {
if (argv[i] == name) {
return std::string(argv[i + 1]);
}
}
return std::nullopt;
}
bool has_arg(int argc, char ** argv, const std::string & name) {
record_flag_query(name);
for (int i = 1; i < argc; ++i) {
if (argv[i] == name) {
return true;
}
}
return false;
}
std::vector<std::string> collect_args(int argc, char ** argv, const std::string & name) {
record_value_query(name);
std::vector<std::string> values;
for (int i = 1; i + 1 < argc; ++i) {
if (argv[i] == name) {
values.emplace_back(argv[i + 1]);
}
}
return values;
}
std::unordered_map<std::string, std::string> collect_key_value_args(
int argc,
char ** argv,
const std::string & name) {
std::unordered_map<std::string, std::string> values;
for (const auto & item : collect_args(argc, argv, name)) {
const auto pos = item.find('=');
if (pos == std::string::npos || pos == 0 || pos + 1 >= item.size()) {
throw std::runtime_error("expected " + name + " key=value, got: " + item);
}
values[item.substr(0, pos)] = item.substr(pos + 1);
}
return values;
}
void set_option(
std::unordered_map<std::string, std::string> & options,
const std::string & key,
const std::string & value) {
const auto [it, inserted] = options.emplace(key, value);
if (!inserted && it->second != value) {
throw std::runtime_error("conflicting request option value for " + key);
}
}
void set_option_from_arg(
int argc,
char ** argv,
const std::string & arg_name,
const std::string & option_key,
std::unordered_map<std::string, std::string> & options) {
if (const auto value = find_arg(argc, argv, arg_name)) {
set_option(options, option_key, *value);
}
}
int parse_int_arg(int argc, char ** argv, const std::string & name, int fallback) {
if (const auto value = find_arg(argc, argv, name)) {
return std::stoi(*value);
}
return fallback;
}
std::optional<float> parse_optional_float_arg(int argc, char ** argv, const std::string & name) {
if (const auto value = find_arg(argc, argv, name)) {
return std::stof(*value);
}
return std::nullopt;
}
std::optional<std::filesystem::path> optional_path_arg(int argc, char ** argv, const std::string & name) {
if (const auto value = find_arg(argc, argv, name)) {
return std::filesystem::path(*value);
}
return std::nullopt;
}
engine::core::BackendType parse_backend(const std::string & value) {
if (value == "cpu") {
return engine::core::BackendType::Cpu;
}
if (value == "cuda") {
return engine::core::BackendType::Cuda;
}
if (value == "hip" || value == "rocm") {
return engine::core::BackendType::Hip;
}
if (value == "vulkan") {
return engine::core::BackendType::Vulkan;
}
if (value == "metal") {
return engine::core::BackendType::Metal;
}
if (value == "best") {
return engine::core::BackendType::BestAvailable;
}
throw std::runtime_error("unsupported backend: " + value);
}
} // namespace minitts::cli