refactor(cli): replace int return codes with e_cli_code enum for argument parsing
- Introduces the e_cli_code enum for CLI status codes, replacing integer constants. - Updates cli_parse_arguments to return e_cli_code instead of int. - Refactors main.c, cli.h, and cli/parse.c to use the new enum.
This commit is contained in:
parent
e5093af710
commit
18211e441c
4 changed files with 16 additions and 10 deletions
|
|
@ -3,9 +3,11 @@
|
|||
|
||||
#include "ft_ping.h"
|
||||
|
||||
#define CLI_SUCCESS 0
|
||||
#define CLI_ERROR 1
|
||||
#define CLI_EXIT_SUCCESS -1
|
||||
enum e_cli_code {
|
||||
CLI_EXIT_SUCCESS = -1,
|
||||
CLI_SUCCESS = 0,
|
||||
CLI_ERROR = 1
|
||||
};
|
||||
|
||||
|
||||
typedef int (*t_option_handler)(const char *arg, t_ping_config *config);
|
||||
|
|
@ -33,6 +35,9 @@ typedef struct s_option_descriptor
|
|||
|
||||
extern const t_option_descriptor g_options[];
|
||||
|
||||
enum e_cli_code
|
||||
cli_parse_arguments(int argc, char **argv, t_ping_config *config);
|
||||
|
||||
int cli_handle_count(const char *arg, t_ping_config *config);
|
||||
int cli_handle_flood(const char *arg, t_ping_config *config);
|
||||
int cli_handle_help(const char *arg, t_ping_config *config);
|
||||
|
|
|
|||
|
|
@ -20,6 +20,4 @@ typedef struct s_ping_config
|
|||
uint8_t flags;
|
||||
} t_ping_config;
|
||||
|
||||
int cli_parse_arguments(int argc, char **argv, t_ping_config *config);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ static const t_option_descriptor *find_option_handler(int opt);
|
|||
static int handle_one_option(int opt, char *arg, t_ping_config *config);
|
||||
/* ------------------- */
|
||||
|
||||
int
|
||||
enum e_cli_code
|
||||
cli_parse_arguments(int argc, char **argv, t_ping_config *config)
|
||||
{
|
||||
struct option long_opts[CLI_OPT_LEN + 1];
|
||||
|
|
@ -30,7 +30,7 @@ cli_parse_arguments(int argc, char **argv, t_ping_config *config)
|
|||
if (0 != res)
|
||||
return res;
|
||||
}
|
||||
return 0;
|
||||
return CLI_SUCCESS;
|
||||
}
|
||||
|
||||
static int
|
||||
|
|
@ -47,7 +47,7 @@ handle_one_option(int opt, char *arg, t_ping_config *config)
|
|||
if (0 != res)
|
||||
return res;
|
||||
}
|
||||
return 0;
|
||||
return CLI_SUCCESS;
|
||||
}
|
||||
|
||||
static void
|
||||
|
|
|
|||
|
|
@ -16,13 +16,16 @@ int
|
|||
main(int argc, char **argv)
|
||||
{
|
||||
t_ping_config config;
|
||||
int ret;
|
||||
enum e_cli_code ret;
|
||||
|
||||
if (0 != init_prog_name(argv[0]))
|
||||
return EXIT_FAILURE;
|
||||
ret = cli_parse_arguments(argc, argv, &config);
|
||||
if (ret != CLI_SUCCESS)
|
||||
return (ret > 0) ? EXIT_FAILURE : EXIT_SUCCESS;
|
||||
{
|
||||
free(g_prog_name.alloc);
|
||||
return (ret == CLI_ERROR) ? EXIT_FAILURE : EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
printf("PING: Not yet implemented\n");
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue