Files
fx/cmd/arg.c

175 lines
3.3 KiB
C
Raw Normal View History

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