diff --git a/groundStation/src/cli/cli.c b/groundStation/src/cli/cli.c index de456c18e271fe6ddc8a30346b34a0e140acc2e9..958b33ee649700a3b1b9a0b9d119db0d55e67de0 100644 --- a/groundStation/src/cli/cli.c +++ b/groundStation/src/cli/cli.c @@ -13,15 +13,7 @@ int main(int argc, char **argv) int c; int i , useSymlink = 0; struct backend_conn *conn; - static int needHelp = 0; - static struct option long_options[] = { - /* These options don’t set a flag. We distinguish them by their indices. */ - {"help", no_argument, &needHelp, 1}, - {0, 0, 0, 0} - }; - - int option_index = 0; - c = getopt_long(argc, argv, "", long_options, &option_index); + int needCliHelp = 0, needCommandHelp = 0; // Determine if the cli was called using a symlink command = basename(argv[0]); @@ -34,7 +26,7 @@ int main(int argc, char **argv) } // Verify the user has entered enough information to continue - if(argc < 2 && !useSymlink) { + if (argc < 2 && !useSymlink) { printf("Incorrect usage :\n"); printf("\n\tUsage : ./Cli command [options] \n"); printf("\tFor a list of available commands run ./Cli --help\n\n"); @@ -42,8 +34,12 @@ int main(int argc, char **argv) return -1; } + // Determine if the user called for help on the cli + needCliHelp = (strncmp("--help", argv[1], strlen(argv[1])) == 0); + + // If the user runs './Cli help' , provide the user with a list of commands available. - if(needHelp) { + if (needCliHelp) { printf("Usage : ./Cli command [options]\n"); printf("For a list of available options for a command run ./Cli command --help\n\n"); printf("Available commands include the following\n"); @@ -53,9 +49,8 @@ int main(int argc, char **argv) return 0; } - // recognize which cli command the user has entered - if(cmdID == -1) { + if (cmdID == -1) { command = argv[1]; for(i = 0; i < MAX_COMMANDS; ++i) { if (strncmp(command, commandNames[i], strlen(commandNames[i])) == 0) @@ -65,7 +60,7 @@ int main(int argc, char **argv) } } - if(cmdID == -1){ + if (cmdID == -1){ printf("Could not match '%s' with a command. Please try again...\n", command); printf("For help running the program, run ./Cli --help\n"); return -1; @@ -73,6 +68,17 @@ int main(int argc, char **argv) printf("Parsed Command : %s\n", commandNames[cmdID]); + // Determine if the user called for help on the command + if (!useSymlink && argc > 2) { + if (strncmp("--help", argv[2], strlen(argv[2])) == 0) { + needCommandHelp = 1; + } + } else if (useSymlink && argc > 1) { + if (strncmp("--help", argv[1], strlen(argv[1])) == 0) { + needCommandHelp = 1; + } + } + /** * I the user has asked for help, and we have already found * the command that they are trying to use. Then we need @@ -85,22 +91,24 @@ int main(int argc, char **argv) */ // Only require the backend if we will need to use it. - if(!needHelp) { + if (!needCommandHelp) { // Create the connection to the backend conn = ucart_backendConnect(); - if(conn == NULL) { + if (conn == NULL) { return -1; } } // Call the appropriate function - if(useSymlink) { + if (useSymlink) { (*cli_functions[cmdID]) (conn, argc, &argv[0]); }else { (*cli_functions[cmdID]) (conn, argc-1, &argv[1]); } // Disconnect from the backend - ucart_backendDisconnect(conn); + if (!needCommandHelp) { + ucart_backendDisconnect(conn); + } return 0; } \ No newline at end of file diff --git a/groundStation/src/cli/cli_monitor.c b/groundStation/src/cli/cli_monitor.c index b9917d1df58c5bde4513fccd9ccccbc6ebd8fe72..5964cb78ace27953b0b9d2a80de6038f210c5718 100644 --- a/groundStation/src/cli/cli_monitor.c +++ b/groundStation/src/cli/cli_monitor.c @@ -13,12 +13,27 @@ int cli_monitor(struct backend_conn * conn, int argc, char **argv) { int c, result; int countFlag = 0; - + /* getopt_long stores the option index here. */ + int option_index = 0; int count, rate = 10; int forever = 0; + static int needHelp = 0; + + static struct option long_options[] = { + /* These options don’t set a flag. We distinguish them by their indices. */ + {"help", no_argument, &needHelp, 1}, + {0, 0, 0, 0} + }; + + while (1) + { + // If you change this VVV please also update the help message + c = getopt_long(argc, argv, "fc:r:", long_options, &option_index); - while ((c = getopt(argc, argv, "fc:r:")) != -1) { - switch(c) { + if (c == -1) + break; + + switch(c) { case 'c' : count = atoi(optarg); countFlag = 1; @@ -34,6 +49,11 @@ int cli_monitor(struct backend_conn * conn, int argc, char **argv) { } } + if (needHelp) { + printf("helping you\n"); + return 0; + } + if (forever) { for (;;) { struct timespec req;