main.c (1786B)
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 #include "cofyc/argparse.h" 6 #include "command/command.h" 7 #include "erkkah/naett.h" 8 #include "rxi/microtar.h" 9 10 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) 11 12 struct cmd_struct *commands = NULL; 13 14 static void print_global_usage(void) { 15 printf("Usage: dep [global options] <command> [command options]\n"); 16 printf("\n"); 17 printf("Global options:\n"); 18 printf(" n/a\n"); 19 printf("\n"); 20 printf("Commands:\n"); 21 22 struct cmd_struct *cmd = commands; 23 while (cmd) { 24 printf(" %-16s %s\n", cmd->display ? cmd->display : cmd->name[0], cmd->description ? cmd->description : ""); 25 cmd = cmd->next; 26 } 27 28 printf("\n"); 29 printf("Help topics:\n"); 30 printf(" global This help text\n"); 31 32 cmd = commands; 33 while (cmd) { 34 printf(" %-16s More detailed explanation on the %s command\n", cmd->name[0], cmd->name[0]); 35 cmd = cmd->next; 36 } 37 } 38 39 static const char *const usages[] = { 40 "dep [global options] <command> [command options]", 41 NULL, 42 }; 43 44 int main(int argc, const char **argv) { 45 struct argparse argparse; 46 struct argparse_option options[] = { 47 OPT_HELP(), 48 OPT_END(), 49 }; 50 argparse_init(&argparse, options, usages, ARGPARSE_STOP_AT_NON_OPTION); 51 argc = argparse_parse(&argparse, argc, argv); 52 if (argc < 1) { 53 print_global_usage(); 54 return 0; 55 } 56 57 /* Try to run command with args provided. */ 58 struct cmd_struct *cmd = commands; 59 while (cmd) { 60 const char **name = cmd->name; 61 while (*name) { 62 if (!strcmp(*name, argv[0])) { 63 goto found; 64 } 65 name++; 66 } 67 cmd = cmd->next; 68 } 69 found: 70 71 if (cmd) { 72 return cmd->fn(argc, argv); 73 } else { 74 fprintf(stderr, "Unknown command: %s\n", argv[0]); 75 return 1; 76 } 77 78 return 0; 79 }