mirror of
https://github.com/sametersoylu/argument-parser.git
synced 2026-05-28 20:08:10 +00:00
Add should_exit support for v2 parsers
- Add should_exit support to v2 platform parsers - Introduce macros.h and integrate trait hints - Wire CMake packaging with config, version, and install dirs - Update parser traits to include validation hooks and fixes
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
#ifndef ARGPARSE_HPP
|
||||
#define ARGPARSE_HPP
|
||||
#include <argument_parser.hpp>
|
||||
#include "macros.h"
|
||||
|
||||
#ifdef __linux__
|
||||
#include <linux_parser.hpp>
|
||||
@@ -37,4 +38,4 @@ namespace argument_parser::v2 {
|
||||
|
||||
#include <base_convention.hpp>
|
||||
#include <gnu_argument_convention.hpp>
|
||||
#include <windows_argument_convention.hpp>
|
||||
#include <windows_argument_convention.hpp>
|
||||
|
||||
8
src/headers/macros.h
Normal file
8
src/headers/macros.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
#ifndef MACROS_H
|
||||
#define MACROS_H
|
||||
|
||||
#define ARGPARSE_TRAIT_FORMAT_HINT static constexpr argument_parser::parsing_traits::hint_type format_hint
|
||||
#define ARGPARSE_TRAIT_PURPOSE_HINT static constexpr argument_parser::parsing_traits::hint_type purpose_hint
|
||||
|
||||
#endif // MACROS_H
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "parser_v2.hpp"
|
||||
#ifndef FAKE_PARSER_HPP
|
||||
#define FAKE_PARSER_HPP
|
||||
|
||||
@@ -18,6 +19,19 @@ namespace argument_parser {
|
||||
void set_program_name(std::string const &program_name);
|
||||
void set_parsed_arguments(std::vector<std::string> const &parsed_arguments);
|
||||
};
|
||||
|
||||
namespace v2 {
|
||||
class fake_parser : public argument_parser::v2::base_parser {
|
||||
public:
|
||||
fake_parser() = default;
|
||||
fake_parser(std::string program_name, std::vector<std::string> const &arguments);
|
||||
fake_parser(std::string const &program_name, std::vector<std::string> &&arguments);
|
||||
fake_parser(std::string const &program_name, std::initializer_list<std::string> const &arguments);
|
||||
|
||||
void set_program_name(std::string const &program_name);
|
||||
void set_parsed_arguments(std::vector<std::string> const &parsed_arguments);
|
||||
};
|
||||
}
|
||||
} // namespace argument_parser
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
#include <argument_parser.hpp>
|
||||
#include <array>
|
||||
#include <cstdlib>
|
||||
#include <fake_parser.hpp>
|
||||
#include <initializer_list>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
@@ -91,12 +90,14 @@ namespace argument_parser::v2 {
|
||||
using argument_parser::base_parser::current_conventions;
|
||||
using argument_parser::base_parser::reset_current_conventions;
|
||||
|
||||
void prepare_help_flag() {
|
||||
void prepare_help_flag(bool should_exit = true) {
|
||||
add_argument({{flags::ShortArgument, "h"},
|
||||
{flags::LongArgument, "help"},
|
||||
{flags::Action, helpers::make_non_parametered_action([this]() {
|
||||
{flags::Action, helpers::make_non_parametered_action([this, should_exit]() {
|
||||
this->display_help(this->current_conventions());
|
||||
std::exit(0);
|
||||
if (should_exit) {
|
||||
std::exit(0);
|
||||
}
|
||||
})},
|
||||
{flags::HelpText, "Prints this help text."}});
|
||||
}
|
||||
|
||||
@@ -10,39 +10,46 @@ namespace argument_parser::parsing_traits {
|
||||
template <typename T_> struct parser_trait {
|
||||
using type = T_;
|
||||
static T_ parse(const std::string &input);
|
||||
static bool validate(T_ const&);
|
||||
|
||||
static constexpr hint_type format_hint = "value";
|
||||
static constexpr hint_type purpose_hint = "value";
|
||||
};
|
||||
|
||||
template <> struct parser_trait<std::string> {
|
||||
static std::string parse(const std::string &input);
|
||||
|
||||
static constexpr hint_type format_hint = "string";
|
||||
static constexpr hint_type purpose_hint = "string value";
|
||||
};
|
||||
|
||||
template <> struct parser_trait<bool> {
|
||||
static bool parse(const std::string &input);
|
||||
|
||||
static constexpr hint_type format_hint = "true/false";
|
||||
static constexpr hint_type purpose_hint = "boolean value";
|
||||
};
|
||||
|
||||
template <> struct parser_trait<int> {
|
||||
static int parse(const std::string &input);
|
||||
|
||||
static constexpr hint_type format_hint = "123";
|
||||
static constexpr hint_type purpose_hint = "integer value";
|
||||
};
|
||||
|
||||
template <> struct parser_trait<float> {
|
||||
static float parse(const std::string &input);
|
||||
|
||||
static constexpr hint_type format_hint = "3.14";
|
||||
static constexpr hint_type purpose_hint = "flaoting point number";
|
||||
static constexpr hint_type purpose_hint = "floating point number";
|
||||
};
|
||||
|
||||
template <> struct parser_trait<double> {
|
||||
static double parse(const std::string &input);
|
||||
|
||||
static constexpr hint_type format_hint = "3.14";
|
||||
static constexpr hint_type purpose_hint = "double precision floating point number";
|
||||
};
|
||||
} // namespace argument_parser::parsing_traits
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -16,11 +16,11 @@ namespace argument_parser {
|
||||
namespace v2 {
|
||||
class linux_parser : public v2::base_parser {
|
||||
public:
|
||||
linux_parser();
|
||||
linux_parser(bool should_exit = true);
|
||||
using base_parser::display_help;
|
||||
};
|
||||
} // namespace v2
|
||||
} // namespace argument_parser
|
||||
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -4,9 +4,7 @@
|
||||
#define MACOS_PARSER_HPP
|
||||
|
||||
#include <argument_parser.hpp>
|
||||
#include <crt_externs.h>
|
||||
#include <parser_v2.hpp>
|
||||
#include <string>
|
||||
|
||||
namespace argument_parser {
|
||||
class macos_parser : public base_parser {
|
||||
@@ -17,11 +15,11 @@ namespace argument_parser {
|
||||
namespace v2 {
|
||||
class macos_parser : public v2::base_parser {
|
||||
public:
|
||||
macos_parser();
|
||||
macos_parser(bool should_exit = true);
|
||||
using base_parser::display_help;
|
||||
};
|
||||
} // namespace v2
|
||||
} // namespace argument_parser
|
||||
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -12,9 +12,9 @@ namespace argument_parser {
|
||||
namespace v2 {
|
||||
class windows_parser : public v2::base_parser {
|
||||
public:
|
||||
windows_parser();
|
||||
windows_parser(bool should_exit = true);
|
||||
using base_parser::display_help;
|
||||
};
|
||||
} // namespace v2
|
||||
} // namespace argument_parser
|
||||
#endif
|
||||
#endif
|
||||
|
||||
94
src/main.cpp
94
src/main.cpp
@@ -1,4 +1,7 @@
|
||||
#include "macros.h"
|
||||
#include "traits.hpp"
|
||||
#include <exception>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include <argparse>
|
||||
@@ -22,31 +25,23 @@ template <> struct argument_parser::parsing_traits::parser_trait<Point> {
|
||||
int y = std::stoi(input.substr(comma_pos + 1));
|
||||
return {x, y};
|
||||
}
|
||||
static constexpr argument_parser::parsing_traits::hint_type format_hint = "x,y";
|
||||
static constexpr argument_parser::parsing_traits::hint_type purpose_hint = "coordinates";
|
||||
|
||||
static bool validate(Point const& p) {
|
||||
return p.x >= 0 && p.y >= 0;
|
||||
}
|
||||
|
||||
ARGPARSE_TRAIT_FORMAT_HINT = "x,y";
|
||||
ARGPARSE_TRAIT_PURPOSE_HINT = "coordinates";
|
||||
};
|
||||
|
||||
template <> struct argument_parser::parsing_traits::parser_trait<std::regex> {
|
||||
static std::regex parse(const std::string &input) {
|
||||
return std::regex(input);
|
||||
}
|
||||
static constexpr argument_parser::parsing_traits::hint_type format_hint = "regex";
|
||||
static constexpr argument_parser::parsing_traits::hint_type purpose_hint = "regular expression";
|
||||
ARGPARSE_TRAIT_FORMAT_HINT = "regex";
|
||||
ARGPARSE_TRAIT_PURPOSE_HINT = "regular expression";
|
||||
};
|
||||
|
||||
template <> struct argument_parser::parsing_traits::parser_trait<std::vector<int>> {
|
||||
static std::vector<int> parse(const std::string &input) {
|
||||
std::vector<int> result;
|
||||
std::stringstream ss{input};
|
||||
std::string item;
|
||||
while (std::getline(ss, item, ',')) {
|
||||
result.push_back(std::stoi(item));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
static constexpr argument_parser::parsing_traits::hint_type format_hint = "int,int,int";
|
||||
static constexpr argument_parser::parsing_traits::hint_type purpose_hint = "list of integers";
|
||||
};
|
||||
|
||||
template <> struct argument_parser::parsing_traits::parser_trait<std::vector<std::string>> {
|
||||
static std::vector<std::string> parse(const std::string &input) {
|
||||
@@ -58,8 +53,24 @@ template <> struct argument_parser::parsing_traits::parser_trait<std::vector<std
|
||||
}
|
||||
return result;
|
||||
}
|
||||
static constexpr argument_parser::parsing_traits::hint_type format_hint = "string,string,string";
|
||||
static constexpr argument_parser::parsing_traits::hint_type purpose_hint = "list of strings";
|
||||
|
||||
ARGPARSE_TRAIT_FORMAT_HINT = "string,string,string";
|
||||
ARGPARSE_TRAIT_PURPOSE_HINT = "list of strings";
|
||||
};
|
||||
|
||||
template <typename VT> struct argument_parser::parsing_traits::parser_trait<std::vector<VT>> {
|
||||
static std::vector<VT> parse(const std::string &input) {
|
||||
std::vector<VT> result;
|
||||
std::stringstream ss{input};
|
||||
std::string item;
|
||||
|
||||
while (std::getline(ss, item, ',')) {
|
||||
result.push_back(argument_parser::parsing_traits::parser_trait<VT>::parse(item));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
ARGPARSE_TRAIT_FORMAT_HINT = "VT,VT,VT";
|
||||
ARGPARSE_TRAIT_PURPOSE_HINT = "list of VT";
|
||||
};
|
||||
|
||||
const std::initializer_list<argument_parser::conventions::convention const *const> conventions = {
|
||||
@@ -88,6 +99,7 @@ const auto cat = argument_parser::helpers::make_parametered_action<std::string>(
|
||||
file.close();
|
||||
});
|
||||
|
||||
|
||||
auto grep(argument_parser::base_parser const &parser, std::string const &filename, std::regex const &pattern) {
|
||||
if (filename.empty()) {
|
||||
std::cerr << "Missing filename" << std::endl;
|
||||
@@ -135,7 +147,7 @@ void run_store_point(argument_parser::base_parser const &parser) {
|
||||
|
||||
int v2Examples() {
|
||||
using namespace argument_parser::v2::flags;
|
||||
argument_parser::v2::parser parser;
|
||||
argument_parser::v2::parser parser{ false };
|
||||
|
||||
parser.add_argument<std::string>(
|
||||
{{ShortArgument, "e"}, {LongArgument, "echo"}, {Action, echo}, {HelpText, "echoes given variable"}});
|
||||
@@ -159,7 +171,8 @@ int v2Examples() {
|
||||
});
|
||||
|
||||
parser.add_argument<std::string>(
|
||||
{{ShortArgument, "c"}, {LongArgument, "cat"}, {Action, cat}, {HelpText, "Prints the content of the file"}});
|
||||
{{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.
|
||||
@@ -180,6 +193,8 @@ int v2Examples() {
|
||||
{HelpText, "Output file path"},
|
||||
});
|
||||
|
||||
parser.add_argument<std::vector<Point>>({{LongArgument, "points"}, {HelpText, "List of points to store"}});
|
||||
|
||||
parser.on_complete(::run_grep);
|
||||
parser.on_complete(::run_store_point);
|
||||
parser.on_complete([](argument_parser::base_parser const &p) {
|
||||
@@ -197,10 +212,43 @@ int v2Examples() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
auto unique_copy(std::unique_ptr<std::string> ptr) {
|
||||
std::cout << *ptr << std::endl;
|
||||
}
|
||||
|
||||
auto unique_reference(std::unique_ptr<std::string> const& ptr) {
|
||||
std::cout << *ptr << std::endl;
|
||||
}
|
||||
|
||||
auto unique_move(std::unique_ptr<std::string>&& ptr) {
|
||||
std::cout << *ptr << std::endl;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T return_example(std::function<T()> func) {
|
||||
if constexpr (std::is_same_v<void, T>) {
|
||||
return func();
|
||||
} else {
|
||||
return func();
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void log_result(std::function<T()> func) {
|
||||
if constexpr (std::is_same_v<void, T>) {
|
||||
func();
|
||||
} else {
|
||||
std::cout << "result: " << func() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
try {
|
||||
return_example<void>([]{});
|
||||
|
||||
return v2Examples();
|
||||
} catch (std::exception const &e) {
|
||||
std::cout << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,4 +21,30 @@ namespace argument_parser {
|
||||
void fake_parser::set_parsed_arguments(std::vector<std::string> const &parsed_arguments) {
|
||||
this->parsed_arguments = parsed_arguments;
|
||||
}
|
||||
} // namespace argument_parser
|
||||
|
||||
namespace v2 {
|
||||
fake_parser::fake_parser(std::string program_name, std::vector<std::string> const &arguments) {
|
||||
set_program_name(program_name);
|
||||
ref_parsed_args() = arguments;
|
||||
prepare_help_flag(false);
|
||||
}
|
||||
|
||||
fake_parser::fake_parser(std::string const &program_name, std::vector<std::string> &&arguments) {
|
||||
set_program_name(program_name);
|
||||
ref_parsed_args() = std::move(arguments);
|
||||
prepare_help_flag(false);
|
||||
}
|
||||
|
||||
fake_parser::fake_parser(std::string const &program_name, std::initializer_list<std::string> const &arguments)
|
||||
: fake_parser(program_name, std::vector<std::string>(arguments)) {
|
||||
}
|
||||
|
||||
void fake_parser::set_program_name(std::string const &program_name) {
|
||||
set_program_name(program_name);
|
||||
}
|
||||
|
||||
void fake_parser::set_parsed_arguments(std::vector<std::string> const &parsed_arguments) {
|
||||
ref_parsed_args() = parsed_arguments;
|
||||
}
|
||||
}
|
||||
} // namespace argument_parser
|
||||
|
||||
@@ -15,7 +15,7 @@ namespace argument_parser {
|
||||
}
|
||||
|
||||
namespace v2 {
|
||||
linux_parser::linux_parser() {
|
||||
linux_parser::linux_parser(bool should_exit) {
|
||||
std::ifstream command_line_file{"/proc/self/cmdline"};
|
||||
std::string program_name;
|
||||
std::getline(command_line_file, program_name, '\0');
|
||||
@@ -25,7 +25,7 @@ namespace argument_parser {
|
||||
parsed_arguments.emplace_back(line);
|
||||
}
|
||||
|
||||
prepare_help_flag();
|
||||
prepare_help_flag(should_exit);
|
||||
}
|
||||
} // namespace v2
|
||||
} // namespace argument_parser
|
||||
|
||||
@@ -2,32 +2,36 @@
|
||||
|
||||
#include "macos_parser.hpp"
|
||||
|
||||
#include <crt_externs.h>
|
||||
|
||||
#define MACOS_GETARGS_LOOP(argc_name, argv_name, before_for, for_body) \
|
||||
do { \
|
||||
const int argc_name = *_NSGetArgc(); \
|
||||
if (char **argv_name = *_NSGetArgv(); argc_name > 0 && argv_name != nullptr && argv_name[0] != nullptr) { \
|
||||
do { before_for; } while(false); \
|
||||
for (int i = 1; i < argc_name; ++i) { \
|
||||
for_body \
|
||||
} \
|
||||
} \
|
||||
} while (false)
|
||||
|
||||
namespace argument_parser {
|
||||
macos_parser::macos_parser() {
|
||||
const int argc = *_NSGetArgc();
|
||||
if (char **argv = *_NSGetArgv(); argc > 0 && argv != nullptr && argv[0] != nullptr) {
|
||||
program_name = (argv[0]);
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
if (argv[i] != nullptr)
|
||||
parsed_arguments.emplace_back(argv[i]);
|
||||
}
|
||||
}
|
||||
MACOS_GETARGS_LOOP(argc, argv, program_name = argv[0], {
|
||||
if (argv[i] != nullptr)
|
||||
parsed_arguments.emplace_back(argv[i]);
|
||||
});
|
||||
}
|
||||
|
||||
namespace v2 {
|
||||
macos_parser::macos_parser() {
|
||||
const int argc = *_NSGetArgc();
|
||||
if (char **argv = *_NSGetArgv(); argc > 0 && argv != nullptr && argv[0] != nullptr) {
|
||||
set_program_name(argv[0]);
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
if (argv[i] != nullptr)
|
||||
ref_parsed_args().emplace_back(argv[i]);
|
||||
}
|
||||
}
|
||||
|
||||
prepare_help_flag();
|
||||
macos_parser::macos_parser(bool should_exit) {
|
||||
MACOS_GETARGS_LOOP(argc, argv, set_program_name(argv[0]), {
|
||||
if (argv[i] != nullptr)
|
||||
ref_parsed_args().emplace_back(argv[i]);
|
||||
});
|
||||
prepare_help_flag(should_exit);
|
||||
}
|
||||
} // namespace v2
|
||||
} // namespace argument_parser
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -95,12 +95,12 @@ namespace argument_parser {
|
||||
} // namespace argument_parser
|
||||
|
||||
namespace argument_parser::v2 {
|
||||
windows_parser::windows_parser() {
|
||||
windows_parser::windows_parser(bool should_exit) {
|
||||
parse_windows_arguments(ref_parsed_args(),
|
||||
[this](std::string const &program_name) { this->set_program_name(program_name); });
|
||||
|
||||
prepare_help_flag();
|
||||
prepare_help_flag(should_exit);
|
||||
}
|
||||
} // namespace argument_parser::v2
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user