feat: added help message

feat: better error handling
master v0.1.3
cobrapitz 3 years ago
parent 18c0cf5bd5
commit 878e63cd08

@ -14,6 +14,7 @@
#include <algorithm>
namespace cb::argparser {
using ArgumentMapType = std::unordered_map<std::string, cb::argparser::Argument>;
@ -51,6 +52,8 @@ public:
ArgumentMap parse(int argc, char *argv[]);
std::string get_help();
private:
static void insert_argument(ArgumentMap &parsed_args, ParseArgument &positional_arg, const Argument &argument);

@ -1,6 +1,8 @@
#include "cb/argparser/argument_parser.h"
#include <format>
cb::argparser::Argument::Argument(std::string content, cb::argparser::Type type) : content{std::move(content)}, type{type} {
if (type == Type::BOOL_TYPE && this->content == "false") {
@ -179,3 +181,28 @@ void cb::argparser::ArgumentParser::add_argument(const std::string &short_name,
assert(type == Type::INTEGER_TYPE);
add_argument(short_name, name, type, std::move(description), std::to_string(default_value), mode);
}
std::string cb::argparser::ArgumentParser::get_help() {
std::string help_message{};
if (not positional_arguments.empty()) {
help_message += "Positional Arguments:\n";
for (auto &&arg: positional_arguments) {
help_message += std::format("{:15} {}\n",
arg.name,
arg.description);
}
help_message += "\n";
}
if (not arguments.empty()) {
help_message += "Arguments:\n";
for (auto &&arg: arguments) {
help_message += std::format("{:5} {:15} {}\n",
arg.short_name,
arg.name,
arg.description);
}
help_message += "\n";
}
return help_message;
}

Loading…
Cancel
Save