Files
fx/cmd/arg.c
2026-03-16 10:35:43 +00:00

175 lines
3.3 KiB
C

#include "command.h"
#include <fx/cmd.h>
#include <fx/ds/string.h>
#include <stdlib.h>
#include <string.h>
struct fx_command_arg *fx_command_arg_create(void)
{
struct fx_command_arg *out = malloc(sizeof *out);
if (!out) {
return out;
}
memset(out, 0x0, sizeof *out);
return out;
}
void fx_command_arg_destroy(struct fx_command_arg *arg)
{
if (arg->arg_name) {
free(arg->arg_name);
}
if (arg->arg_description) {
free(arg->arg_description);
}
if (arg->arg_allowed_values) {
for (unsigned int i = 0; arg->arg_allowed_values[i]; i++) {
free(arg->arg_allowed_values[i]);
}
free(arg->arg_allowed_values);
}
free(arg);
}
fx_status fx_command_arg_set_name(struct fx_command_arg *arg, const char *name)
{
char *n = fx_strdup(name);
if (!n) {
return FX_ERR_NO_MEMORY;
}
if (arg->arg_name) {
free(arg->arg_name);
arg->arg_name = NULL;
}
arg->arg_name = n;
return FX_SUCCESS;
}
fx_status fx_command_arg_set_description(
struct fx_command_arg *arg, const char *description)
{
char *desc = fx_strdup(description);
if (!desc) {
return FX_ERR_NO_MEMORY;
}
if (arg->arg_description) {
free(arg->arg_description);
arg->arg_description = NULL;
}
arg->arg_description = desc;
return FX_SUCCESS;
}
fx_status fx_command_arg_set_nr_values(
struct fx_command_arg *arg, enum fx_command_arg_value_count nr_values)
{
arg->arg_nr_values = nr_values;
return FX_SUCCESS;
}
fx_status fx_command_arg_set_allowed_values(
struct fx_command_arg *arg, const char **allowed_values)
{
size_t count;
for (count = 0; allowed_values[count]; count++)
;
char **values = calloc(count + 1, sizeof *values);
if (!values) {
return FX_ERR_NO_MEMORY;
}
for (size_t i = 0; i < count; i++) {
values[i] = fx_strdup(allowed_values[i]);
if (!values[i]) {
/* TODO also free strings in `values` */
free(values);
return FX_ERR_NO_MEMORY;
}
}
arg->arg_allowed_values = values;
return FX_SUCCESS;
}
void z__fx_get_arg_usage_string(struct fx_command_arg *arg, bool colour, fx_string *out)
{
bool optional = false, multi = false;
switch (arg->arg_nr_values) {
case FX_ARG_0_OR_1_VALUES:
optional = true;
multi = false;
break;
case FX_ARG_0_OR_MORE_VALUES:
optional = true;
multi = true;
break;
case FX_ARG_1_OR_MORE_VALUES:
optional = false;
multi = true;
break;
default:
optional = false;
multi = false;
break;
}
if (optional) {
fx_string_append_cstrf(
out, colour ? F_GREEN "[[%s]" : "[[%s]", arg->arg_name);
} else {
fx_string_append_cstrf(
out, colour ? F_GREEN "<%s>" : "<%s>", arg->arg_name);
}
for (int i = 1; i < arg->arg_nr_values; i++) {
fx_string_append_cstrf(out, " <%s>", arg->arg_name);
}
if (multi) {
fx_string_append_cstr(out, "...");
}
if (colour) {
fx_string_append_cstr(out, F_RESET);
}
}
void z__fx_get_arg_description(struct fx_command_arg *arg, fx_string *out)
{
if (arg->arg_description) {
fx_string_append_cstr(out, arg->arg_description);
}
if (!arg->arg_allowed_values) {
return;
}
if (arg->arg_description) {
fx_string_append_cstr(out, " ");
}
fx_string_append_cstr(out, "[[values:");
for (size_t i = 0; arg->arg_allowed_values[i]; i++) {
if (i > 0) {
fx_string_append_cstr(out, ",");
}
fx_string_append_cstrf(
out, " " F_GREEN "%s" F_RESET, arg->arg_allowed_values[i]);
}
fx_string_append_cstr(out, "]");
}