Extract CLI options into a reusable macro (CLI_OPTIONS_LIST) in cli_opt.h and refactor handler_map.c to eliminate manual option array duplication. Calculate g_has_len automatically from option definitions.
26 lines
650 B
C
26 lines
650 B
C
#include <getopt.h>
|
|
#include <stddef.h>
|
|
|
|
#include "cli.h"
|
|
#include "cli_opt.h"
|
|
|
|
#undef X
|
|
#define X(short_opt, long_opt, has_arg, handler, arg_type, desc) \
|
|
{ short_opt, long_opt, has_arg, handler, arg_type, desc },
|
|
|
|
const t_option_descriptor g_options[] = {
|
|
CLI_OPTIONS_LIST
|
|
{0, NULL, 0, NULL, OPT_ARG_NONE, NULL }
|
|
};
|
|
|
|
|
|
const size_t g_options_len = ((sizeof(g_options) / sizeof(*g_options)) - 1);
|
|
|
|
#undef X
|
|
#define X(short_opt, long_opt, has_arg, handler, arg_type, desc) \
|
|
+ (1 * (has_arg == no_argument) \
|
|
+ (2 * (has_arg == required_argument) \
|
|
+ (3 * (has_arg == optional_argument))))
|
|
|
|
const size_t g_opt_str_len = CLI_OPT_STR_LEN;
|
|
#undef X
|