2#ifndef LIBSBX_CORE_CLI_HPP_
3#define LIBSBX_CORE_CLI_HPP_
15#include <fmt/format.h>
17#include <libsbx/utility/logger.hpp>
21template<
typename Type>
22concept 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>);
30 cli(std::span<std::string_view> args) {
31 for (
const auto& arg : args) {
32 if (arg.substr(0, 2) !=
"--") {
36 const auto pos = arg.find_first_of(
"=");
38 if (pos == std::string::npos) {
43 const auto key = std::string{arg.substr(2, pos - 2u)};
44 const auto value = std::string{arg.substr(pos + 1u)};
46 _arguments[key] = std::move(value);
50 template<argument Type>
51 auto argument(
const std::string& name)
const -> std::optional<Type> {
52 if (
auto entry = _arguments.find(name); entry != _arguments.end()) {
53 const auto& value = entry->second;
55 if constexpr (std::is_same_v<Type, bool>) {
56 if (value ==
"true") {
58 }
else if (value ==
"false") {
63 }
else if constexpr (std::is_floating_point_v<Type> || std::is_integral_v<Type>) {
66 auto [ptr, ec] = std::from_chars(value.data(), value.data() + value.size(), result);
68 if (ec == std::errc::invalid_argument || ec == std::errc::result_out_of_range) {
73 }
else if constexpr (std::is_same_v<Type, std::string>) {
83 auto _argument_value(
const std::string& name) -> std::optional<std::string> {
84 if (
auto entry = _arguments.find(name); entry != _arguments.end()) {
91 std::unordered_map<std::string, std::string> _arguments;
Definition: engine.hpp:32
Definition: logger.hpp:124