2024-10-24 21:32:28 +01:00
|
|
|
#include "command.h"
|
|
|
|
|
|
|
|
|
|
#include <blue/cmd.h>
|
2025-08-09 19:57:42 +01:00
|
|
|
#include <blue/ds/string.h>
|
2024-10-24 21:32:28 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
struct b_command_arg *b_command_arg_create(void)
|
|
|
|
|
{
|
|
|
|
|
struct b_command_arg *out = malloc(sizeof *out);
|
|
|
|
|
if (!out) {
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memset(out, 0x0, sizeof *out);
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-27 19:43:05 +00:00
|
|
|
void b_command_arg_destroy(struct b_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);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-24 21:32:28 +01:00
|
|
|
b_status b_command_arg_set_name(struct b_command_arg *arg, const char *name)
|
|
|
|
|
{
|
|
|
|
|
char *n = b_strdup(name);
|
|
|
|
|
if (!n) {
|
|
|
|
|
return B_ERR_NO_MEMORY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (arg->arg_name) {
|
|
|
|
|
free(arg->arg_name);
|
|
|
|
|
arg->arg_name = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
arg->arg_name = n;
|
|
|
|
|
return B_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b_status b_command_arg_set_description(
|
|
|
|
|
struct b_command_arg *arg, const char *description)
|
|
|
|
|
{
|
|
|
|
|
char *desc = b_strdup(description);
|
|
|
|
|
if (!desc) {
|
|
|
|
|
return B_ERR_NO_MEMORY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (arg->arg_description) {
|
|
|
|
|
free(arg->arg_description);
|
|
|
|
|
arg->arg_description = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
arg->arg_description = desc;
|
|
|
|
|
return B_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b_status b_command_arg_set_nr_values(
|
|
|
|
|
struct b_command_arg *arg, enum b_command_arg_value_count nr_values)
|
|
|
|
|
{
|
|
|
|
|
arg->arg_nr_values = nr_values;
|
|
|
|
|
return B_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b_status b_command_arg_set_allowed_values(
|
|
|
|
|
struct b_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 B_ERR_NO_MEMORY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < count; i++) {
|
|
|
|
|
values[i] = b_strdup(allowed_values[i]);
|
|
|
|
|
if (!values[i]) {
|
|
|
|
|
/* TODO also free strings in `values` */
|
|
|
|
|
free(values);
|
|
|
|
|
return B_ERR_NO_MEMORY;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
arg->arg_allowed_values = values;
|
|
|
|
|
return B_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-19 21:02:12 +01:00
|
|
|
void z__b_get_arg_usage_string(struct b_command_arg *arg, bool colour, b_string *out)
|
2024-10-24 21:32:28 +01:00
|
|
|
{
|
|
|
|
|
bool optional = false, multi = false;
|
|
|
|
|
switch (arg->arg_nr_values) {
|
|
|
|
|
case B_ARG_0_OR_1_VALUES:
|
|
|
|
|
optional = true;
|
|
|
|
|
multi = false;
|
|
|
|
|
break;
|
|
|
|
|
case B_ARG_0_OR_MORE_VALUES:
|
|
|
|
|
optional = true;
|
|
|
|
|
multi = true;
|
|
|
|
|
break;
|
|
|
|
|
case B_ARG_1_OR_MORE_VALUES:
|
|
|
|
|
optional = false;
|
|
|
|
|
multi = true;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
optional = false;
|
|
|
|
|
multi = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (optional) {
|
|
|
|
|
b_string_append_cstrf(
|
2025-10-12 17:29:03 +01:00
|
|
|
out, colour ? F_GREEN "[[%s]" : "[[%s]", arg->arg_name);
|
2024-10-24 21:32:28 +01:00
|
|
|
} else {
|
|
|
|
|
b_string_append_cstrf(
|
|
|
|
|
out, colour ? F_GREEN "<%s>" : "<%s>", arg->arg_name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 1; i < arg->arg_nr_values; i++) {
|
|
|
|
|
b_string_append_cstrf(out, " <%s>", arg->arg_name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (multi) {
|
|
|
|
|
b_string_append_cstr(out, "...");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (colour) {
|
|
|
|
|
b_string_append_cstr(out, F_RESET);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void z__b_get_arg_description(struct b_command_arg *arg, b_string *out)
|
|
|
|
|
{
|
|
|
|
|
if (arg->arg_description) {
|
|
|
|
|
b_string_append_cstr(out, arg->arg_description);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!arg->arg_allowed_values) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (arg->arg_description) {
|
|
|
|
|
b_string_append_cstr(out, " ");
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-14 22:03:39 +00:00
|
|
|
b_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) {
|
|
|
|
|
b_string_append_cstr(out, ",");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b_string_append_cstrf(
|
|
|
|
|
out, " " F_GREEN "%s" F_RESET, arg->arg_allowed_values[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b_string_append_cstr(out, "]");
|
|
|
|
|
}
|