Files
bluelib/cmd/command.h
2024-10-24 21:32:28 +01:00

132 lines
3.2 KiB
C

#ifndef _B_COMMAND_H_
#define _B_COMMAND_H_
#include <blue/cmd.h>
#include <blue/core/btree.h>
#include <blue/core/queue.h>
#define F_RED "\033[91m"
#define F_GREEN "\033[92m"
#define F_YELLOW "\033[93m"
#define F_RED_BOLD "\033[1;91m"
#define F_GREEN_BOLD "\033[1;92m"
#define F_YELLOW_BOLD "\033[1;93m"
#define F_RESET "\033[0m"
enum cmd_string_flags {
CMD_STR_COLOUR = 0x01u,
CMD_STR_DIRECT_USAGE = 0x02u,
};
struct b_string;
struct b_command {
unsigned int b_id;
unsigned int b_parent_id;
struct b_command *b_parent;
enum b_command_flags b_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;
b_command_callback b_callback;
struct b_queue_entry b_entry;
struct b_btree_node b_node;
};
struct b_command_usage_opt {
struct b_queue_entry opt_entry;
struct b_command_option *opt;
};
struct b_command_usage_arg {
struct b_queue_entry arg_entry;
struct b_command_arg *arg;
};
struct b_command_usage {
b_command_usage_flags u_flags;
struct b_queue_entry u_entry;
struct b_queue u_opt;
struct b_queue u_arg;
};
struct b_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;
// char **arg_allowed_values;
struct b_queue opt_args;
struct b_queue_entry opt_entry;
};
struct b_arglist_option {
unsigned int opt_id;
struct b_btree opt_values;
struct b_btree_node opt_node;
};
struct b_arglist {
struct b_btree list_options;
};
struct b_command_arg {
unsigned int arg_id;
char *arg_name;
char *arg_description;
b_command_arg_value_count arg_nr_values;
char **arg_allowed_values;
struct b_queue_entry arg_entry;
};
extern struct b_command *b_command_get_subcommand_with_name(
struct b_command *cmd, const char *name);
extern struct b_command *b_command_get_subcommand_with_long_name(
struct b_command *cmd, const char *long_name);
extern struct b_command *b_command_get_subcommand_with_short_name(
struct b_command *cmd, char short_name);
extern struct b_command_option *b_command_get_option_with_long_name(
struct b_command *cmd, const char *long_name);
extern struct b_command_option *b_command_get_option_with_short_name(
struct b_command *cmd, char short_name);
extern struct b_command_option *b_command_option_create(void);
extern struct b_command_arg *b_command_arg_create(void);
extern struct b_arglist *b_arglist_create(void);
extern b_status b_arglist_parse(
struct b_arglist *args, struct b_command **cmd, int argc,
const char **argv);
extern void b_arglist_destroy(struct b_arglist *args);
extern struct b_string *z__b_command_default_usage_string(
struct b_command *cmd, struct b_command_option *with_opt);
extern void z__b_get_arg_usage_string(
struct b_command_arg *arg, bool colour, struct b_string *out);
extern void z__b_get_arg_description(
struct b_command_arg *arg, struct b_string *out);
extern void z__b_get_option_usage_string(
struct b_command_option *opt, enum cmd_string_flags flags,
struct b_string *out);
extern void z__b_get_option_description(
struct b_command_option *opt, struct b_string *out);
#endif