chore: add missing definitions to the files. remove static qualifier from the headers. add help text to the actions.

This commit is contained in:
2026-03-15 23:55:06 +04:00
parent 9e646622db
commit 3552d34dac
5 changed files with 33 additions and 13 deletions

View File

@@ -27,9 +27,8 @@ namespace argument_parser::conventions {
} // namespace argument_parser::conventions
namespace argument_parser::conventions::helpers {
static std::string to_lower(std::string s);
static std::string to_upper(std::string s);
std::string to_lower(std::string s);
std::string to_upper(std::string s);
} // namespace argument_parser::conventions::helpers
#endif

View File

@@ -2,7 +2,6 @@
#define ALLOW_DASH_FOR_WINDOWS 0
#include <argparse>
#include <expected>
#include <fstream>
#include <iostream>
#include <parser_v2.hpp>
@@ -10,7 +9,6 @@
#include <sstream>
#include <vector>
struct Point {
int x, y;
};
@@ -45,6 +43,18 @@ template <> struct argument_parser::parsing_traits::parser_trait<std::vector<int
}
};
template <> struct argument_parser::parsing_traits::parser_trait<std::vector<std::string>> {
static std::vector<std::string> parse(const std::string &input) {
std::vector<std::string> result;
std::stringstream ss{input};
std::string item;
while (std::getline(ss, item, ',')) {
result.push_back(item);
}
return result;
}
};
const std::initializer_list<argument_parser::conventions::convention const *const> conventions = {
&argument_parser::conventions::gnu_argument_convention,
&argument_parser::conventions::gnu_equal_argument_convention,
@@ -154,14 +164,17 @@ int v2Examples() {
using namespace argument_parser::v2::flags;
argument_parser::v2::base_parser parser;
parser.add_argument<std::string>({{ShortArgument, "e"}, {LongArgument, "echo"}, {Action, echo}});
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}});
parser.add_argument<Point>(
{{ShortArgument, "ep"}, {LongArgument, "echo-point"}, {Action, echo_point}, {HelpText, "echoes given point"}});
parser.add_argument<std::string>({
// stores string for f/file flag
{ShortArgument, "f"},
{LongArgument, "file"},
{HelpText, "File to grep, required only if using grep"},
// if no action, falls to store operation with given type.
});
@@ -169,10 +182,12 @@ int v2Examples() {
// stores string for g/grep flag
{ShortArgument, "g"},
{LongArgument, "grep"},
{HelpText, "Grep pattern, required only if using file"},
// same as 'file' flag
});
parser.add_argument<std::string>({{ShortArgument, "c"}, {LongArgument, "cat"}, {Action, cat}});
parser.add_argument<std::string>(
{{ShortArgument, "c"}, {LongArgument, "cat"}, {Action, cat}, {HelpText, "Prints the content of the file"}});
parser.add_argument<Point>({
// { ShortArgument, "sp" }, // now if ShortArgument or LongArgument is missing, it will use it for the other.

View File

@@ -2,12 +2,12 @@
#include <algorithm>
namespace argument_parser::conventions::helpers {
static std::string to_lower(std::string s) {
std::string to_lower(std::string s) {
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return std::tolower(c); });
return s;
}
static std::string to_upper(std::string s) {
std::string to_upper(std::string s) {
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return std::toupper(c); });
return s;
}

View File

@@ -27,6 +27,10 @@ namespace argument_parser {
return *this;
}
bool argument::expects_parameter() const {
return action->expects_parameter();
}
bool argument::is_required() const {
return required;
}
@@ -181,13 +185,13 @@ namespace argument_parser {
std::initializer_list<conventions::convention const *const> convention_types) {
std::vector<std::pair<std::string, std::string>> required_args;
for (auto const &[key, arg] : argument_map) {
if (arg.is_required() and not arg.is_invoked()) {
if (arg.is_required() && !arg.is_invoked()) {
required_args.emplace_back<std::pair<std::string, std::string>>(
{reverse_short_arguments[key], reverse_long_arguments[key]});
}
}
if (not required_args.empty()) {
if (!required_args.empty()) {
std::cerr << "These arguments were expected but not provided: ";
for (auto const &[s, l] : required_args) {
std::cerr << "[-" << s << ", --" << l << "] ";