1#ifndef LIBSBX_CORE_CLI_HPP_
2#define LIBSBX_CORE_CLI_HPP_
14#include <fmt/format.h>
20template<
typename Type>
21concept argument = (std::is_same_v<Type, bool> || std::is_integral_v<Type> || std::is_floating_point_v<Type> || std::is_same_v<Type, std::string>);
29 cli(std::span<std::string_view> args) {
30 for (
const auto& arg : args) {
31 if (arg.substr(0, 2) !=
"--") {
35 const auto pos = arg.find_first_of(
"=");
37 if (pos == std::string::npos) {
38 logger::warn(
"Could not parse argument: '{}'", arg);
42 const auto key = std::string{arg.substr(2, pos - 2u)};
43 const auto value = std::string{arg.substr(pos + 1u)};
45 _arguments[key] = std::move(value);
49 template<argument Type>
50 auto argument(
const std::string& name)
const -> std::optional<Type> {
51 if (
auto entry = _arguments.find(name); entry != _arguments.end()) {
52 const auto& value = entry->second;
54 if constexpr (std::is_same_v<Type, bool>) {
55 if (value ==
"true") {
57 }
else if (value ==
"false") {
62 }
else if constexpr (std::is_floating_point_v<Type> || std::is_integral_v<Type>) {
65 auto [ptr, ec] = std::from_chars(value.data(), value.data() + value.size(), result);
67 if (ec == std::errc::invalid_argument || ec == std::errc::result_out_of_range) {
72 }
else if constexpr (std::is_same_v<Type, std::string>) {
82 auto _argument_value(
const std::string& name) -> std::optional<std::string> {
83 if (
auto entry = _arguments.find(name); entry != _arguments.end()) {
90 std::unordered_map<std::string, std::string> _arguments;
Definition: engine.hpp:27