poafloc

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

commit 6742bf3cc7de1fc167e7ee1036188ee7090fcfa1
parent 7e17af6fb29d98f538cab80db0fdbef1bd170929
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Tue, 11 Jun 2024 00:48:30 +0200

Fix ugly enums using prepprocessor magic

Diffstat:
Mdemo/main.cpp | 6++----
Minclude/args.h | 36++++++++++++++++++++++++------------
Minclude/args.hpp | 15---------------
3 files changed, 26 insertions(+), 31 deletions(-)

diff --git a/demo/main.cpp b/demo/main.cpp @@ -33,15 +33,13 @@ int parse_opt(int key, const char *arg, Parser *parser) { break; case 'o': arguments->output_file = arg ? arg : "stdout"; break; case 'i': arguments->input_file = arg; break; - case Parser::Key::ARG: arguments->args.push_back(arg); break; - case Parser::Key::ERROR: std::cerr << "handled error\n"; + case Key::ARG: arguments->args.push_back(arg); break; + case Key::ERROR: std::cerr << "handled error\n"; } return 0; } -using enum Parser::Option; - // clang-format off static const Parser::option_t options[] = { { 0, 'R', 0, 0, "random 0-group option"}, diff --git a/include/args.h b/include/args.h @@ -3,13 +3,22 @@ #ifdef __cplusplus +#define MANGLE_ENUM(enumn, name) name +#define ENUM_OPTION Option +#define ENUM_KEY Key + extern "C" { namespace args { + struct Parser; typedef Parser args_parser; #else +#define MANGLE_ENUM(enumn, name) ARGS_##enumn##_##name +#define ENUM_OPTION args_option_e +#define ENUM_KEY args_key_e + struct __Parser; typedef struct __Parser args_parser; @@ -33,22 +42,21 @@ typedef struct { const char *message; } args_argp_t; -enum args_option_e { - ARGS_OPTION_ARG_OPTIONAL = 0x1, - ARGS_OPTION_HIDDEN = 0x2, - ARGS_OPTION_ALIAS = 0x4, +enum ENUM_OPTION { + MANGLE_ENUM(OPTION, ARG_OPTIONAL) = 0x1, + MANGLE_ENUM(OPTION, HIDDEN) = 0x2, + MANGLE_ENUM(OPTION, ALIAS) = 0x4, }; -enum args_key_e { - ARGS_KEY_ARG = 0, - ARGS_KEY_END = 0x1000001, - ARGS_KEY_NO_ARGS = 0x1000002, - ARGS_KEY_INIT = 0x1000003, - ARGS_KEY_SUCCESS = 0x1000004, - ARGS_KEY_ERROR = 0x1000005, +enum ENUM_KEY { + MANGLE_ENUM(KEY, ARG) = 0, + MANGLE_ENUM(KEY, END) = 0x1000001, + MANGLE_ENUM(KEY, NO_ARGS) = 0x1000002, + MANGLE_ENUM(KEY, INIT) = 0x1000003, + MANGLE_ENUM(KEY, SUCCESS) = 0x1000004, + MANGLE_ENUM(KEY, ERROR) = 0x1000005, }; - #if !defined __cplusplus || defined WITH_C_BINDINGS int args_parse(args_argp_t *argp, int argc, char *argv[], void *input); @@ -56,6 +64,10 @@ void *args_parser_input(args_parser *parser); #endif +#undef MANGLE_ENUM +#undef ENUM_OPTION +#undef ENUM_KEY + #ifdef __cplusplus } // namespace args } // extern "C" diff --git a/include/args.hpp b/include/args.hpp @@ -14,21 +14,6 @@ class Parser { using option_t = args_option_t; using argp_t = args_argp_t; - enum Option { - ARG_OPTIONAL = ARGS_OPTION_ARG_OPTIONAL, - HIDDEN = ARGS_OPTION_HIDDEN, - ALIAS = ARGS_OPTION_ALIAS, - }; - - enum Key { - ARG = ARGS_KEY_ARG, - END = ARGS_KEY_END, - NO_ARGS = ARGS_KEY_NO_ARGS, - INIT = ARGS_KEY_INIT, - SUCCESS = ARGS_KEY_SUCCESS, - ERROR = ARGS_KEY_ERROR, - }; - static int parse(argp_t *argp, int argc, char *argv[], void *input) { Parser parser(input, argp); return parser.parse(argc, argv, &parser);