From ecd3d1a7010634764257af9491cdcf4fb2774e5b Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Fri, 28 Feb 2025 18:08:20 -0500 Subject: [PATCH] i think argparse is done --- CMakeLists.txt | 2 +- include/blt/parse/argparse_v2.h | 2 +- src/blt/parse/argparse_v2.cpp | 92 +++++++++++++++++++++++++++++++-- 3 files changed, 89 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f51e587..227960f 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.36) +set(BLT_VERSION 4.0.37) set(BLT_TARGET BLT) diff --git a/include/blt/parse/argparse_v2.h b/include/blt/parse/argparse_v2.h index 88713b4..3ff1fc5 100644 --- a/include/blt/parse/argparse_v2.h +++ b/include/blt/parse/argparse_v2.h @@ -623,7 +623,7 @@ namespace blt::argparse argument_parser_t& with_help() { - add_flag("--help", "-h").set_action(action_t::HELP); + add_flag("--help", "-h").set_action(action_t::HELP).set_help("Show this help menu and exit"); return *this; } diff --git a/src/blt/parse/argparse_v2.cpp b/src/blt/parse/argparse_v2.cpp index 309a9b2..d4e2104 100644 --- a/src/blt/parse/argparse_v2.cpp +++ b/src/blt/parse/argparse_v2.cpp @@ -405,20 +405,86 @@ namespace blt::argparse help.newline(); for (const auto& [key, value] : m_subparsers) { - help += '\t'; auto map = value.get_allowed_strings(); - // TODO: make an unzip? - for (const auto& [parser, strings] : map) + help += '\t'; + help += key; + help += ": {"; + for (const auto& [i, parser, strings] : enumerate(map).flatten()) { + if (strings.size() > 1) + help += '['; for (const auto& [i, str] : enumerate(strings)) { help += str; if (i != strings.size() - 1) help += ", "; } - // help += parser.he - help.newline(); + if (strings.size() > 1) + help += ']'; + if (i != map.size() - 1) + help += ", "; } + help += "}"; + help.newline(); + } + help.newline(); + } + + if (!m_positional_arguments.empty()) + { + help += "Positional Arguments:"; + help.newline(); + for (auto& [name, builder] : m_positional_arguments) + { + help += '\t'; + if (!builder.m_required) + help += '['; + help += name; + if (!builder.m_required) + help += ']'; + if (builder.m_default_value && !(builder.m_action == action_t::STORE_TRUE || builder.m_action == action_t::STORE_FALSE)) + { + if (!std::isblank(help.str().back())) + help += " "; + help += "(Default: "; + std::visit(detail::arg_meta_type_helper_t::make_visitor( + [&](auto& value) + { + help += value; + }, + [&](auto& vec) + { + if constexpr (!std::is_same_v>, std::vector>) + { + help += '['; + for (const auto& [i, v] : enumerate(vec)) + { + help += v; + if (i != vec.size() - 1) + help += ", "; + } + help += ']'; + } + }), *builder.m_default_value); + help += ")"; + } + if (builder.m_choices) + { + if (!std::isblank(help.str().back())) + help += " "; + help += "(Choices: "; + for (const auto& [i, v] : enumerate(*builder.m_choices)) + { + help += '\''; + help += v; + help += '\''; + if (i != builder.m_choices->size() - 1) + help += ", "; + } + help += ')'; + } + help.newline(); + help.newline(); } } @@ -514,12 +580,20 @@ namespace blt::argparse str += "(Choices: "; for (const auto& [i, v] : enumerate(*builder->m_choices)) { + str += '\''; str += v; + str += '\''; if (i != builder->m_choices->size() - 1) str += ", "; } str += ')'; } + if (builder->m_required) + { + if (!std::isblank(str.str().back())) + str += " "; + str += "(Required)"; + } } } @@ -545,6 +619,14 @@ namespace blt::argparse parent = parent->m_parent->m_parent; } + for (const auto& [key, _] : m_subparsers) + { + aligner += '{'; + aligner += key; + aligner += '}'; + aligner += ' '; + } + hashmap_t> singleFlags; std::vector> compoundFlags;