Repository navigation
Expand file tree
/
Copy pathargs.cpp
More file actions
111 lines (98 loc) · 3.17 KB
/
Copy pathargs.cpp
File metadata and controls
111 lines (98 loc) · 3.17 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
#include "args.h"
#include <stdexcept>
namespace minitts::cli {
std::optional<std::string> find_arg(int argc, char ** argv, const std::string & 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) {
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) {
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 == "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