Files
fx/cmd/command.h

152 lines
4.0 KiB
C

#ifndef _FX_COMMAND_H_
#define _FX_COMMAND_H_
#include <fx/cmd.h>
#include <fx/core/bst.h>
#include <fx/core/queue.h>
#include <fx/ds/string.h>
#define F_RED "[bright_red]"
#define F_GREEN "[bright_green]"
#define F_YELLOW "[bright_yellow]"
#define F_RED_BOLD "[bright_red,bold]"
#define F_GREEN_BOLD "[bright_green,bold]"
#define F_YELLOW_BOLD "[bright_yellow,bold]"
#define F_RESET "[reset]"
enum cmd_string_flags {
CMD_STR_COLOUR = 0x01u,
CMD_STR_DIRECT_USAGE = 0x02u,
};
enum fx_command_usage_entry_type {
CMD_USAGE_NONE = 0,
CMD_USAGE_ARG,
CMD_USAGE_OPT,
CMD_USAGE_COMMAND,
};
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;
};
struct fx_command_usage_entry {
struct fx_queue_entry e_entry;
enum fx_command_usage_entry_type e_type;
union {
struct fx_command_option *e_opt;
struct fx_command_arg *e_arg;
unsigned int e_cmd_id;
};
};
struct fx_command_usage {
fx_command_usage_flags u_flags;
struct fx_queue_entry u_entry;
struct fx_queue u_parts;
};
struct fx_command_option {
unsigned int opt_id;
char *opt_long_name;
char opt_short_name;
char *opt_description;
// fx_command_arg_value_count arg_nr_values;
// char **arg_allowed_values;
struct fx_queue opt_args;
struct fx_queue_entry opt_entry;
};
struct fx_command_arg {
unsigned int arg_id;
char *arg_name;
char *arg_description;
fx_command_arg_value_count arg_nr_values;
char **arg_allowed_values;
struct fx_queue_entry arg_entry;
};
struct fx_arglist_option {
unsigned int opt_id;
struct fx_bst opt_values;
struct fx_bst_node opt_node;
};
struct fx_arglist {
size_t list_argc;
const char **list_argv;
unsigned int list_argv_last_command;
struct fx_command *list_command;
struct fx_bst list_options;
};
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,
const char **argv);
FX_API void fx_arglist_destroy(struct fx_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);
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);
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