c-template
cli.c
Go to the documentation of this file.
1 #include <stdbool.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <argtable3.h>
5 #include "../include/utils/colors.h"
6 #include "../include/utils/command_line.h"
7 #include "../include/utils/safe_mem.h"
8 #include "../include/utils/logger.h"
9 
10 
11 #ifndef COMMAND_VERSION_STRING
12 #define COMMAND_VERSION_STRING "0.0.1"
13 #endif
14 
15 // displays the help command
17 
18 int main(int argc, char *argv[]) {
20  void *argtable[] = {help, version, file, output, command_to_run, end};
21  int response = parse_args(argc, argv, argtable);
22  switch (response) {
23  case 0:
24  break;
25  case -1:
26  printf("parse_args failed\n");
27  return response;
28  case -2: // this means --help was invoked
29  return 0;
30  }
31  // handle help if no other cli arguments were given (aka binary invoked with ./some-binary)
32  if (argc == 1) {
33  print_help(argv[0], argtable);
34  return 0;
35  }
36  // construct the command object
37  command_object *pcmd = new_command_object(argc, argv);
38  if (pcmd == NULL) {
39  printf("failed to get command_object");
40  return -1;
41  }
42  // END COMMAND INPUT PREPARATION
43  int resp = execute(pcmd, "helo");
44  if (resp != 0) {
45  // TODO(bonedaddy): figure out if we should log this
46  // printf("command run failed\n");
47  }
48  free_command_object(pcmd);
49  arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
50  return resp;
51 }
command_to_run
struct arg_str * command_to_run
Definition: command_line.h:27
print_help
void print_help(char *program_name, void *argtable[])
formats output
Definition: command_line.c:99
parse_args
int parse_args(int argc, char *argv[], void *argtable[])
parses arguments, and checks for any errors
Definition: command_line.c:82
free_command_object
void free_command_object(command_object *self)
frees memory allocated for the command_object and sets pointer to null for some reason this is causin...
Definition: command_line.c:74
help
struct arg_lit * help
Definition: command_line.h:26
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
execute
int execute(command_object *self, char *run)
checks to see if we have a command named according to run and executes it
Definition: command_line.c:59
command
Definition: command_line.h:48
new_help_command
command_handler * new_help_command(command_object *self)
file
struct arg_file * file
Definition: command_line.h:28
end
struct arg_end * end
Definition: command_line.h:29
COMMAND_VERSION_STRING
#define COMMAND_VERSION_STRING
Definition: cli.c:12
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
output
struct arg_file * output
Definition: command_line.h:28
main
int main(int argc, char *argv[])
Definition: cli.c:18
version
struct arg_lit * version
Definition: command_line.h:26