From 174b46ae94514fc7f6f442af67a7c36bf89710b3 Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Wed, 12 Feb 2025 22:17:04 -0500 Subject: [PATCH] path_helper file provides base_name of a path. Argparse working on --- CMakeLists.txt | 2 +- include/blt/compatibility.h | 8 +++ include/blt/fs/path_helper.h | 33 +++++++++ include/blt/parse/argparse_v2.h | 116 ++++++++++++++++++++++---------- src/blt/fs/path_helper.cpp | 40 +++++++++++ 5 files changed, 161 insertions(+), 38 deletions(-) create mode 100644 include/blt/fs/path_helper.h create mode 100644 src/blt/fs/path_helper.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 5863b5a..2176d00 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.20) include(cmake/color.cmake) -set(BLT_VERSION 4.0.1) +set(BLT_VERSION 4.0.2) set(BLT_TARGET BLT) diff --git a/include/blt/compatibility.h b/include/blt/compatibility.h index b10d4f6..a3dfc45 100644 --- a/include/blt/compatibility.h +++ b/include/blt/compatibility.h @@ -51,4 +51,12 @@ #error Filesystem ops not supported!\ #endif +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) + #define BLT_OS_WINDOWS +#elif defined(__linux__) || defined(__unix__) + #define BLT_OS_LINUX +#else + #define BLT_OS_UNKNOWN +#endif + #endif //BLT_COMPATIBILITY_H diff --git a/include/blt/fs/path_helper.h b/include/blt/fs/path_helper.h new file mode 100644 index 0000000..530eb1a --- /dev/null +++ b/include/blt/fs/path_helper.h @@ -0,0 +1,33 @@ +#pragma once +/* + * Copyright (C) 2024 Brett Terpstra + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef BLT_FS_PATH_HELPER_H +#define BLT_FS_PATH_HELPER_H + +#include +#include + +namespace blt::fs +{ + + std::string base_name(const std::string& str); + std::string_view base_name_sv(std::string_view str); + +} + +#endif //BLT_FS_PATH_HELPER_H diff --git a/include/blt/parse/argparse_v2.h b/include/blt/parse/argparse_v2.h index d9bfa37..d047e69 100644 --- a/include/blt/parse/argparse_v2.h +++ b/include/blt/parse/argparse_v2.h @@ -21,11 +21,13 @@ #include #include +#include #include #include #include #include #include +#include #include #include #include @@ -33,6 +35,13 @@ namespace blt::argparse { + class argument_string_t; + class argument_consumer_t; + class argument_parser_t; + class argument_subparser_t; + class parsed_argset_t; + class argument_builder_t; + namespace detail { inline hashset_t allowed_flag_prefixes = {"-", "--", "+"}; @@ -73,34 +82,24 @@ namespace blt::argparse { static T convert(const std::string_view value) { + static_assert(std::is_arithmetic_v, "Type must be arithmetic!"); const std::string temp{value}; - if constexpr (std::is_same_v || std::is_same_v || std::is_same_v) - { - return static_cast(std::stoi(temp)); - } - else if constexpr (std::is_same_v) - { - return static_cast(std::stoll(temp)); - } - else if constexpr (std::is_same_v || std::is_same_v || std::is_same_v) - { - return static_cast(std::stoul(temp)); - } - else if constexpr (std::is_same_v) - { - return static_cast(std::stoull(temp)); - } - else if constexpr (std::is_same_v) + if constexpr (std::is_same_v) { return std::stof(temp); } else if constexpr (std::is_same_v) { return std::stod(temp); - } else + } + else if constexpr (std::is_unsigned_v) { - static_assert(std::is_arithmetic_v, "Unsupported type for this specialization"); + return static_cast(std::stoull(temp)); + } + else if constexpr (std::is_signed_v) + { + return static_cast(std::stoll(temp)); } BLT_UNREACHABLE; } @@ -120,16 +119,16 @@ namespace blt::argparse [[nodiscard]] std::string_view get_flag() const { - if (!flag_section) + if (!m_flag_section) process_argument(); - return *flag_section; + return *m_flag_section; } [[nodiscard]] std::string_view get_name() const { - if (!name_section) + if (!m_name_section) process_argument(); - return *name_section; + return *m_name_section; } [[nodiscard]] std::string_view value() const @@ -162,56 +161,99 @@ namespace blt::argparse } } m_is_flag = (start != 0); - flag_section = {m_argument.data(), start}; - name_section = {m_argument.data() + start, m_argument.size() - start}; + m_flag_section = {m_argument.data(), start}; + m_name_section = {m_argument.data() + start, m_argument.size() - start}; - if (!flag_section->empty() && !detail::allowed_flag_prefixes.contains(*flag_section)) + if (!m_flag_section->empty() && !detail::allowed_flag_prefixes.contains(*m_flag_section)) throw detail::bad_flag( - "Invalid flag " + std::string(*flag_section) + " detected, flag is not in allowed list of flags! Must be one of " + + "Invalid flag " + std::string(*m_flag_section) + " detected, flag is not in allowed list of flags! Must be one of " + detail::flag_prefix_list_string); } std::string_view m_argument; mutable std::optional m_is_flag; - mutable std::optional flag_section; - mutable std::optional name_section; + mutable std::optional m_flag_section; + mutable std::optional m_name_section; }; class argument_consumer_t { public: - explicit argument_consumer_t(const span& args): args(args) + explicit argument_consumer_t(const span& args): m_args(args) { } [[nodiscard]] argument_string_t peek(const i32 offset = 0) const { - return args[forward_index + offset]; + return m_args[m_forward_index + offset]; } argument_string_t consume() { - return args[forward_index++]; + return m_args[m_forward_index++]; } [[nodiscard]] i32 position() const { - return forward_index; + return m_forward_index; } [[nodiscard]] i32 remaining() const { - return static_cast(args.size()) - forward_index; + return static_cast(m_args.size()) - m_forward_index; } [[nodiscard]] bool has_next(const i32 offset = 0) const { - return (offset + forward_index) < args.size(); + return (offset + m_forward_index) < m_args.size(); } private: - span args; - i32 forward_index = 0; + span m_args; + i32 m_forward_index = 0; + }; + + class argument_builder_t + { + public: + + private: + }; + + class parsed_argset_t + { + public: + + private: + }; + + class argument_parser_t + { + public: + argument_parser_t(const std::optional name = {}, const std::optional usage = {}): + m_name(name.value_or(std::optional{})), m_usage(usage.value_or(std::optional{})) + { + } + + argument_parser_t& set_name(const std::string_view name) + { + m_name = name; + return *this; + } + + private: + std::optional m_name; + std::optional m_usage; + std::optional m_description; + std::optional m_epilogue; + }; + + class argument_subparser_t + { + public: + + private: + }; } diff --git a/src/blt/fs/path_helper.cpp b/src/blt/fs/path_helper.cpp new file mode 100644 index 0000000..71efd5c --- /dev/null +++ b/src/blt/fs/path_helper.cpp @@ -0,0 +1,40 @@ +/* + * + * Copyright (C) 2025 Brett Terpstra + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include +#include +#include + +namespace blt::fs +{ +#ifdef BLT_WINDOWS + constexpr static char delim = '\\'; +#else + constexpr static char delim = '/'; +#endif + + std::string base_name(const std::string& str) + { + return std::string(base_name_sv(str)); + } + + std::string_view base_name_sv(const std::string_view str) + { + const auto parts = string::split_sv(str, delim); + return parts.back(); + } +}