mirror of
https://github.com/sametersoylu/argument-parser.git
synced 2026-04-13 03:41:18 +00:00
chore: add missing definitions to the files. remove static qualifier from the headers. add help text to the actions.
This commit is contained in:
@@ -16,4 +16,6 @@ include_directories(src/headers/conventions/implementations)
|
|||||||
include_directories(src/headers/parser/platform_headers)
|
include_directories(src/headers/parser/platform_headers)
|
||||||
include_directories(src/headers/parser/parsing_traits)
|
include_directories(src/headers/parser/parsing_traits)
|
||||||
|
|
||||||
add_executable(test src/main.cpp)
|
file(GLOB_RECURSE SRC_FILES "src/source/*.cpp" "src/source/**/*.cpp" "src/source/**/**/*.cpp")
|
||||||
|
|
||||||
|
add_executable(test src/main.cpp ${SRC_FILES})
|
||||||
@@ -27,9 +27,8 @@ namespace argument_parser::conventions {
|
|||||||
} // namespace argument_parser::conventions
|
} // namespace argument_parser::conventions
|
||||||
|
|
||||||
namespace argument_parser::conventions::helpers {
|
namespace argument_parser::conventions::helpers {
|
||||||
static std::string to_lower(std::string s);
|
std::string to_lower(std::string s);
|
||||||
|
std::string to_upper(std::string s);
|
||||||
static std::string to_upper(std::string s);
|
|
||||||
} // namespace argument_parser::conventions::helpers
|
} // namespace argument_parser::conventions::helpers
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
25
src/main.cpp
25
src/main.cpp
@@ -2,7 +2,6 @@
|
|||||||
#define ALLOW_DASH_FOR_WINDOWS 0
|
#define ALLOW_DASH_FOR_WINDOWS 0
|
||||||
|
|
||||||
#include <argparse>
|
#include <argparse>
|
||||||
#include <expected>
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <parser_v2.hpp>
|
#include <parser_v2.hpp>
|
||||||
@@ -10,7 +9,6 @@
|
|||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
|
||||||
struct Point {
|
struct Point {
|
||||||
int x, y;
|
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 = {
|
const std::initializer_list<argument_parser::conventions::convention const *const> conventions = {
|
||||||
&argument_parser::conventions::gnu_argument_convention,
|
&argument_parser::conventions::gnu_argument_convention,
|
||||||
&argument_parser::conventions::gnu_equal_argument_convention,
|
&argument_parser::conventions::gnu_equal_argument_convention,
|
||||||
@@ -154,14 +164,17 @@ int v2Examples() {
|
|||||||
using namespace argument_parser::v2::flags;
|
using namespace argument_parser::v2::flags;
|
||||||
argument_parser::v2::base_parser parser;
|
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>({
|
parser.add_argument<std::string>({
|
||||||
// stores string for f/file flag
|
// stores string for f/file flag
|
||||||
{ShortArgument, "f"},
|
{ShortArgument, "f"},
|
||||||
{LongArgument, "file"},
|
{LongArgument, "file"},
|
||||||
|
{HelpText, "File to grep, required only if using grep"},
|
||||||
// if no action, falls to store operation with given type.
|
// if no action, falls to store operation with given type.
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -169,10 +182,12 @@ int v2Examples() {
|
|||||||
// stores string for g/grep flag
|
// stores string for g/grep flag
|
||||||
{ShortArgument, "g"},
|
{ShortArgument, "g"},
|
||||||
{LongArgument, "grep"},
|
{LongArgument, "grep"},
|
||||||
|
{HelpText, "Grep pattern, required only if using file"},
|
||||||
// same as 'file' flag
|
// 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>({
|
parser.add_argument<Point>({
|
||||||
// { ShortArgument, "sp" }, // now if ShortArgument or LongArgument is missing, it will use it for the other.
|
// { ShortArgument, "sp" }, // now if ShortArgument or LongArgument is missing, it will use it for the other.
|
||||||
|
|||||||
@@ -2,12 +2,12 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
namespace argument_parser::conventions::helpers {
|
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); });
|
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return std::tolower(c); });
|
||||||
return s;
|
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); });
|
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return std::toupper(c); });
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,6 +27,10 @@ namespace argument_parser {
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool argument::expects_parameter() const {
|
||||||
|
return action->expects_parameter();
|
||||||
|
}
|
||||||
|
|
||||||
bool argument::is_required() const {
|
bool argument::is_required() const {
|
||||||
return required;
|
return required;
|
||||||
}
|
}
|
||||||
@@ -181,13 +185,13 @@ namespace argument_parser {
|
|||||||
std::initializer_list<conventions::convention const *const> convention_types) {
|
std::initializer_list<conventions::convention const *const> convention_types) {
|
||||||
std::vector<std::pair<std::string, std::string>> required_args;
|
std::vector<std::pair<std::string, std::string>> required_args;
|
||||||
for (auto const &[key, arg] : argument_map) {
|
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>>(
|
required_args.emplace_back<std::pair<std::string, std::string>>(
|
||||||
{reverse_short_arguments[key], reverse_long_arguments[key]});
|
{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: ";
|
std::cerr << "These arguments were expected but not provided: ";
|
||||||
for (auto const &[s, l] : required_args) {
|
for (auto const &[s, l] : required_args) {
|
||||||
std::cerr << "[-" << s << ", --" << l << "] ";
|
std::cerr << "[-" << s << ", --" << l << "] ";
|
||||||
|
|||||||
Reference in New Issue
Block a user