Files
cig/cli.c
T
2025-12-06 14:24:55 +01:00

111 lines
2.6 KiB
C

#include "cig.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
bool cli_command(args_t *args, const char *command_name) {
if (args->count == 0) {
return false;
}
if (strcmp(args->values[0], command_name) == 0) {
args->values++;
args->count--;
return true;
}
return false;
}
bool cli_bool(args_t args, const char *flag_name) {
for ( int i = 0; i < args.count; i++ ) {
if (strcmp(args.values[i], flag_name) == 0) {
return true;
}
}
return false;
}
bool cli_opt_str_func(args_t args, const char *flag_name, const char **output, const char *file, int line) {
bool is_flag_name_found = false;
int flag_name_index;
for ( int i = 0; i < args.count; i++ ) {
if (strcmp(args.values[i], flag_name) == 0) {
is_flag_name_found = true;
flag_name_index = i;
break;
}
}
static const char *dummy = "";
if (!is_flag_name_found) {
*output = dummy;
return false;
}
int flag_value_index = flag_name_index + 1;
if (args.count <= flag_value_index) {
fprintf(stderr, "%s:%d: No value provided for %s!\n", file, line, flag_name);
exit(1);
}
*output = args.values[flag_value_index];
return true;
}
const char *cli_req_str_func(args_t args, const char *flag_name, const char *file, int line) {
const char *value;
if (cli_opt_str_func(args, flag_name, &value, file, line)) {
return value;
} else {
fprintf(stderr, "%s:%d: Required string %s not provided!\n", file, line, flag_name);
exit(1);
}
return "";
}
bool cli_opt_int_func(args_t args, const char *flag_name, int *output, const char *file, int line) {
const char *text = "";
if (cli_opt_str_func(args, flag_name, &text, file, line)) {
if (
text[0] != '-' &&
text[0] < '0' &&
'9' < text[0]
) {
fprintf(stderr, "%s:%d: Invalid integer %s!\n", file, line, text);
exit(1);
}
if (
text[0] == '-' &&
text[1] == '\0'
) {
fprintf(stderr, "%s:%d: Invalid integer %s!\n", file, line, text);
exit(1);
}
for ( const char *c = &text[1]; (*c) != '\0'; c++ ) {
if ((*c) < '0' || '9' < (*c)) {
fprintf(stderr, "%s:%d: Invalid integer %s!\n", file, line, text);
exit(1);
}
}
*output = atoi(text);
return true;
}
*output = 0;
return false;
}
int cli_req_int_func(args_t args, const char *flag_name, const char *file, int line) {
int value;
if (cli_opt_int_func(args, flag_name, &value, file, line)) {
return value;
} else {
fprintf(stderr, "%s:%d: Required int %s not provided!\n", file, line, flag_name);
exit(1);
}
return 0;
}
args_t cli_make_args(int argc, const char **argv) {
return (args_t) {
.count = argc-1,
.values = argv+1,
};
}