-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtool_args_parser.hpp
More file actions
46 lines (38 loc) · 1.13 KB
/
Copy pathtool_args_parser.hpp
File metadata and controls
46 lines (38 loc) · 1.13 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
#pragma once
#include <nlohmann/json.hpp>
#include <string>
#include <optional>
namespace acecode {
// Unified argument parser for tool implementations
class ToolArgsParser {
public:
explicit ToolArgsParser(const std::string& json_str) {
try {
args_ = nlohmann::json::parse(json_str);
} catch (const nlohmann::json::parse_error& e) {
error_ = "Failed to parse tool arguments: " + std::string(e.what());
}
}
template<typename T>
std::optional<T> get(const std::string& key) const {
if (has_error() || !args_.contains(key)) {
return std::nullopt;
}
try {
return args_[key].get<T>();
} catch (...) {
return std::nullopt;
}
}
template<typename T>
T get_or(const std::string& key, const T& default_val) const {
auto result = get<T>(key);
return result.has_value() ? *result : default_val;
}
bool has_error() const { return !error_.empty(); }
std::string error() const { return error_; }
private:
nlohmann::json args_;
std::string error_;
};
} // namespace acecode