net-tools/includes/cli_opt.h
lohhiiccc 2fc8d92e98 refactor(cli): replace runtime size vars with compile-time enum constants
Replace g_options_len and g_opt_str_len extern variables with
CLI_OPT_LEN and CLI_OPT_STR_LEN enum constants computed via X-macros.

Disables Variable Length Arrays (VLA) by enforcing the use of the -Wvla
flag in strict mode.
2026-03-03 09:06:28 +01:00

98 lines
1.7 KiB
C

#ifndef PING_CLI_OPT_H
#define PING_CLI_OPT_H
#define CLI_OPTIONS_LIST \
X( \
'h', \
"help", \
no_argument, \
cli_handle_help, \
OPT_ARG_NONE, \
"Display this help and exit" \
) \
X( \
'V', \
"version", \
no_argument, \
cli_handle_version, \
OPT_ARG_NONE, \
"Display version information and exit" \
) \
X( \
'v', \
"verbose", \
no_argument, \
cli_handle_verbose, \
OPT_ARG_NONE, \
"Verbose output" \
) \
X( \
'q', \
"quiet", \
no_argument, \
cli_handle_quiet, \
OPT_ARG_NONE, \
"Quiet mode (only show summary)" \
) \
X( \
'c', \
"count", \
required_argument, \
cli_handle_count, \
OPT_ARG_UINT, \
"Stop after sending N packets" \
) \
X( \
'i', \
"interval", \
required_argument, \
cli_handle_interval, \
OPT_ARG_SECONDS, \
"Wait N seconds between packets" \
) \
X( \
't', \
"ttl", \
required_argument, \
cli_handle_ttl, \
OPT_ARG_TTL, \
"Set Time To Live" \
) \
X( \
's', \
"size", \
required_argument, \
cli_handle_size, \
OPT_ARG_BYTES, \
"Packet size in bytes" \
) \
X( \
'W', \
"timeout", \
required_argument, \
cli_handle_timeout, \
OPT_ARG_SECONDS, \
"Timeout for replies in seconds" \
) \
X( \
'f', \
"flood", \
no_argument, \
cli_handle_flood, \
OPT_ARG_NONE, \
"Flood mode" \
)
#undef X
#define X(short_opt, long_opt, has_arg, handler, arg_type, desc) + 1
enum { CLI_OPT_LEN = (0 CLI_OPTIONS_LIST) };
#include <getopt.h>
#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))))
enum { CLI_OPT_STR_LEN = (0 CLI_OPTIONS_LIST) };
#endif