net-tools/src/cli/handlers/handle_help.c
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

52 lines
1.2 KiB
C

#include <stddef.h>
#include <stdio.h>
#include <getopt.h>
#include "cli.h"
#include "cli_opt.h"
#include "ft_ping.h"
/* Forward declarations */
static const char * option_arg_type_to_str(t_option_arg_type type);
/* -------------------- */
int
cli_handle_help(const char *arg, t_ping_config *config)
{
(void)config;
(void)arg;
printf("ft_ping - Send ICMP ECHO_REQUEST to network hosts\n");
printf("Usage: ft_ping [options] <destination>\n\n");
printf("Options:\n");
for (size_t i = 0; i < CLI_OPT_LEN; ++i)
{
const t_option_descriptor *opt = &g_options[i];
const char *argstr = option_arg_type_to_str(opt->arg_type);
printf(" -%c, --%-10s %-8s %s\n",
opt->short_opt,
opt->long_opt,
argstr[0] ? argstr : "",
opt->description
);
}
return CLI_EXIT_SUCCESS;
}
static const char *
option_arg_type_to_str(t_option_arg_type type)
{
switch (type)
{
case OPT_ARG_NONE: return "";
case OPT_ARG_INT: return "<NUM>";
case OPT_ARG_UINT: return "<NUM>";
case OPT_ARG_SECONDS: return "<SEC>";
case OPT_ARG_BYTES: return "<SIZE>";
case OPT_ARG_TTL: return "<TTL>";
case OPT_ARG_STRING: return "<STR>";
default: return "<ARG>";
}
}