mirror of
https://github.com/sametersoylu/argument-parser.git
synced 2026-05-28 20:08:10 +00:00
feat: introduce help. auto add help through parser generation like another action.
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user