poafloc

poafloc - Parser Of Arguments For Lines Of Commands
git clone git://git.dimitrijedobrota.com/poafloc.git
Log | Files | Refs | README | LICENSE

commit dc2a74089da196b2e6255ddad12e45a5198e9717
parent 20708150e7984fdb32d04d08c8b366f1e0eada5b
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Tue, 11 Jun 2024 11:46:55 +0200

Code shuffle

Diffstat:
MCMakeLists.txt | 2+-
Mdemo/main.c | 8+++++---
Mdemo/main.cpp | 14++++++++------
Minclude/args.h | 2+-
Minclude/args.hpp | 20++++++++++----------
Msrc/args.cpp | 7++++++-
Msrc/c_bindings.cpp | 6+++---
7 files changed, 34 insertions(+), 25 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt @@ -3,7 +3,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON) project( args - VERSION 0.0.3 + VERSION 0.0.4 DESCRIPTION "Command Line Argument Parser" LANGUAGES CXX C ) diff --git a/demo/main.c b/demo/main.c @@ -53,13 +53,15 @@ static const args_option_t options[] = { { 0, 0, 0, 0, "Informational Options", -1}, {0}, }; + +static const args_argp_t argp = { + options, parse_opt, "doc string\nother usage", + "First half of the message\vsecond half of the message" +}; // clang-format on int main(int argc, char *argv[]) { arguments_t arguments = {0}; - args_argp_t argp = { - options, parse_opt, "doc string\nother usage", - "First half of the message\vsecond half of the message"}; if (args_parse(&argp, argc, argv, &arguments)) { error("There was an error while parsing arguments"); diff --git a/demo/main.cpp b/demo/main.cpp @@ -19,7 +19,7 @@ struct arguments_t { }; int parse_opt(int key, const char *arg, Parser *parser) { - auto arguments = (arguments_t *)parser->input; + auto arguments = (arguments_t *)parser->input(); switch (key) { case 777: arguments->debug = true; break; @@ -41,7 +41,7 @@ int parse_opt(int key, const char *arg, Parser *parser) { } // clang-format off -static const Parser::option_t options[] = { +static const option_t options[] = { { 0, 'R', 0, 0, "random 0-group option"}, { 0, 0, 0, 0, "Program mode", 1}, {"relocatable", 'r', 0, 0, "Output in relocatable format"}, @@ -55,15 +55,17 @@ static const Parser::option_t options[] = { { 0, 0, 0, 0, "Informational Options", -1}, {0}, }; + +static const argp_t argp = { + options, parse_opt, "doc string\nother usage", + "First half of the message\vsecond half of the message" +}; // clang-format on int main(int argc, char *argv[]) { arguments_t arguments; - Parser::argp_t argp = { - options, parse_opt, "doc string\nother usage", - "First half of the message\vsecond half of the message"}; - if (Parser::parse(&argp, argc, argv, &arguments)) { + if (parse(&argp, argc, argv, &arguments)) { error("There was an error while parsing arguments"); return 1; } diff --git a/include/args.h b/include/args.h @@ -59,7 +59,7 @@ enum ENUM_KEY { #if !defined __cplusplus || defined WITH_C_BINDINGS -int args_parse(args_argp_t *argp, int argc, char *argv[], void *input); +int args_parse(const args_argp_t *argp, int argc, char *argv[], void *input); void *args_parser_input(args_parser *parser); #endif diff --git a/include/args.hpp b/include/args.hpp @@ -9,20 +9,19 @@ namespace args { -class Parser { - public: - using option_t = args_option_t; - using argp_t = args_argp_t; +using option_t = args_option_t; +using argp_t = args_argp_t; - static int parse(argp_t *argp, int argc, char *argv[], void *input) { - Parser parser(input, argp); - return parser.parse(argc, argv, &parser); - } +int parse(const argp_t *argp, int argc, char *argv[], void *input); - void *input; +class Parser { + public: + void *input() const { return m_input; } private: - Parser(void *input, argp_t *argp); + friend int parse(const argp_t *argp, int argc, char *argv[], void *input); + + Parser(const argp_t *argp, void *input); int parse(int argc, char *argv[], void *input); @@ -64,6 +63,7 @@ class Parser { }; const argp_t *argp; + void *m_input; std::unordered_map<int, const option_t *> options; std::vector<help_entry_t> help_entries; diff --git a/src/args.cpp b/src/args.cpp @@ -9,7 +9,12 @@ namespace args { -Parser::Parser(void *input, argp_t *argp) : input(input), argp(argp) { +int parse(const argp_t *argp, int argc, char *argv[], void *input) { + Parser parser(argp, input); + return parser.parse(argc, argv, &parser); +} + +Parser::Parser(const argp_t *argp, void *input) : argp(argp), m_input(input) { int group = 0, key_last = 0; bool hidden = false; diff --git a/src/c_bindings.cpp b/src/c_bindings.cpp @@ -3,10 +3,10 @@ namespace args { -int args_parse(args_argp_t *argp, int argc, char *argv[], void *input) { - return Parser::parse(argp, argc, argv, input); +int args_parse(const args_argp_t *argp, int argc, char *argv[], void *input) { + return parse(argp, argc, argv, input); } -void *args_parser_input(args_parser *parser) { return parser->input; } +void *args_parser_input(args_parser *parser) { return parser->input(); } } // namespace args