42sh/src/utils/args/args.c

65 lines
1.8 KiB
C
Raw Normal View History

2026-01-12 18:42:02 +00:00
#include "./args.h"
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
int args_handler(int argc, char **argv, struct args_options *options)
{
options->type = INPUT_UNDEFINED;
options->input_source = NULL;
options->pretty_print = false;
options->verbose = false;
for (int i = 1; i < argc; i++)
{
if (strcmp(argv[i], "--pretty-print") == 0)
{
options->pretty_print = true;
}
else if (strcmp(argv[i], "--verbose") == 0)
{
options->verbose = true;
}
else if (strcmp(argv[i], "-c") == 0 && i + 1 < argc)
{
if (options->type != INPUT_UNDEFINED)
return 1; // Error: multiple input types specified
options->type = INPUT_CMD;
options->input_source = argv[i + 1];
i++;
}
else if (argv[i][0] == '-')
{
return 1; // Error: unknown option
}
else
{
if (options->type != INPUT_UNDEFINED)
return 1; // Error: multiple input types specified
options->type = INPUT_FILE;
options->input_source = argv[i];
}
}
if (options->type == INPUT_UNDEFINED)
options->type = INPUT_STDIN;
return 0;
}
void args_print(struct args_options *options)
{
printf("Input type: %s\n",
options->type == INPUT_CMD ? "COMMAND"
: options->type == INPUT_FILE ? "FILE"
: options->type == INPUT_STDIN ? "STDIN"
: "UNDEFINED");
printf("Input source: %s\n",
options->input_source ? options->input_source : "NULL");
printf("Pretty print: %s\n", options->pretty_print ? "true" : "false");
printf("Verbose: %s\n", options->verbose ? "true" : "false");
}