2026-03-16 10:35:43 +00:00
|
|
|
#ifndef _FX_COMMAND_H_
|
|
|
|
|
#define _FX_COMMAND_H_
|
2024-10-24 21:32:28 +01:00
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
#include <fx/cmd.h>
|
2026-03-16 15:11:45 +00:00
|
|
|
#include <fx/core/bst.h>
|
2026-03-16 10:35:43 +00:00
|
|
|
#include <fx/core/queue.h>
|
|
|
|
|
#include <fx/ds/string.h>
|
2024-10-24 21:32:28 +01:00
|
|
|
|
2024-11-20 22:12:50 +00:00
|
|
|
#define F_RED "[bright_red]"
|
|
|
|
|
#define F_GREEN "[bright_green]"
|
|
|
|
|
#define F_YELLOW "[bright_yellow]"
|
2024-10-24 21:32:28 +01:00
|
|
|
|
2024-11-20 22:12:50 +00:00
|
|
|
#define F_RED_BOLD "[bright_red,bold]"
|
|
|
|
|
#define F_GREEN_BOLD "[bright_green,bold]"
|
|
|
|
|
#define F_YELLOW_BOLD "[bright_yellow,bold]"
|
2024-11-14 22:03:39 +00:00
|
|
|
#define F_RESET "[reset]"
|
2024-10-24 21:32:28 +01:00
|
|
|
|
|
|
|
|
enum cmd_string_flags {
|
|
|
|
|
CMD_STR_COLOUR = 0x01u,
|
|
|
|
|
CMD_STR_DIRECT_USAGE = 0x02u,
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
enum fx_command_usage_entry_type {
|
2025-07-17 17:56:00 +01:00
|
|
|
CMD_USAGE_NONE = 0,
|
|
|
|
|
CMD_USAGE_ARG,
|
|
|
|
|
CMD_USAGE_OPT,
|
|
|
|
|
CMD_USAGE_COMMAND,
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
struct fx_command {
|
|
|
|
|
unsigned int c_id;
|
|
|
|
|
unsigned int c_parent_id;
|
|
|
|
|
struct fx_command *c_parent;
|
|
|
|
|
enum fx_command_flags c_flags;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
fx_command_callback c_callback;
|
|
|
|
|
struct fx_queue_entry c_entry;
|
|
|
|
|
struct fx_bst_node c_node;
|
2024-10-24 21:32:28 +01:00
|
|
|
};
|
|
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
struct fx_command_usage_entry {
|
|
|
|
|
struct fx_queue_entry e_entry;
|
|
|
|
|
enum fx_command_usage_entry_type e_type;
|
2024-10-24 21:32:28 +01:00
|
|
|
|
2025-07-17 17:56:00 +01:00
|
|
|
union {
|
2026-03-16 10:35:43 +00:00
|
|
|
struct fx_command_option *e_opt;
|
|
|
|
|
struct fx_command_arg *e_arg;
|
2025-07-17 17:56:00 +01:00
|
|
|
unsigned int e_cmd_id;
|
|
|
|
|
};
|
2024-10-31 19:33:48 +00:00
|
|
|
};
|
|
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
struct fx_command_usage {
|
|
|
|
|
fx_command_usage_flags u_flags;
|
|
|
|
|
struct fx_queue_entry u_entry;
|
2024-10-24 21:32:28 +01:00
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
struct fx_queue u_parts;
|
2024-10-24 21:32:28 +01:00
|
|
|
};
|
|
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
struct fx_command_option {
|
2024-10-24 21:32:28 +01:00
|
|
|
unsigned int opt_id;
|
|
|
|
|
|
|
|
|
|
char *opt_long_name;
|
|
|
|
|
char opt_short_name;
|
|
|
|
|
char *opt_description;
|
|
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
// fx_command_arg_value_count arg_nr_values;
|
2024-10-24 21:32:28 +01:00
|
|
|
// char **arg_allowed_values;
|
|
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
struct fx_queue opt_args;
|
|
|
|
|
struct fx_queue_entry opt_entry;
|
2024-10-24 21:32:28 +01:00
|
|
|
};
|
|
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
struct fx_command_arg {
|
2024-10-24 21:32:28 +01:00
|
|
|
unsigned int arg_id;
|
|
|
|
|
char *arg_name;
|
|
|
|
|
char *arg_description;
|
|
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
fx_command_arg_value_count arg_nr_values;
|
2024-10-24 21:32:28 +01:00
|
|
|
char **arg_allowed_values;
|
|
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
struct fx_queue_entry arg_entry;
|
2024-10-24 21:32:28 +01:00
|
|
|
};
|
|
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
struct fx_arglist_option {
|
2024-10-27 19:43:05 +00:00
|
|
|
unsigned int opt_id;
|
2026-03-16 10:35:43 +00:00
|
|
|
struct fx_bst opt_values;
|
|
|
|
|
struct fx_bst_node opt_node;
|
2024-10-27 19:43:05 +00:00
|
|
|
};
|
|
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
struct fx_arglist {
|
2025-08-09 19:36:46 +01:00
|
|
|
size_t list_argc;
|
|
|
|
|
const char **list_argv;
|
|
|
|
|
unsigned int list_argv_last_command;
|
2026-03-16 10:35:43 +00:00
|
|
|
struct fx_command *list_command;
|
|
|
|
|
struct fx_bst list_options;
|
2024-10-27 19:43:05 +00:00
|
|
|
};
|
|
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
FX_API struct fx_command_arg *fx_command_option_get_arg_with_id(
|
|
|
|
|
struct fx_command_option *opt, unsigned int id);
|
|
|
|
|
|
|
|
|
|
FX_API struct fx_command_option *fx_command_option_create(void);
|
|
|
|
|
FX_API void fx_command_option_destroy(struct fx_command_option *opt);
|
|
|
|
|
|
|
|
|
|
FX_API struct fx_command_arg *fx_command_arg_create(void);
|
|
|
|
|
FX_API void fx_command_arg_destroy(struct fx_command_arg *arg);
|
|
|
|
|
|
|
|
|
|
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,
|
2024-10-24 21:32:28 +01:00
|
|
|
const char **argv);
|
2026-03-16 10:35:43 +00:00
|
|
|
FX_API void fx_arglist_destroy(struct fx_arglist *args);
|
2024-10-24 21:32:28 +01:00
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
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);
|
2024-10-24 21:32:28 +01:00
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
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);
|
2024-10-24 21:32:28 +01:00
|
|
|
|
2026-03-16 10:35:43 +00:00
|
|
|
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);
|
2024-10-24 21:32:28 +01:00
|
|
|
|
|
|
|
|
#endif
|