feat: improve the readability of the generated help text. add type hints for the generation of the help text when description is not given for the variable. use sfinae for the check so that it compiles if not given.

This commit is contained in:
2026-03-16 21:50:05 +04:00
parent d5b99ef407
commit 8e502bcb8b
9 changed files with 190 additions and 79 deletions

View File

@@ -1,3 +1,4 @@
#include "headers/parser/parsing_traits/traits.hpp"
#include <string>
#define ALLOW_DASH_FOR_WINDOWS 0
@@ -22,12 +23,16 @@ template <> struct argument_parser::parsing_traits::parser_trait<Point> {
int y = std::stoi(input.substr(comma_pos + 1));
return {x, y};
}
static constexpr argument_parser::parsing_traits::hint_type format_hint = "x,y";
static constexpr argument_parser::parsing_traits::hint_type purpose_hint = "point coordinates";
};
template <> struct argument_parser::parsing_traits::parser_trait<std::regex> {
static std::regex parse(const std::string &input) {
return std::regex(input);
}
static constexpr argument_parser::parsing_traits::hint_type format_hint = "regex";
static constexpr argument_parser::parsing_traits::hint_type purpose_hint = "regular expression";
};
template <> struct argument_parser::parsing_traits::parser_trait<std::vector<int>> {
@@ -40,6 +45,8 @@ template <> struct argument_parser::parsing_traits::parser_trait<std::vector<int
}
return result;
}
static constexpr argument_parser::parsing_traits::hint_type format_hint = "int,int,int";
static constexpr argument_parser::parsing_traits::hint_type purpose_hint = "list of integers";
};
template <> struct argument_parser::parsing_traits::parser_trait<std::vector<std::string>> {
@@ -52,13 +59,16 @@ template <> struct argument_parser::parsing_traits::parser_trait<std::vector<std
}
return result;
}
static constexpr argument_parser::parsing_traits::hint_type format_hint = "string,string,string";
static constexpr argument_parser::parsing_traits::hint_type purpose_hint = "list of strings";
};
const std::initializer_list<argument_parser::conventions::convention const *const> conventions = {
&argument_parser::conventions::gnu_argument_convention,
&argument_parser::conventions::gnu_equal_argument_convention,
&argument_parser::conventions::windows_argument_convention,
&argument_parser::conventions::windows_equal_argument_convention};
// &argument_parser::conventions::windows_argument_convention,
// &argument_parser::conventions::windows_equal_argument_convention
};
const auto echo = argument_parser::helpers::make_parametered_action<std::string>(
[](std::string const &text) { std::cout << text << std::endl; });
@@ -131,8 +141,7 @@ int v2Examples() {
parser.add_argument<std::string>(
{{ShortArgument, "e"}, {LongArgument, "echo"}, {Action, echo}, {HelpText, "echoes given variable"}});
parser.add_argument<Point>(
{{ShortArgument, "ep"}, {LongArgument, "echo-point"}, {Action, echo_point}, {HelpText, "echoes given point"}});
parser.add_argument<Point>({{ShortArgument, "ep"}, {LongArgument, "echo-point"}, {Action, echo_point}});
parser.add_argument<std::string>({
// stores string for f/file flag
@@ -159,6 +168,8 @@ int v2Examples() {
{Required, true} // makes this flag required
});
parser.add_argument({{ShortArgument, "v"}, {LongArgument, "verbose"}});
parser.on_complete(::run_grep);
parser.on_complete(::run_store_point);