c-template
command_line.h
Go to the documentation of this file.
1 
2 /*! @file command_line.h
3  * @brief provides a basic CLI building tool
4  * attribution note: modified version of commander see `deps/commander` for the license for that package
5  * uses argtable3 for command line configuration and allows loading a list of commands to execute
6  * using the `--conmmand` or `-c` CLI flag you can specify the command out of the list of commands that have been loaded
7  * before calling execute you'll want to load the appropriate `argc` and `argv` values for the callback of the command you want to make
8  *
9 */
10 
11 // pragma once is an alternative to header guards which can be very verbose
12 #pragma once
13 
14 #include <argtable3.h>
15 #include <stdbool.h>
16 
17 #ifndef MAX_COMMANDS
18 #define MAX_COMMANDS 32
19 #endif
20 
21 #ifndef MAX_COMMAND_ARGS
22 #define MAX_COMMAND_ARGS 32
23 #endif
24 
25 // default command line arguments
26 struct arg_lit *help, *version;
27 struct arg_str *command_to_run;
28 struct arg_file *file, *output;
29 struct arg_end *end;
30 
31 // command is the main object type
32 struct command;
33 
34 // declares the command handler callback which takes in an instance of command
35 typedef void (*command_handler_callback)(int argc, char *argv[]);
36 
37 /*! @struct an individual command to run
38  * @brief callback is a function to be executed
39 */
40 typedef struct {
41  char *name;
44 
45 /*! @struct the root command object
46  * @brief contains the list of possible commands to execute
47 */
48 typedef struct command {
50  int argc;
54 
55 /*! @brief returns the value of command_to_run
56 */
57 char *get_run_command();
58 /*! @brief setups the default argtable arguments
59 */
60 void setup_args(const char *version_string);
61 /*! @brief formats output
62 */
63 void print_help(char *program_name, void *argtable[]);
64 /*! @brief parses arguments, and checks for any errors
65 */
66 int parse_args(int argc, char *argv[], void *argtable[]);
67 
68 /*! @brief checks to see if we have a command named according to run and executes it
69 */
70 int execute(command_object *self, char *command_to_run);
71 
72 /*! @brief loads command handler and makes it executable
73 */
75 
76 /*! @brief intializes a new command_object to have commands loaded into
77 */
78 command_object *new_command_object(int argc, char *argv[]);
79 
80 /*! @brief frees memory allocated for the command_object and sets pointer to null
81 */
command_to_run
struct arg_str * command_to_run
Definition: command_line.h:27
free_command_object
void free_command_object(command_object *self)
frees memory allocated for the command_object and sets pointer to null
Definition: command_line.c:74
MAX_COMMANDS
#define MAX_COMMANDS
Definition: command_line.h:18
help
struct arg_lit * help
Definition: command_line.h:26
command_handler::name
char * name
Definition: command_line.h:41
command::commands
command_handler * commands[32]
Definition: command_line.h:52
setup_args
void setup_args(const char *version_string)
setups the default argtable arguments
Definition: command_line.c:106
command_handler
Definition: command_line.h:40
MAX_COMMAND_ARGS
#define MAX_COMMAND_ARGS
Definition: command_line.h:22
command_object
struct command command_object
command::argc
int argc
Definition: command_line.h:50
command
Definition: command_line.h:48
file
struct arg_file * file
Definition: command_line.h:28
execute
int execute(command_object *self, char *command_to_run)
checks to see if we have a command named according to run and executes it
Definition: command_line.c:59
command::argv
char * argv[32]
Definition: command_line.h:51
end
struct arg_end * end
Definition: command_line.h:29
command_handler::callback
command_handler_callback callback
Definition: command_line.h:42
new_command_object
command_object * new_command_object(int argc, char *argv[])
intializes a new command_object to have commands loaded into
Definition: command_line.c:14
command::command_count
int command_count
Definition: command_line.h:49
output
struct arg_file * output
Definition: command_line.h:28
command_handler_callback
void(* command_handler_callback)(int argc, char *argv[])
Definition: command_line.h:35
get_run_command
char * get_run_command()
returns the value of command_to_run
Definition: command_line.c:115
parse_args
int parse_args(int argc, char *argv[], void *argtable[])
parses arguments, and checks for any errors
Definition: command_line.c:82
load_command
int load_command(command_object *self, command_handler *command)
loads command handler and makes it executable
Definition: command_line.c:47
version
struct arg_lit * version
Definition: command_line.h:26
print_help
void print_help(char *program_name, void *argtable[])
formats output
Definition: command_line.c:99