meta: rename to fx

This commit is contained in:
2026-03-16 10:35:43 +00:00
parent 84df46489a
commit e9d0e323f0
233 changed files with 12875 additions and 12869 deletions

View File

@@ -1,3 +1,3 @@
include(../cmake/Templates.cmake)
add_bluelib_module(NAME cmd DEPENDENCIES core ds term)
add_fx_module(NAME cmd DEPENDENCIES core ds term)

View File

@@ -1,13 +1,13 @@
#include "command.h"
#include <blue/cmd.h>
#include <blue/ds/string.h>
#include <fx/cmd.h>
#include <fx/ds/string.h>
#include <stdlib.h>
#include <string.h>
struct b_command_arg *b_command_arg_create(void)
struct fx_command_arg *fx_command_arg_create(void)
{
struct b_command_arg *out = malloc(sizeof *out);
struct fx_command_arg *out = malloc(sizeof *out);
if (!out) {
return out;
}
@@ -16,7 +16,7 @@ struct b_command_arg *b_command_arg_create(void)
return out;
}
void b_command_arg_destroy(struct b_command_arg *arg)
void fx_command_arg_destroy(struct fx_command_arg *arg)
{
if (arg->arg_name) {
free(arg->arg_name);
@@ -37,11 +37,11 @@ void b_command_arg_destroy(struct b_command_arg *arg)
free(arg);
}
b_status b_command_arg_set_name(struct b_command_arg *arg, const char *name)
fx_status fx_command_arg_set_name(struct fx_command_arg *arg, const char *name)
{
char *n = b_strdup(name);
char *n = fx_strdup(name);
if (!n) {
return B_ERR_NO_MEMORY;
return FX_ERR_NO_MEMORY;
}
if (arg->arg_name) {
@@ -50,15 +50,15 @@ b_status b_command_arg_set_name(struct b_command_arg *arg, const char *name)
}
arg->arg_name = n;
return B_SUCCESS;
return FX_SUCCESS;
}
b_status b_command_arg_set_description(
struct b_command_arg *arg, const char *description)
fx_status fx_command_arg_set_description(
struct fx_command_arg *arg, const char *description)
{
char *desc = b_strdup(description);
char *desc = fx_strdup(description);
if (!desc) {
return B_ERR_NO_MEMORY;
return FX_ERR_NO_MEMORY;
}
if (arg->arg_description) {
@@ -67,18 +67,18 @@ b_status b_command_arg_set_description(
}
arg->arg_description = desc;
return B_SUCCESS;
return FX_SUCCESS;
}
b_status b_command_arg_set_nr_values(
struct b_command_arg *arg, enum b_command_arg_value_count nr_values)
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 B_SUCCESS;
return FX_SUCCESS;
}
b_status b_command_arg_set_allowed_values(
struct b_command_arg *arg, const char **allowed_values)
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++)
@@ -86,35 +86,35 @@ b_status b_command_arg_set_allowed_values(
char **values = calloc(count + 1, sizeof *values);
if (!values) {
return B_ERR_NO_MEMORY;
return FX_ERR_NO_MEMORY;
}
for (size_t i = 0; i < count; i++) {
values[i] = b_strdup(allowed_values[i]);
values[i] = fx_strdup(allowed_values[i]);
if (!values[i]) {
/* TODO also free strings in `values` */
free(values);
return B_ERR_NO_MEMORY;
return FX_ERR_NO_MEMORY;
}
}
arg->arg_allowed_values = values;
return B_SUCCESS;
return FX_SUCCESS;
}
void z__b_get_arg_usage_string(struct b_command_arg *arg, bool colour, b_string *out)
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 B_ARG_0_OR_1_VALUES:
case FX_ARG_0_OR_1_VALUES:
optional = true;
multi = false;
break;
case B_ARG_0_OR_MORE_VALUES:
case FX_ARG_0_OR_MORE_VALUES:
optional = true;
multi = true;
break;
case B_ARG_1_OR_MORE_VALUES:
case FX_ARG_1_OR_MORE_VALUES:
optional = false;
multi = true;
break;
@@ -125,30 +125,30 @@ void z__b_get_arg_usage_string(struct b_command_arg *arg, bool colour, b_string
}
if (optional) {
b_string_append_cstrf(
fx_string_append_cstrf(
out, colour ? F_GREEN "[[%s]" : "[[%s]", arg->arg_name);
} else {
b_string_append_cstrf(
fx_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);
fx_string_append_cstrf(out, " <%s>", arg->arg_name);
}
if (multi) {
b_string_append_cstr(out, "...");
fx_string_append_cstr(out, "...");
}
if (colour) {
b_string_append_cstr(out, F_RESET);
fx_string_append_cstr(out, F_RESET);
}
}
void z__b_get_arg_description(struct b_command_arg *arg, b_string *out)
void z__fx_get_arg_description(struct fx_command_arg *arg, fx_string *out)
{
if (arg->arg_description) {
b_string_append_cstr(out, arg->arg_description);
fx_string_append_cstr(out, arg->arg_description);
}
if (!arg->arg_allowed_values) {
@@ -156,19 +156,19 @@ void z__b_get_arg_description(struct b_command_arg *arg, b_string *out)
}
if (arg->arg_description) {
b_string_append_cstr(out, " ");
fx_string_append_cstr(out, " ");
}
b_string_append_cstr(out, "[[values:");
fx_string_append_cstr(out, "[[values:");
for (size_t i = 0; arg->arg_allowed_values[i]; i++) {
if (i > 0) {
b_string_append_cstr(out, ",");
fx_string_append_cstr(out, ",");
}
b_string_append_cstrf(
fx_string_append_cstrf(
out, " " F_GREEN "%s" F_RESET, arg->arg_allowed_values[i]);
}
b_string_append_cstr(out, "]");
fx_string_append_cstr(out, "]");
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,10 +1,10 @@
#ifndef _B_COMMAND_H_
#define _B_COMMAND_H_
#ifndef _FX_COMMAND_H_
#define _FX_COMMAND_H_
#include <blue/cmd.h>
#include <blue/core/btree.h>
#include <blue/core/queue.h>
#include <blue/ds/string.h>
#include <fx/cmd.h>
#include <fx/core/btree.h>
#include <fx/core/queue.h>
#include <fx/ds/string.h>
#define F_RED "[bright_red]"
#define F_GREEN "[bright_green]"
@@ -20,132 +20,132 @@ enum cmd_string_flags {
CMD_STR_DIRECT_USAGE = 0x02u,
};
enum b_command_usage_entry_type {
enum fx_command_usage_entry_type {
CMD_USAGE_NONE = 0,
CMD_USAGE_ARG,
CMD_USAGE_OPT,
CMD_USAGE_COMMAND,
};
struct b_command {
unsigned int b_id;
unsigned int b_parent_id;
struct b_command *b_parent;
enum b_command_flags b_flags;
struct fx_command {
unsigned int c_id;
unsigned int c_parent_id;
struct fx_command *c_parent;
enum fx_command_flags c_flags;
char *b_name;
char *b_long_name;
char b_short_name;
char *b_description;
struct b_queue b_opt;
struct b_queue b_arg;
struct b_queue b_subcommands;
struct b_queue b_usage;
char *c_name;
char *c_long_name;
char c_short_name;
char *c_description;
struct fx_queue c_opt;
struct fx_queue c_arg;
struct fx_queue c_subcommands;
struct fx_queue c_usage;
b_command_callback b_callback;
struct b_queue_entry b_entry;
struct b_btree_node b_node;
fx_command_callback c_callback;
struct fx_queue_entry c_entry;
struct fx_bst_node c_node;
};
struct b_command_usage_entry {
struct b_queue_entry e_entry;
enum b_command_usage_entry_type e_type;
struct fx_command_usage_entry {
struct fx_queue_entry e_entry;
enum fx_command_usage_entry_type e_type;
union {
struct b_command_option *e_opt;
struct b_command_arg *e_arg;
struct fx_command_option *e_opt;
struct fx_command_arg *e_arg;
unsigned int e_cmd_id;
};
};
struct b_command_usage {
b_command_usage_flags u_flags;
struct b_queue_entry u_entry;
struct fx_command_usage {
fx_command_usage_flags u_flags;
struct fx_queue_entry u_entry;
struct b_queue u_parts;
struct fx_queue u_parts;
};
struct b_command_option {
struct fx_command_option {
unsigned int opt_id;
char *opt_long_name;
char opt_short_name;
char *opt_description;
// b_command_arg_value_count arg_nr_values;
// fx_command_arg_value_count arg_nr_values;
// char **arg_allowed_values;
struct b_queue opt_args;
struct b_queue_entry opt_entry;
struct fx_queue opt_args;
struct fx_queue_entry opt_entry;
};
struct b_command_arg {
struct fx_command_arg {
unsigned int arg_id;
char *arg_name;
char *arg_description;
b_command_arg_value_count arg_nr_values;
fx_command_arg_value_count arg_nr_values;
char **arg_allowed_values;
struct b_queue_entry arg_entry;
struct fx_queue_entry arg_entry;
};
struct b_arglist_option {
struct fx_arglist_option {
unsigned int opt_id;
struct b_btree opt_values;
struct b_btree_node opt_node;
struct fx_bst opt_values;
struct fx_bst_node opt_node;
};
struct b_arglist {
struct fx_arglist {
size_t list_argc;
const char **list_argv;
unsigned int list_argv_last_command;
struct b_command *list_command;
struct b_btree list_options;
struct fx_command *list_command;
struct fx_bst list_options;
};
BLUE_API struct b_command *b_command_get_subcommand_with_name(
struct b_command *cmd, const char *name);
BLUE_API struct b_command *b_command_get_subcommand_with_long_name(
struct b_command *cmd, const char *long_name);
BLUE_API struct b_command *b_command_get_subcommand_with_short_name(
struct b_command *cmd, char short_name);
FX_API struct fx_command *fx_command_get_subcommand_with_name(
struct fx_command *cmd, const char *name);
FX_API struct fx_command *fx_command_get_subcommand_with_long_name(
struct fx_command *cmd, const char *long_name);
FX_API struct fx_command *fx_command_get_subcommand_with_short_name(
struct fx_command *cmd, char short_name);
BLUE_API struct b_command_option *b_command_get_option_with_long_name(
struct b_command *cmd, const char *long_name);
BLUE_API struct b_command_option *b_command_get_option_with_short_name(
struct b_command *cmd, char short_name);
BLUE_API struct b_command_option *b_command_get_option_with_id(
struct b_command *cmd, unsigned int id);
BLUE_API struct b_command_arg *b_command_get_arg_with_id(
struct b_command *cmd, unsigned int id);
FX_API struct fx_command_option *fx_command_get_option_with_long_name(
struct fx_command *cmd, const char *long_name);
FX_API struct fx_command_option *fx_command_get_option_with_short_name(
struct fx_command *cmd, char short_name);
FX_API struct fx_command_option *fx_command_get_option_with_id(
struct fx_command *cmd, unsigned int id);
FX_API struct fx_command_arg *fx_command_get_arg_with_id(
struct fx_command *cmd, unsigned int id);
BLUE_API struct b_command_arg *b_command_option_get_arg_with_id(
struct b_command_option *opt, unsigned int id);
FX_API struct fx_command_arg *fx_command_option_get_arg_with_id(
struct fx_command_option *opt, unsigned int id);
BLUE_API struct b_command_option *b_command_option_create(void);
BLUE_API void b_command_option_destroy(struct b_command_option *opt);
FX_API struct fx_command_option *fx_command_option_create(void);
FX_API void fx_command_option_destroy(struct fx_command_option *opt);
BLUE_API struct b_command_arg *b_command_arg_create(void);
BLUE_API void b_command_arg_destroy(struct b_command_arg *arg);
FX_API struct fx_command_arg *fx_command_arg_create(void);
FX_API void fx_command_arg_destroy(struct fx_command_arg *arg);
BLUE_API struct b_arglist *b_arglist_create(void);
BLUE_API b_status b_arglist_parse(
struct b_arglist *args, struct b_command **cmd, int argc,
FX_API struct fx_arglist *fx_arglist_create(void);
FX_API fx_status fx_arglist_parse(
struct fx_arglist *args, struct fx_command **cmd, int argc,
const char **argv);
BLUE_API void b_arglist_destroy(struct b_arglist *args);
FX_API void fx_arglist_destroy(struct fx_arglist *args);
BLUE_API b_string *z__b_command_default_usage_string(
struct b_command *cmd, struct b_command_option *with_opt,
const struct b_arglist *args);
FX_API fx_string *z__fx_command_default_usage_string(
struct fx_command *cmd, struct fx_command_option *with_opt,
const struct fx_arglist *args);
BLUE_API void z__b_get_arg_usage_string(
struct b_command_arg *arg, bool colour, b_string *out);
BLUE_API void z__b_get_arg_description(struct b_command_arg *arg, b_string *out);
FX_API void z__fx_get_arg_usage_string(
struct fx_command_arg *arg, bool colour, fx_string *out);
FX_API void z__fx_get_arg_description(struct fx_command_arg *arg, fx_string *out);
BLUE_API void z__b_get_option_usage_string(
struct b_command_option *opt, enum cmd_string_flags flags, b_string *out);
BLUE_API void z__b_get_option_description(
struct b_command_option *opt, b_string *out);
FX_API void z__fx_get_option_usage_string(
struct fx_command_option *opt, enum cmd_string_flags flags, fx_string *out);
FX_API void z__fx_get_option_description(
struct fx_command_option *opt, fx_string *out);
#endif

View File

@@ -1,289 +0,0 @@
#ifndef BLUE_CMD_H_
#define BLUE_CMD_H_
#include <blue/core/btree.h>
#include <blue/core/init.h>
#include <blue/core/iterator.h>
#include <blue/core/queue.h>
#include <blue/ds/array.h>
#include <stdbool.h>
#include <stdint.h>
#define b_arglist_foreach(it, q) \
for (int z__b_unique_name() = b_arglist_iterator_begin( \
q, B_COMMAND_INVALID_ID, B_COMMAND_INVALID_ID, (it)); \
b_arglist_iterator_is_valid(it); b_arglist_iterator_next(it))
#define b_arglist_foreach_filtered(it, q, opt_id, arg_id) \
for (int z__b_unique_name() \
= b_arglist_iterator_begin(q, opt_id, arg_id, (it)); \
b_arglist_iterator_is_valid(it); b_arglist_iterator_next(it))
#define b_arglist_option_foreach(it, q) \
for (int z__b_unique_name() \
= b_arglist_option_iterator_begin(q, B_COMMAND_INVALID_ID, (it)); \
b_arglist_option_iterator_is_valid(it); \
b_arglist_option_iterator_next(it))
#define b_arglist_option_foreach_filtered(it, q, opt_id) \
for (int z__b_unique_name() \
= b_arglist_option_iterator_begin(q, opt_id, (it)); \
b_arglist_option_iterator_is_valid(it); \
b_arglist_option_iterator_next(it))
#define B_COMMAND(id, parent_id) \
static b_command *command_##id = NULL; \
static void __init_##id( \
b_command *, b_command_option *, b_command_arg *, \
b_command_usage *); \
B_INIT(init_##id) \
{ \
command_##id = b_command_create(id); \
if ((parent_id) != B_COMMAND_INVALID_ID) { \
b_command_set_parent(command_##id, parent_id); \
} \
__init_##id(command_##id, NULL, NULL, NULL); \
b_command_register(command_##id); \
} \
static void __init_##id( \
b_command *this_cmd, b_command_option *this_opt, \
b_command_arg *this_arg, b_command_usage *this_usage)
#define B_COMMAND_NAME(name) b_command_set_name(this_cmd, (name))
#define B_COMMAND_LONG_NAME(name) b_command_set_long_name(this_cmd, (name))
#define B_COMMAND_SHORT_NAME(name) b_command_set_short_name(this_cmd, (name))
#define B_COMMAND_DESC(desc) b_command_set_description(this_cmd, (desc))
#define B_COMMAND_FLAGS(flags) b_command_set_flags(this_cmd, (flags))
#define B_COMMAND_FUNCTION(fn) b_command_set_callback(this_cmd, (fn))
#define B_COMMAND_OPTION(id) \
b_command_option *opt_##id = b_command_add_option(this_cmd, (id)); \
this_opt = opt_##id; \
if (this_opt)
#define B_COMMAND_OPTION_GEN(id) \
b_command_option *z__b_unique_name() \
= b_command_add_option(this_cmd, (id)); \
this_opt = z__b_unique_name(); \
if (this_opt)
#define B_COMMAND_HELP_OPTION() \
do { \
b_command_option *opt \
= b_command_add_option(this_cmd, B_COMMAND_OPTION_HELP); \
b_command_option_set_description(opt, "Show this help message"); \
b_command_option_set_short_name(opt, 'h'); \
b_command_option_set_long_name(opt, "help"); \
} while (0)
#define B_OPTION_LONG_NAME(name) \
b_command_option_set_long_name(this_opt, (name))
#define B_OPTION_SHORT_NAME(name) \
b_command_option_set_short_name(this_opt, (name))
#define B_OPTION_DESC(desc) b_command_option_set_description(this_opt, (desc))
#define B_OPTION_ARG(id) \
b_command_arg *arg_##id = b_command_option_add_arg(this_opt, (id)); \
this_arg = arg_##id; \
if (this_arg)
#define B_COMMAND_ARG(id) \
b_command_arg *arg_##id = b_command_add_arg(this_cmd, (id)); \
this_arg = arg_##id; \
if (this_arg)
#define B_ARG_NAME(name) b_command_arg_set_name(this_arg, (name))
#define B_ARG_DESC(desc) b_command_arg_set_description(this_arg, (desc))
#define B_ARG_NR_VALUES(nr_values) \
b_command_arg_set_nr_values(this_arg, (nr_values))
#define B_ARG_ALLOWED_VALUES(...) \
static const char *allowed_values[] = { \
__VA_ARGS__, \
NULL, \
}; \
b_command_arg_set_allowed_values(this_arg, allowed_values)
#define B_COMMAND_USAGE() \
b_command_usage *z__b_unique_name() = b_command_add_usage(this_cmd); \
this_usage = z__b_unique_name(); \
if (this_usage)
#define B_COMMAND_USAGE_COMMAND(cmd_id) \
b_command_usage_add_command(this_usage, cmd_id)
#define B_COMMAND_USAGE_COMMAND_PLACEHOLDER() \
b_command_usage_add_command(this_usage, B_COMMAND_INVALID_ID)
#define B_COMMAND_USAGE_OPT(opt_id) \
b_command_usage_add_option(this_usage, opt_##opt_id)
#define B_COMMAND_USAGE_OPT_PLACEHOLDER() \
b_command_usage_add_option(this_usage, NULL)
#define B_COMMAND_USAGE_ARG(opt_id) \
b_command_usage_add_arg(this_usage, arg_##opt_id)
#define B_COMMAND_USAGE_ARG_PLACEHOLDER() \
b_command_usage_add_arg(this_usage, NULL)
#define B_COMMAND_OPTION_HELP ((uintptr_t)0xF0000001)
#define B_COMMAND_INVALID_ID ((uintptr_t)0xFFFFFFFF)
typedef enum b_command_arg_value_count {
B_ARG_0_OR_1_VALUES = -1,
B_ARG_0_OR_MORE_VALUES = -2,
B_ARG_1_OR_MORE_VALUES = -3,
} b_command_arg_value_count;
typedef enum b_command_arg_type {
B_COMMAND_ARG_NONE = 0,
B_COMMAND_ARG_STRING,
B_COMMAND_ARG_SIGNED_INT,
B_COMMAND_ARG_UNSIGNED_INT,
B_COMMAND_ARG_INT = B_COMMAND_ARG_SIGNED_INT,
} b_command_arg_type;
typedef enum b_command_flags {
B_COMMAND_SHOW_HELP_BY_DEFAULT = 0x01u,
} b_command_flags;
typedef enum b_command_usage_flags {
B_COMMAND_USAGE_SHOW_OPTIONS = 0x01u,
} b_command_usage_flags;
typedef struct b_arglist_value {
unsigned int val_id;
b_command_arg_type val_type;
struct b_btree_node val_node;
union {
char *val_str;
long long val_int;
unsigned long long val_uint;
};
} b_arglist_value;
typedef struct b_arglist_iterator {
size_t i;
unsigned int opt_id;
struct b_arglist_value *value;
b_btree_node *_opt_it, *_arg_it;
unsigned int _opt_filter, _arg_filter;
} b_arglist_iterator;
typedef struct b_arglist_option_iterator {
size_t i;
unsigned int opt_id;
struct b_arglist_option *opt;
b_btree_node *_opt_it;
unsigned int _opt_filter;
} b_arglist_option_iterator;
typedef struct b_command b_command;
typedef struct b_command_option b_command_option;
typedef struct b_command_arg b_command_arg;
typedef struct b_command_usage b_command_usage;
typedef struct b_arglist b_arglist;
typedef struct b_arglist_option b_arglist_option;
typedef int (*b_command_callback)(
const b_command *, const b_arglist *, const b_array *);
BLUE_API b_command *b_command_create(unsigned int id);
BLUE_API void b_command_destroy(b_command *cmd);
BLUE_API b_status b_command_register(b_command *cmd);
BLUE_API int b_command_dispatch(unsigned int cmd_id, int argc, const char **argv);
BLUE_API b_status b_command_set_name(b_command *cmd, const char *name);
BLUE_API b_status b_command_set_long_name(b_command *cmd, const char *name);
BLUE_API b_status b_command_set_short_name(b_command *cmd, char name);
BLUE_API b_status b_command_set_flags(b_command *cmd, b_command_flags flags);
BLUE_API b_status b_command_set_description(b_command *cmd, const char *description);
BLUE_API b_status b_command_set_callback(
b_command *cmd, b_command_callback callback);
BLUE_API b_status b_command_set_parent(b_command *cmd, unsigned int parent_id);
BLUE_API b_command_option *b_command_add_option(b_command *cmd, int id);
BLUE_API b_command_arg *b_command_add_arg(b_command *cmd, int id);
BLUE_API b_command_usage *b_command_add_usage(b_command *cmd);
BLUE_API const b_command_option *b_command_get_option(const b_command *cmd, int id);
BLUE_API const char *b_command_option_get_long_name(const b_command_option *opt);
BLUE_API char b_command_option_get_short_name(const b_command_option *opt);
BLUE_API const char *b_command_option_get_description(b_command_option *opt);
BLUE_API b_status b_command_option_set_long_name(
b_command_option *opt, const char *name);
BLUE_API b_status b_command_option_set_short_name(b_command_option *opt, char name);
BLUE_API b_status b_command_option_set_description(
b_command_option *opt, const char *description);
BLUE_API b_command_arg *b_command_option_add_arg(b_command_option *opt, int id);
BLUE_API b_status b_command_arg_set_name(b_command_arg *arg, const char *name);
BLUE_API b_status b_command_arg_set_description(
b_command_arg *arg, const char *description);
BLUE_API b_status b_command_arg_set_nr_values(
b_command_arg *arg, b_command_arg_value_count nr_values);
BLUE_API b_status b_command_arg_set_allowed_values(
b_command_arg *arg, const char **allowed_values);
BLUE_API b_status b_command_usage_add_option(
b_command_usage *usage, b_command_option *opt);
BLUE_API b_status b_command_usage_add_arg(
b_command_usage *usage, b_command_arg *opt);
BLUE_API b_status b_command_usage_add_command(
b_command_usage *usage, unsigned int cmd_id);
BLUE_API b_status b_arglist_get_string(
const b_arglist *args, unsigned int opt_id, unsigned int arg_id,
unsigned int index, const char **out);
BLUE_API b_status b_arglist_get_int(
const b_arglist *args, unsigned int opt_id, unsigned int arg_id,
unsigned int index, long long *out);
BLUE_API b_status b_arglist_get_uint(
const b_arglist *args, unsigned int opt_id, unsigned int arg_id,
unsigned int index, unsigned long long *out);
BLUE_API b_status b_arglist_get_option(
const b_arglist *args, unsigned int opt_id, unsigned int index,
b_arglist_option **out);
BLUE_API size_t b_arglist_get_count(
const b_arglist *args, unsigned int opt_id, unsigned int arg_id);
BLUE_API b_status b_arglist_report_missing_option(
const b_arglist *args, unsigned int opt_id);
BLUE_API b_status b_arglist_report_unexpected_arg(
const b_arglist *args, const char *value);
BLUE_API b_status b_arglist_report_invalid_arg_value(
const b_arglist *args, unsigned int opt_id, unsigned int arg_id,
const char *value);
BLUE_API b_status b_arglist_report_missing_args(
const b_arglist *args, unsigned int opt_id, unsigned int arg_id,
size_t nr_supplied);
BLUE_API b_status b_arglist_option_get_value(
const b_arglist_option *opt, unsigned int arg_id, unsigned int index,
b_arglist_value **out);
BLUE_API int b_arglist_iterator_begin(
const b_arglist *args, unsigned int opt_filter, unsigned int arg_filter,
b_arglist_iterator *it);
BLUE_API bool b_arglist_iterator_next(b_arglist_iterator *it);
BLUE_API bool b_arglist_iterator_is_valid(const b_arglist_iterator *it);
BLUE_API int b_arglist_option_iterator_begin(
const b_arglist *args, unsigned int opt_filter,
b_arglist_option_iterator *it);
BLUE_API bool b_arglist_option_iterator_next(b_arglist_option_iterator *it);
BLUE_API bool b_arglist_option_iterator_is_valid(
const b_arglist_option_iterator *it);
#endif

289
cmd/include/fx/cmd.h Normal file
View File

@@ -0,0 +1,289 @@
#ifndef FX_CMD_H_
#define FX_CMD_H_
#include <fx/core/btree.h>
#include <fx/core/init.h>
#include <fx/core/iterator.h>
#include <fx/core/queue.h>
#include <fx/ds/array.h>
#include <stdbool.h>
#include <stdint.h>
#define fx_arglist_foreach(it, q) \
for (int z__fx_unique_name() = fx_arglist_iterator_begin( \
q, FX_COMMAND_INVALID_ID, FX_COMMAND_INVALID_ID, (it)); \
fx_arglist_iterator_is_valid(it); fx_arglist_iterator_next(it))
#define fx_arglist_foreach_filtered(it, q, opt_id, arg_id) \
for (int z__fx_unique_name() \
= fx_arglist_iterator_begin(q, opt_id, arg_id, (it)); \
fx_arglist_iterator_is_valid(it); fx_arglist_iterator_next(it))
#define fx_arglist_option_foreach(it, q) \
for (int z__fx_unique_name() \
= fx_arglist_option_iterator_begin(q, FX_COMMAND_INVALID_ID, (it)); \
fx_arglist_option_iterator_is_valid(it); \
fx_arglist_option_iterator_next(it))
#define fx_arglist_option_foreach_filtered(it, q, opt_id) \
for (int z__fx_unique_name() \
= fx_arglist_option_iterator_begin(q, opt_id, (it)); \
fx_arglist_option_iterator_is_valid(it); \
fx_arglist_option_iterator_next(it))
#define FX_COMMAND(id, parent_id) \
static fx_command *command_##id = NULL; \
static void __init_##id( \
fx_command *, fx_command_option *, fx_command_arg *, \
fx_command_usage *); \
FX_INIT(init_##id) \
{ \
command_##id = fx_command_create(id); \
if ((parent_id) != FX_COMMAND_INVALID_ID) { \
fx_command_set_parent(command_##id, parent_id); \
} \
__init_##id(command_##id, NULL, NULL, NULL); \
fx_command_register(command_##id); \
} \
static void __init_##id( \
fx_command *this_cmd, fx_command_option *this_opt, \
fx_command_arg *this_arg, fx_command_usage *this_usage)
#define FX_COMMAND_NAME(name) fx_command_set_name(this_cmd, (name))
#define FX_COMMAND_LONG_NAME(name) fx_command_set_long_name(this_cmd, (name))
#define FX_COMMAND_SHORT_NAME(name) fx_command_set_short_name(this_cmd, (name))
#define FX_COMMAND_DESC(desc) fx_command_set_description(this_cmd, (desc))
#define FX_COMMAND_FLAGS(flags) fx_command_set_flags(this_cmd, (flags))
#define FX_COMMAND_FUNCTION(fn) fx_command_set_callback(this_cmd, (fn))
#define FX_COMMAND_OPTION(id) \
fx_command_option *opt_##id = fx_command_add_option(this_cmd, (id)); \
this_opt = opt_##id; \
if (this_opt)
#define FX_COMMAND_OPTION_GEN(id) \
fx_command_option *z__fx_unique_name() \
= fx_command_add_option(this_cmd, (id)); \
this_opt = z__fx_unique_name(); \
if (this_opt)
#define FX_COMMAND_HELP_OPTION() \
do { \
fx_command_option *opt \
= fx_command_add_option(this_cmd, FX_COMMAND_OPTION_HELP); \
fx_command_option_set_description(opt, "Show this help message"); \
fx_command_option_set_short_name(opt, 'h'); \
fx_command_option_set_long_name(opt, "help"); \
} while (0)
#define FX_OPTION_LONG_NAME(name) \
fx_command_option_set_long_name(this_opt, (name))
#define FX_OPTION_SHORT_NAME(name) \
fx_command_option_set_short_name(this_opt, (name))
#define FX_OPTION_DESC(desc) fx_command_option_set_description(this_opt, (desc))
#define FX_OPTION_ARG(id) \
fx_command_arg *arg_##id = fx_command_option_add_arg(this_opt, (id)); \
this_arg = arg_##id; \
if (this_arg)
#define FX_COMMAND_ARG(id) \
fx_command_arg *arg_##id = fx_command_add_arg(this_cmd, (id)); \
this_arg = arg_##id; \
if (this_arg)
#define FX_ARG_NAME(name) fx_command_arg_set_name(this_arg, (name))
#define FX_ARG_DESC(desc) fx_command_arg_set_description(this_arg, (desc))
#define FX_ARG_NR_VALUES(nr_values) \
fx_command_arg_set_nr_values(this_arg, (nr_values))
#define FX_ARG_ALLOWED_VALUES(...) \
static const char *allowed_values[] = { \
__VA_ARGS__, \
NULL, \
}; \
fx_command_arg_set_allowed_values(this_arg, allowed_values)
#define FX_COMMAND_USAGE() \
fx_command_usage *z__fx_unique_name() = fx_command_add_usage(this_cmd); \
this_usage = z__fx_unique_name(); \
if (this_usage)
#define FX_COMMAND_USAGE_COMMAND(cmd_id) \
fx_command_usage_add_command(this_usage, cmd_id)
#define FX_COMMAND_USAGE_COMMAND_PLACEHOLDER() \
fx_command_usage_add_command(this_usage, FX_COMMAND_INVALID_ID)
#define FX_COMMAND_USAGE_OPT(opt_id) \
fx_command_usage_add_option(this_usage, opt_##opt_id)
#define FX_COMMAND_USAGE_OPT_PLACEHOLDER() \
fx_command_usage_add_option(this_usage, NULL)
#define FX_COMMAND_USAGE_ARG(opt_id) \
fx_command_usage_add_arg(this_usage, arg_##opt_id)
#define FX_COMMAND_USAGE_ARG_PLACEHOLDER() \
fx_command_usage_add_arg(this_usage, NULL)
#define FX_COMMAND_OPTION_HELP ((uintptr_t)0xF0000001)
#define FX_COMMAND_INVALID_ID ((uintptr_t)0xFFFFFFFF)
typedef enum fx_command_arg_value_count {
FX_ARG_0_OR_1_VALUES = -1,
FX_ARG_0_OR_MORE_VALUES = -2,
FX_ARG_1_OR_MORE_VALUES = -3,
} fx_command_arg_value_count;
typedef enum fx_command_arg_type {
FX_COMMAND_ARG_NONE = 0,
FX_COMMAND_ARG_STRING,
FX_COMMAND_ARG_SIGNED_INT,
FX_COMMAND_ARG_UNSIGNED_INT,
FX_COMMAND_ARG_INT = FX_COMMAND_ARG_SIGNED_INT,
} fx_command_arg_type;
typedef enum fx_command_flags {
FX_COMMAND_SHOW_HELP_BY_DEFAULT = 0x01u,
} fx_command_flags;
typedef enum fx_command_usage_flags {
FX_COMMAND_USAGE_SHOW_OPTIONS = 0x01u,
} fx_command_usage_flags;
typedef struct fx_arglist_value {
unsigned int val_id;
fx_command_arg_type val_type;
struct fx_bst_node val_node;
union {
char *val_str;
long long val_int;
unsigned long long val_uint;
};
} fx_arglist_value;
typedef struct fx_arglist_iterator {
size_t i;
unsigned int opt_id;
struct fx_arglist_value *value;
fx_bst_node *_opt_it, *_arg_it;
unsigned int _opt_filter, _arg_filter;
} fx_arglist_iterator;
typedef struct fx_arglist_option_iterator {
size_t i;
unsigned int opt_id;
struct fx_arglist_option *opt;
fx_bst_node *_opt_it;
unsigned int _opt_filter;
} fx_arglist_option_iterator;
typedef struct fx_command fx_command;
typedef struct fx_command_option fx_command_option;
typedef struct fx_command_arg fx_command_arg;
typedef struct fx_command_usage fx_command_usage;
typedef struct fx_arglist fx_arglist;
typedef struct fx_arglist_option fx_arglist_option;
typedef int (*fx_command_callback)(
const fx_command *, const fx_arglist *, const fx_array *);
FX_API fx_command *fx_command_create(unsigned int id);
FX_API void fx_command_destroy(fx_command *cmd);
FX_API fx_status fx_command_register(fx_command *cmd);
FX_API int fx_command_dispatch(unsigned int cmd_id, int argc, const char **argv);
FX_API fx_status fx_command_set_name(fx_command *cmd, const char *name);
FX_API fx_status fx_command_set_long_name(fx_command *cmd, const char *name);
FX_API fx_status fx_command_set_short_name(fx_command *cmd, char name);
FX_API fx_status fx_command_set_flags(fx_command *cmd, fx_command_flags flags);
FX_API fx_status fx_command_set_description(fx_command *cmd, const char *description);
FX_API fx_status fx_command_set_callback(
fx_command *cmd, fx_command_callback callback);
FX_API fx_status fx_command_set_parent(fx_command *cmd, unsigned int parent_id);
FX_API fx_command_option *fx_command_add_option(fx_command *cmd, int id);
FX_API fx_command_arg *fx_command_add_arg(fx_command *cmd, int id);
FX_API fx_command_usage *fx_command_add_usage(fx_command *cmd);
FX_API const fx_command_option *fx_command_get_option(const fx_command *cmd, int id);
FX_API const char *fx_command_option_get_long_name(const fx_command_option *opt);
FX_API char fx_command_option_get_short_name(const fx_command_option *opt);
FX_API const char *fx_command_option_get_description(fx_command_option *opt);
FX_API fx_status fx_command_option_set_long_name(
fx_command_option *opt, const char *name);
FX_API fx_status fx_command_option_set_short_name(fx_command_option *opt, char name);
FX_API fx_status fx_command_option_set_description(
fx_command_option *opt, const char *description);
FX_API fx_command_arg *fx_command_option_add_arg(fx_command_option *opt, int id);
FX_API fx_status fx_command_arg_set_name(fx_command_arg *arg, const char *name);
FX_API fx_status fx_command_arg_set_description(
fx_command_arg *arg, const char *description);
FX_API fx_status fx_command_arg_set_nr_values(
fx_command_arg *arg, fx_command_arg_value_count nr_values);
FX_API fx_status fx_command_arg_set_allowed_values(
fx_command_arg *arg, const char **allowed_values);
FX_API fx_status fx_command_usage_add_option(
fx_command_usage *usage, fx_command_option *opt);
FX_API fx_status fx_command_usage_add_arg(
fx_command_usage *usage, fx_command_arg *opt);
FX_API fx_status fx_command_usage_add_command(
fx_command_usage *usage, unsigned int cmd_id);
FX_API fx_status fx_arglist_get_string(
const fx_arglist *args, unsigned int opt_id, unsigned int arg_id,
unsigned int index, const char **out);
FX_API fx_status fx_arglist_get_int(
const fx_arglist *args, unsigned int opt_id, unsigned int arg_id,
unsigned int index, long long *out);
FX_API fx_status fx_arglist_get_uint(
const fx_arglist *args, unsigned int opt_id, unsigned int arg_id,
unsigned int index, unsigned long long *out);
FX_API fx_status fx_arglist_get_option(
const fx_arglist *args, unsigned int opt_id, unsigned int index,
fx_arglist_option **out);
FX_API size_t fx_arglist_get_count(
const fx_arglist *args, unsigned int opt_id, unsigned int arg_id);
FX_API fx_status fx_arglist_report_missing_option(
const fx_arglist *args, unsigned int opt_id);
FX_API fx_status fx_arglist_report_unexpected_arg(
const fx_arglist *args, const char *value);
FX_API fx_status fx_arglist_report_invalid_arg_value(
const fx_arglist *args, unsigned int opt_id, unsigned int arg_id,
const char *value);
FX_API fx_status fx_arglist_report_missing_args(
const fx_arglist *args, unsigned int opt_id, unsigned int arg_id,
size_t nr_supplied);
FX_API fx_status fx_arglist_option_get_value(
const fx_arglist_option *opt, unsigned int arg_id, unsigned int index,
fx_arglist_value **out);
FX_API int fx_arglist_iterator_begin(
const fx_arglist *args, unsigned int opt_filter, unsigned int arg_filter,
fx_arglist_iterator *it);
FX_API bool fx_arglist_iterator_next(fx_arglist_iterator *it);
FX_API bool fx_arglist_iterator_is_valid(const fx_arglist_iterator *it);
FX_API int fx_arglist_option_iterator_begin(
const fx_arglist *args, unsigned int opt_filter,
fx_arglist_option_iterator *it);
FX_API bool fx_arglist_option_iterator_next(fx_arglist_option_iterator *it);
FX_API bool fx_arglist_option_iterator_is_valid(
const fx_arglist_option_iterator *it);
#endif

View File

@@ -1,13 +1,13 @@
#include "command.h"
#include <blue/cmd.h>
#include <blue/ds/string.h>
#include <fx/cmd.h>
#include <fx/ds/string.h>
#include <stdlib.h>
#include <string.h>
struct b_command_option *b_command_option_create(void)
struct fx_command_option *fx_command_option_create(void)
{
struct b_command_option *out = malloc(sizeof *out);
struct fx_command_option *out = malloc(sizeof *out);
if (!out) {
return out;
}
@@ -16,7 +16,7 @@ struct b_command_option *b_command_option_create(void)
return out;
}
void b_command_option_destroy(struct b_command_option *opt)
void fx_command_option_destroy(struct fx_command_option *opt)
{
if (opt->opt_long_name) {
free(opt->opt_long_name);
@@ -26,43 +26,43 @@ void b_command_option_destroy(struct b_command_option *opt)
free(opt->opt_description);
}
struct b_queue_entry *entry = b_queue_first(&opt->opt_args);
struct fx_queue_entry *entry = fx_queue_first(&opt->opt_args);
while (entry) {
struct b_command_arg *arg
= b_unbox(struct b_command_arg, entry, arg_entry);
struct fx_command_arg *arg
= fx_unbox(struct fx_command_arg, entry, arg_entry);
struct b_queue_entry *next = b_queue_next(entry);
b_queue_delete(&opt->opt_args, entry);
struct fx_queue_entry *next = fx_queue_next(entry);
fx_queue_delete(&opt->opt_args, entry);
b_command_arg_destroy(arg);
fx_command_arg_destroy(arg);
entry = next;
}
free(opt);
}
const char *b_command_option_get_long_name(const struct b_command_option *opt)
const char *fx_command_option_get_long_name(const struct fx_command_option *opt)
{
return opt->opt_long_name;
}
char b_command_option_get_short_name(const struct b_command_option *opt)
char fx_command_option_get_short_name(const struct fx_command_option *opt)
{
return opt->opt_short_name;
}
const char *b_command_option_get_description(struct b_command_option *opt)
const char *fx_command_option_get_description(struct fx_command_option *opt)
{
return opt->opt_description;
}
b_status b_command_option_set_long_name(
struct b_command_option *opt, const char *name)
fx_status fx_command_option_set_long_name(
struct fx_command_option *opt, const char *name)
{
char *n = b_strdup(name);
char *n = fx_strdup(name);
if (!n) {
return B_ERR_NO_MEMORY;
return FX_ERR_NO_MEMORY;
}
if (opt->opt_long_name) {
@@ -71,21 +71,21 @@ b_status b_command_option_set_long_name(
}
opt->opt_long_name = n;
return B_SUCCESS;
return FX_SUCCESS;
}
b_status b_command_option_set_short_name(struct b_command_option *opt, char name)
fx_status fx_command_option_set_short_name(struct fx_command_option *opt, char name)
{
opt->opt_short_name = name;
return B_SUCCESS;
return FX_SUCCESS;
}
b_status b_command_option_set_description(
struct b_command_option *opt, const char *description)
fx_status fx_command_option_set_description(
struct fx_command_option *opt, const char *description)
{
char *desc = b_strdup(description);
char *desc = fx_strdup(description);
if (!desc) {
return B_ERR_NO_MEMORY;
return FX_ERR_NO_MEMORY;
}
if (opt->opt_description) {
@@ -94,12 +94,12 @@ b_status b_command_option_set_description(
}
opt->opt_description = desc;
return B_SUCCESS;
return FX_SUCCESS;
}
struct b_command_arg *b_command_option_add_arg(struct b_command_option *opt, int id)
struct fx_command_arg *fx_command_option_add_arg(struct fx_command_option *opt, int id)
{
struct b_command_arg *arg = malloc(sizeof *arg);
struct fx_command_arg *arg = malloc(sizeof *arg);
if (!arg) {
return NULL;
}
@@ -108,111 +108,111 @@ struct b_command_arg *b_command_option_add_arg(struct b_command_option *opt, int
arg->arg_id = id;
b_queue_push_back(&opt->opt_args, &arg->arg_entry);
fx_queue_push_back(&opt->opt_args, &arg->arg_entry);
return arg;
}
void z__b_get_option_description(struct b_command_option *opt, b_string *out)
void z__fx_get_option_description(struct fx_command_option *opt, fx_string *out)
{
if (opt->opt_description) {
b_string_append_cstr(out, opt->opt_description);
fx_string_append_cstr(out, opt->opt_description);
}
size_t nr_args = b_queue_length(&opt->opt_args);
size_t nr_args = fx_queue_length(&opt->opt_args);
bool close_bracket = false;
size_t i = 0;
struct b_queue_entry *entry = b_queue_first(&opt->opt_args);
struct fx_queue_entry *entry = fx_queue_first(&opt->opt_args);
while (entry) {
struct b_command_arg *arg
= b_unbox(struct b_command_arg, entry, arg_entry);
struct fx_command_arg *arg
= fx_unbox(struct fx_command_arg, entry, arg_entry);
if (!arg || !arg->arg_allowed_values) {
goto skip;
}
if (i > 0) {
b_string_append_cstr(out, "; ");
fx_string_append_cstr(out, "; ");
} else {
b_string_append_cstr(out, " [[");
fx_string_append_cstr(out, " [[");
close_bracket = true;
}
if (nr_args > 1) {
b_string_append_cstrf(
fx_string_append_cstrf(
out, "values for `%s`:", arg->arg_name);
} else {
b_string_append_cstr(out, "values:");
fx_string_append_cstr(out, "values:");
}
for (size_t i = 0; arg->arg_allowed_values[i]; i++) {
if (i > 0) {
b_string_append_cstr(out, ",");
fx_string_append_cstr(out, ",");
}
b_string_append_cstrf(
fx_string_append_cstrf(
out, " " F_GREEN "%s" F_RESET,
arg->arg_allowed_values[i]);
}
skip:
i++;
entry = b_queue_next(entry);
entry = fx_queue_next(entry);
}
if (close_bracket) {
b_string_append_cstr(out, "]");
fx_string_append_cstr(out, "]");
}
}
void z__b_get_option_usage_string(
struct b_command_option *opt, enum cmd_string_flags flags, b_string *out)
void z__fx_get_option_usage_string(
struct fx_command_option *opt, enum cmd_string_flags flags, fx_string *out)
{
if (flags & CMD_STR_DIRECT_USAGE) {
b_string_append_cstr(out, "{");
fx_string_append_cstr(out, "{");
}
if (opt->opt_short_name) {
b_string_append_cstrf(
fx_string_append_cstrf(
out,
(flags & CMD_STR_COLOUR) ? F_GREEN "-%c" F_RESET : "-%c",
opt->opt_short_name);
}
if (opt->opt_short_name && opt->opt_long_name) {
b_string_append_cstr(
fx_string_append_cstr(
out, (flags & CMD_STR_DIRECT_USAGE) ? "|" : ", ");
}
if (opt->opt_long_name) {
b_string_append_cstrf(
fx_string_append_cstrf(
out,
(flags & CMD_STR_COLOUR) ? F_GREEN "--%s" F_RESET : "--%s",
opt->opt_long_name);
}
if (flags & CMD_STR_DIRECT_USAGE) {
b_string_append_cstr(out, "}");
fx_string_append_cstr(out, "}");
}
struct b_queue_entry *entry = b_queue_first(&opt->opt_args);
struct fx_queue_entry *entry = fx_queue_first(&opt->opt_args);
while (entry) {
struct b_command_arg *arg
= b_unbox(struct b_command_arg, entry, arg_entry);
struct fx_command_arg *arg
= fx_unbox(struct fx_command_arg, entry, arg_entry);
if (!arg) {
goto skip;
}
bool optional = false, multi = false;
switch (arg->arg_nr_values) {
case B_ARG_0_OR_1_VALUES:
case FX_ARG_0_OR_1_VALUES:
optional = true;
multi = false;
break;
case B_ARG_0_OR_MORE_VALUES:
case FX_ARG_0_OR_MORE_VALUES:
optional = true;
multi = true;
break;
case B_ARG_1_OR_MORE_VALUES:
case FX_ARG_1_OR_MORE_VALUES:
optional = false;
multi = true;
break;
@@ -223,13 +223,13 @@ void z__b_get_option_usage_string(
}
if (optional) {
b_string_append_cstrf(
fx_string_append_cstrf(
out,
(flags & CMD_STR_COLOUR) ? " " F_GREEN "[%s]"
: " [%s]",
arg->arg_name);
} else {
b_string_append_cstrf(
fx_string_append_cstrf(
out,
(flags & CMD_STR_COLOUR) ? " " F_GREEN "<%s>"
: " <%s>",
@@ -237,36 +237,36 @@ void z__b_get_option_usage_string(
}
for (int i = 1; i < arg->arg_nr_values; i++) {
b_string_append_cstrf(out, " <%s>", arg->arg_name);
fx_string_append_cstrf(out, " <%s>", arg->arg_name);
}
if (multi) {
b_string_append_cstr(out, "...");
fx_string_append_cstr(out, "...");
}
if (flags & CMD_STR_COLOUR) {
b_string_append_cstr(out, F_RESET);
fx_string_append_cstr(out, F_RESET);
}
skip:
entry = b_queue_next(entry);
entry = fx_queue_next(entry);
}
}
struct b_command_arg *b_command_option_get_arg_with_id(
struct b_command_option *opt, unsigned int id)
struct fx_command_arg *fx_command_option_get_arg_with_id(
struct fx_command_option *opt, unsigned int id)
{
struct b_queue_entry *entry = b_queue_first(&opt->opt_args);
struct fx_queue_entry *entry = fx_queue_first(&opt->opt_args);
while (entry) {
struct b_command_arg *arg
= b_unbox(struct b_command_arg, entry, arg_entry);
struct fx_command_arg *arg
= fx_unbox(struct fx_command_arg, entry, arg_entry);
if (arg->arg_id == id) {
return arg;
}
entry = b_queue_next(entry);
entry = fx_queue_next(entry);
}
return NULL;

View File

@@ -1,28 +1,28 @@
#include "command.h"
#include <assert.h>
#include <blue/core/stringstream.h>
#include <blue/ds/string.h>
#include <blue/term/print.h>
#include <fx/core/stringstream.h>
#include <fx/ds/string.h>
#include <fx/term/print.h>
#include <stdio.h>
enum b_status b_arglist_report_missing_option(
const b_arglist *args, unsigned int opt_id)
enum fx_status fx_arglist_report_missing_option(
const fx_arglist *args, unsigned int opt_id)
{
struct b_command_option *opt = NULL;
struct fx_command_option *opt = NULL;
if (opt_id != B_COMMAND_INVALID_ID) {
opt = b_command_get_option_with_id(args->list_command, opt_id);
if (opt_id != FX_COMMAND_INVALID_ID) {
opt = fx_command_get_option_with_id(args->list_command, opt_id);
}
if (!opt) {
return B_ERR_INVALID_ARGUMENT;
return FX_ERR_INVALID_ARGUMENT;
}
b_string *opt_string = b_string_create();
z__b_get_option_usage_string(opt, 0, opt_string);
fx_string *opt_string = fx_string_create();
z__fx_get_option_usage_string(opt, 0, opt_string);
b_stringstream *opt_name = b_stringstream_create();
fx_stringstream *opt_name = fx_stringstream_create();
int opt_names = 0;
if (opt->opt_short_name) {
@@ -34,121 +34,121 @@ enum b_status b_arglist_report_missing_option(
}
if (opt_names == 2) {
b_stream_write_fmt(
fx_stream_write_fmt(
opt_name, NULL, "-%c / --%s", opt->opt_short_name,
opt->opt_long_name);
} else if (opt->opt_short_name) {
b_stream_write_fmt(opt_name, NULL, "-%c", opt->opt_short_name);
fx_stream_write_fmt(opt_name, NULL, "-%c", opt->opt_short_name);
} else if (opt->opt_long_name) {
b_stream_write_fmt(opt_name, NULL, "--%s", opt->opt_long_name);
fx_stream_write_fmt(opt_name, NULL, "--%s", opt->opt_long_name);
}
b_err("required option `" F_YELLOW "%s" F_RESET "` was not specified.",
b_stringstream_ptr(opt_name));
b_i("usage: %s", b_string_ptr(opt_string));
b_i("for more information, use `" F_YELLOW "--help" F_RESET "`");
fx_err("required option `" F_YELLOW "%s" F_RESET "` was not specified.",
fx_stringstream_ptr(opt_name));
fx_i("usage: %s", fx_string_ptr(opt_string));
fx_i("for more information, use `" F_YELLOW "--help" F_RESET "`");
b_string_unref(opt_string);
b_stringstream_unref(opt_name);
fx_string_unref(opt_string);
fx_stringstream_unref(opt_name);
return B_SUCCESS;
return FX_SUCCESS;
}
enum b_status b_arglist_report_unexpected_arg(
const b_arglist *args, const char *value)
enum fx_status fx_arglist_report_unexpected_arg(
const fx_arglist *args, const char *value)
{
b_string *usage = z__b_command_default_usage_string(
fx_string *usage = z__fx_command_default_usage_string(
args->list_command, NULL, args);
b_err("unexpected argument '" F_YELLOW "%s" F_RESET "' found.", value);
b_i("usage: %s", b_string_ptr(usage));
b_i("for more information, use '" F_YELLOW "--help" F_RESET "'");
fx_err("unexpected argument '" F_YELLOW "%s" F_RESET "' found.", value);
fx_i("usage: %s", fx_string_ptr(usage));
fx_i("for more information, use '" F_YELLOW "--help" F_RESET "'");
return B_SUCCESS;
return FX_SUCCESS;
}
enum b_status b_arglist_report_invalid_arg_value(
const b_arglist *args, unsigned int opt_id, unsigned int arg_id,
enum fx_status fx_arglist_report_invalid_arg_value(
const fx_arglist *args, unsigned int opt_id, unsigned int arg_id,
const char *value)
{
struct b_command_option *opt = NULL;
struct b_command_arg *arg = NULL;
struct fx_command_option *opt = NULL;
struct fx_command_arg *arg = NULL;
if (opt_id != B_COMMAND_INVALID_ID) {
opt = b_command_get_option_with_id(args->list_command, opt_id);
if (opt_id != FX_COMMAND_INVALID_ID) {
opt = fx_command_get_option_with_id(args->list_command, opt_id);
}
if (arg_id != B_COMMAND_INVALID_ID) {
arg = opt ? b_command_option_get_arg_with_id(opt, arg_id)
: b_command_get_arg_with_id(args->list_command, arg_id);
if (arg_id != FX_COMMAND_INVALID_ID) {
arg = opt ? fx_command_option_get_arg_with_id(opt, arg_id)
: fx_command_get_arg_with_id(args->list_command, arg_id);
}
b_string *usage = z__b_command_default_usage_string(
fx_string *usage = z__fx_command_default_usage_string(
args->list_command, opt, args);
b_string *opt_string = b_string_create();
fx_string *opt_string = fx_string_create();
if (opt) {
z__b_get_option_usage_string(opt, 0, opt_string);
z__fx_get_option_usage_string(opt, 0, opt_string);
} else {
z__b_get_arg_usage_string(arg, 0, opt_string);
z__fx_get_arg_usage_string(arg, 0, opt_string);
}
b_err("invalid value '" F_YELLOW "%s" F_RESET "' for '" F_YELLOW
fx_err("invalid value '" F_YELLOW "%s" F_RESET "' for '" F_YELLOW
"%s" F_RESET "'.",
value, b_string_ptr(opt_string));
value, fx_string_ptr(opt_string));
if (opt) {
b_i("'" F_YELLOW "%s" F_RESET
fx_i("'" F_YELLOW "%s" F_RESET
"' accepts the following values for '" F_YELLOW "%s" F_RESET
"':",
b_string_ptr(opt_string), arg->arg_name);
fx_string_ptr(opt_string), arg->arg_name);
} else {
b_i("'" F_YELLOW "%s" F_RESET "' accepts the following values:",
b_string_ptr(opt_string));
fx_i("'" F_YELLOW "%s" F_RESET "' accepts the following values:",
fx_string_ptr(opt_string));
}
for (int i = 0; arg->arg_allowed_values[i]; i++) {
b_printf(
fx_printf(
" * " F_GREEN "%s" F_RESET "\n",
arg->arg_allowed_values[i]);
}
b_printf("\n");
b_i("usage: %s", b_string_ptr(usage));
b_i("for more information, use '" F_YELLOW "--help" F_RESET);
fx_printf("\n");
fx_i("usage: %s", fx_string_ptr(usage));
fx_i("for more information, use '" F_YELLOW "--help" F_RESET);
b_string_unref(usage);
b_string_unref(opt_string);
fx_string_unref(usage);
fx_string_unref(opt_string);
return B_SUCCESS;
return FX_SUCCESS;
}
enum b_status b_arglist_report_missing_args(
const b_arglist *args, unsigned int opt_id, unsigned int arg_id,
enum fx_status fx_arglist_report_missing_args(
const fx_arglist *args, unsigned int opt_id, unsigned int arg_id,
size_t args_supplied)
{
struct b_command_option *opt = NULL;
struct b_command_arg *arg = NULL;
struct fx_command_option *opt = NULL;
struct fx_command_arg *arg = NULL;
if (opt_id != B_COMMAND_INVALID_ID) {
opt = b_command_get_option_with_id(args->list_command, opt_id);
if (opt_id != FX_COMMAND_INVALID_ID) {
opt = fx_command_get_option_with_id(args->list_command, opt_id);
assert(opt);
}
if (arg_id != B_COMMAND_INVALID_ID) {
arg = opt ? b_command_option_get_arg_with_id(opt, arg_id)
: b_command_get_arg_with_id(args->list_command, arg_id);
if (arg_id != FX_COMMAND_INVALID_ID) {
arg = opt ? fx_command_option_get_arg_with_id(opt, arg_id)
: fx_command_get_arg_with_id(args->list_command, arg_id);
assert(arg);
}
b_string *usage = z__b_command_default_usage_string(
fx_string *usage = z__fx_command_default_usage_string(
args->list_command, opt, args);
b_string *opt_string = b_string_create();
fx_string *opt_string = fx_string_create();
if (opt) {
z__b_get_option_usage_string(opt, 0, opt_string);
z__fx_get_option_usage_string(opt, 0, opt_string);
} else {
z__b_get_arg_usage_string(arg, 0, opt_string);
z__fx_get_arg_usage_string(arg, 0, opt_string);
}
char supplied_arg_str[64];
@@ -170,7 +170,7 @@ enum b_status b_arglist_report_missing_args(
char required_arg_count[64];
switch (arg->arg_nr_values) {
case B_ARG_1_OR_MORE_VALUES:
case FX_ARG_1_OR_MORE_VALUES:
snprintf(
required_arg_count, sizeof required_arg_count,
"one or more");
@@ -181,15 +181,15 @@ enum b_status b_arglist_report_missing_args(
arg->arg_nr_values);
}
b_err("argument `" F_YELLOW "%s" F_RESET "` requires " F_GREEN_BOLD
fx_err("argument `" F_YELLOW "%s" F_RESET "` requires " F_GREEN_BOLD
"%s" F_RESET " `" F_YELLOW "%s" F_RESET "` value%s, but %s.",
b_string_ptr(opt_string), required_arg_count, arg->arg_name,
fx_string_ptr(opt_string), required_arg_count, arg->arg_name,
(arg->arg_nr_values == 1) ? "" : "s", supplied_arg_str);
b_i("usage: %s", b_string_ptr(usage));
b_i("for more information, use '" F_YELLOW "--help" F_RESET "'");
fx_i("usage: %s", fx_string_ptr(usage));
fx_i("for more information, use '" F_YELLOW "--help" F_RESET "'");
b_string_unref(usage);
b_string_unref(opt_string);
fx_string_unref(usage);
fx_string_unref(opt_string);
return B_SUCCESS;
return FX_SUCCESS;
}