feat: introduce help. auto add help through parser generation like another action.

This commit is contained in:
2026-03-16 18:45:16 +04:00
parent 3a8e919ad1
commit a8b7078949
5 changed files with 110 additions and 6 deletions

View File

@@ -2,6 +2,18 @@
#include <iostream>
#include <sstream>
#include <thread>
class deferred_exec {
public:
deferred_exec(std::function<void()> const &func) : func(func) {}
~deferred_exec() {
func();
}
private:
std::function<void()> func;
};
bool contains(std::unordered_map<std::string, int> const &map, std::string const &key) {
return map.find(key) != map.end();
@@ -101,6 +113,13 @@ namespace argument_parser {
}
void base_parser::handle_arguments(std::initializer_list<conventions::convention const *const> convention_types) {
if (std::this_thread::get_id() != this->creation_thread_id.load()) {
throw std::runtime_error("handle_arguments must be called from the main thread");
}
deferred_exec reset_current_conventions([this]() { this->reset_current_conventions(); });
this->current_conventions(convention_types);
for (auto it = parsed_arguments.begin(); it != parsed_arguments.end(); ++it) {
std::stringstream error_stream;
bool arg_correctly_handled = false;

View File

@@ -1,6 +1,9 @@
#ifdef _WIN32
#include "windows_parser.hpp"
#include "argument_parser.hpp"
#include "parser_v2.hpp"
#include <Windows.h>
#include <iostream>
@@ -96,6 +99,14 @@ namespace argument_parser::v2 {
windows_parser::windows_parser() {
parse_windows_arguments(ref_parsed_args(),
[this](std::string const &program_name) { this->set_program_name(program_name); });
add_argument({{flags::ShortArgument, "h"},
{flags::LongArgument, "help"},
{flags::Action, helpers::make_non_parametered_action([this]() {
this->display_help(this->current_conventions());
std::exit(0);
})},
{flags::HelpText, "Prints this help text."}});
}
} // namespace argument_parser::v2