help with choices
parent
1b6d23fad4
commit
0913208e6b
|
@ -1,6 +1,6 @@
|
|||
cmake_minimum_required(VERSION 3.20)
|
||||
include(cmake/color.cmake)
|
||||
set(BLT_VERSION 4.0.28)
|
||||
set(BLT_VERSION 4.0.29)
|
||||
|
||||
set(BLT_TARGET BLT)
|
||||
|
||||
|
|
|
@ -32,6 +32,7 @@
|
|||
#include <functional>
|
||||
#include <type_traits>
|
||||
#include <blt/iterator/iterator.h>
|
||||
#include <blt/meta/type_traits.h>
|
||||
#include <blt/std/assert.h>
|
||||
#include <blt/std/expected.h>
|
||||
#include <blt/std/ranges.h>
|
||||
|
@ -227,6 +228,16 @@ namespace blt::argparse
|
|||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
auto ensure_is_string(T&& t)
|
||||
{
|
||||
if constexpr (std::is_arithmetic_v<meta::remove_cvref_t<T>> && !(std::is_same_v<T, char>
|
||||
|| std::is_same_v<T, unsigned char> || std::is_same_v<T, signed char>))
|
||||
return std::to_string(std::forward<T>(t));
|
||||
else
|
||||
return std::forward<T>(t);
|
||||
}
|
||||
|
||||
void test();
|
||||
}
|
||||
|
||||
|
@ -493,6 +504,14 @@ namespace blt::argparse
|
|||
return *this;
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
argument_builder_t& set_choices(Args&&... args)
|
||||
{
|
||||
m_choices = hashset_t<std::string>{};
|
||||
((m_choices->emplace(detail::ensure_is_string(std::forward<Args>(args)))), ...);
|
||||
return *this;
|
||||
}
|
||||
|
||||
argument_builder_t& set_default(const detail::arg_data_t& default_value)
|
||||
{
|
||||
m_default_value = default_value;
|
||||
|
|
|
@ -67,16 +67,6 @@ namespace blt::argparse
|
|||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
auto ensure_is_string(T&& t)
|
||||
{
|
||||
if constexpr (std::is_arithmetic_v<meta::remove_cvref_t<T>> && !(std::is_same_v<T, char>
|
||||
|| std::is_same_v<T, unsigned char> || std::is_same_v<T, signed char>))
|
||||
return std::to_string(std::forward<T>(t));
|
||||
else
|
||||
return std::forward<T>(t);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::string to_string(const T& t)
|
||||
{
|
||||
|
@ -99,7 +89,7 @@ namespace blt::argparse
|
|||
{
|
||||
std::string out;
|
||||
out.reserve((get_const_char_size(strings) + ...));
|
||||
((out += ensure_is_string(std::forward<Strings>(strings))), ...);
|
||||
((out += detail::ensure_is_string(std::forward<Strings>(strings))), ...);
|
||||
return out;
|
||||
}
|
||||
|
||||
|
@ -151,7 +141,7 @@ namespace blt::argparse
|
|||
template <typename T>
|
||||
aligned_internal_string_t& operator+=(T&& value)
|
||||
{
|
||||
const auto str = to_string(ensure_is_string(std::forward<T>(value)));
|
||||
const auto str = to_string(detail::ensure_is_string(std::forward<T>(value)));
|
||||
for (size_t i = 0; i < str.size(); i++)
|
||||
{
|
||||
size_t j = i;
|
||||
|
@ -166,6 +156,11 @@ namespace blt::argparse
|
|||
return *this;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::string& str() const
|
||||
{
|
||||
return string;
|
||||
}
|
||||
|
||||
private:
|
||||
std::string& string;
|
||||
size_t max_line_size;
|
||||
|
@ -264,7 +259,7 @@ namespace blt::argparse
|
|||
template <typename T>
|
||||
aligned_printer_t& add(T&& value)
|
||||
{
|
||||
const auto str = to_string(ensure_is_string(std::forward<T>(value)));
|
||||
const auto str = to_string(detail::ensure_is_string(std::forward<T>(value)));
|
||||
if (buffer.back().size() + str.size() > max_line_size)
|
||||
newline();
|
||||
buffer.back() += replace_tabs(str);
|
||||
|
@ -458,13 +453,10 @@ namespace blt::argparse
|
|||
{
|
||||
auto& [builder, flag_list] = pair;
|
||||
str += builder->m_help.value_or("");
|
||||
}
|
||||
mark.align(4);
|
||||
for (auto [str, pair] : mark.iter().zip(same_flags))
|
||||
{
|
||||
auto& [builder, flag_list] = pair;
|
||||
if (builder->m_default_value)
|
||||
{
|
||||
if (!std::isblank(str.str().back()))
|
||||
str += " ";
|
||||
str += "(Default: ";
|
||||
std::visit(detail::arg_meta_type_helper_t::make_visitor(
|
||||
[&](auto& value)
|
||||
|
@ -487,6 +479,19 @@ namespace blt::argparse
|
|||
}), *builder->m_default_value);
|
||||
str += ")";
|
||||
}
|
||||
if (builder->m_choices)
|
||||
{
|
||||
if (!std::isblank(str.str().back()))
|
||||
str += " ";
|
||||
str += "(Choices: ";
|
||||
for (const auto& [i, v] : enumerate(*builder->m_choices))
|
||||
{
|
||||
str += v;
|
||||
if (i != builder->m_choices->size() - 1)
|
||||
str += ", ";
|
||||
}
|
||||
str += ')';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue