c-md/srcs/cli/cli.c.md
2026-01-12 16:22:13 +01:00

201 lines
3.6 KiB
Markdown

# cli.c
The command-line interface (CLI) parser for the c-md transpiler.
Handles parsing command-line arguments and populating the t_args structure.
---
### Includes
```c
#include <stddef.h>
#include <getopt.h>
#include <string.h>
#include "cli.h"
```
---
### Structs and Type Definitions
Function pointer type for option handlers.
```c
typedef void (*t_handler)(t_args *, const char *);
```
Struct to map short options to their handlers.
```c
typedef struct s_opt
{
int8_t short_opt;
t_handler handler;
} t_opt;
```
### Forward Declarations
```c
static void handle_help(t_args *args, const char *value);
static void handle_ext(t_args *args, const char *value);
static void handle_input(t_args *args, const char *value);
static void handle_output(t_args *args, const char *value);
static void handle_map(t_args *args, const char *value);
static t_handler find_handler(int8_t opt);
static void init_args(t_args *args);
```
### Static Variables
Initialize the mapping of options to their handlers.
```c
static const t_opt g_opts[] = {
{'h', handle_help},
{'e', handle_ext},
{'i', handle_input},
{'o', handle_output},
{'m', handle_map},
{0, NULL}
};
```
Initialize the long options for getopt_long.
See `man getopt` for details.
```c
static struct option g_long_opts[] = {
{"help", no_argument, NULL, 'h'},
{"ext", required_argument, NULL, 'e'},
{"input", required_argument, NULL, 'i'},
{"output", required_argument, NULL, 'o'},
{"map", required_argument, NULL, 'm'},
{NULL, 0, NULL, 0}
};
```
### Function Definitions
### `cli_parse`
Parses command-line arguments and populates the t_args structure.
#### Parameters
- `args`: Pointer to the t_args structure to populate.
- `argc`: Argument count.
- `argv`: argv.
#### Return Value
Returns 0 on success, or -1 on failure.
#### Implementation
```c
int8_t
cli_parse(t_args *args, int32_t argc, char **argv)
{
int32_t opt;
t_handler handler;
init_args(args);
while (-1 != (opt = getopt_long(argc, argv, "he:i:o:m:", g_long_opts, NULL)))
{
handler = find_handler((int8_t)opt);
if (NULL == handler)
return (-1);
handler(args, optarg);
}
return (0);
}
```
### `init_args`
Initializes the t_args structure with default values.
#### Parameters
- `args`: Pointer to the t_args structure to initialize.
#### Implementation
```c
static void
init_args(t_args *args)
{
args->ext = NULL;
args->input = NULL;
args->output = NULL;
args->map_path = NULL;
args->show_help = 0;
}
```
### `find_handler`
Finds the handler function for a given option.
#### Parameters
- `opt`: The short option character.
#### Return Value
Returns the corresponding handler function, or NULL if not found.
#### Implementation
```c
static t_handler
find_handler(int8_t opt)
{
size_t i;
i = 0;
while (0 != g_opts[i].short_opt)
{
if (opt == g_opts[i].short_opt)
return (g_opts[i].handler);
i++;
}
return (NULL);
}
```
### `handle_help`
Sets the show_help flag in t_args.
```c
static void
handle_help(t_args *args, const char *value)
{
(void)value;
args->show_help = 1;
}
```
### `handle_ext`
Sets the file extension in t_args.
```c
static void
handle_ext(t_args *args, const char *value)
{
args->ext = value;
}
```
### `handle_input`
Sets the input file path in t_args.
```c
static void
handle_input(t_args *args, const char *value)
{
args->input = value;
}
```
### `handle_output`
Sets the output file path in t_args.
```c
static void
handle_output(t_args *args, const char *value)
{
args->output = value;
}
```
### `handle_map`
Sets the mapping file path in t_args.
```c
static void
handle_map(t_args *args, const char *value)
{
args->map_path = value;
}
```