Skip to content
Snippets Groups Projects
Commit dec6a9be authored by burneykb's avatar burneykb
Browse files

added help flag to monitor command

parent c4d5cbdf
No related branches found
No related tags found
No related merge requests found
...@@ -13,15 +13,7 @@ int main(int argc, char **argv) ...@@ -13,15 +13,7 @@ int main(int argc, char **argv)
int c; int c;
int i , useSymlink = 0; int i , useSymlink = 0;
struct backend_conn *conn; struct backend_conn *conn;
static int needHelp = 0; int needCliHelp = 0, needCommandHelp = 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);
// Determine if the cli was called using a symlink // Determine if the cli was called using a symlink
command = basename(argv[0]); command = basename(argv[0]);
...@@ -34,7 +26,7 @@ int main(int argc, char **argv) ...@@ -34,7 +26,7 @@ int main(int argc, char **argv)
} }
// Verify the user has entered enough information to continue // Verify the user has entered enough information to continue
if(argc < 2 && !useSymlink) { if (argc < 2 && !useSymlink) {
printf("Incorrect usage :\n"); printf("Incorrect usage :\n");
printf("\n\tUsage : ./Cli command [options] \n"); printf("\n\tUsage : ./Cli command [options] \n");
printf("\tFor a list of available commands run ./Cli --help\n\n"); printf("\tFor a list of available commands run ./Cli --help\n\n");
...@@ -42,8 +34,12 @@ int main(int argc, char **argv) ...@@ -42,8 +34,12 @@ int main(int argc, char **argv)
return -1; 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 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("Usage : ./Cli command [options]\n");
printf("For a list of available options for a command run ./Cli command --help\n\n"); printf("For a list of available options for a command run ./Cli command --help\n\n");
printf("Available commands include the following\n"); printf("Available commands include the following\n");
...@@ -53,9 +49,8 @@ int main(int argc, char **argv) ...@@ -53,9 +49,8 @@ int main(int argc, char **argv)
return 0; return 0;
} }
// recognize which cli command the user has entered // recognize which cli command the user has entered
if(cmdID == -1) { if (cmdID == -1) {
command = argv[1]; command = argv[1];
for(i = 0; i < MAX_COMMANDS; ++i) { for(i = 0; i < MAX_COMMANDS; ++i) {
if (strncmp(command, commandNames[i], strlen(commandNames[i])) == 0) if (strncmp(command, commandNames[i], strlen(commandNames[i])) == 0)
...@@ -65,7 +60,7 @@ int main(int argc, char **argv) ...@@ -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("Could not match '%s' with a command. Please try again...\n", command);
printf("For help running the program, run ./Cli --help\n"); printf("For help running the program, run ./Cli --help\n");
return -1; return -1;
...@@ -73,6 +68,17 @@ int main(int argc, char **argv) ...@@ -73,6 +68,17 @@ int main(int argc, char **argv)
printf("Parsed Command : %s\n", commandNames[cmdID]); 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 * I the user has asked for help, and we have already found
* the command that they are trying to use. Then we need * the command that they are trying to use. Then we need
...@@ -85,22 +91,24 @@ int main(int argc, char **argv) ...@@ -85,22 +91,24 @@ int main(int argc, char **argv)
*/ */
// Only require the backend if we will need to use it. // Only require the backend if we will need to use it.
if(!needHelp) { if (!needCommandHelp) {
// Create the connection to the backend // Create the connection to the backend
conn = ucart_backendConnect(); conn = ucart_backendConnect();
if(conn == NULL) { if (conn == NULL) {
return -1; return -1;
} }
} }
// Call the appropriate function // Call the appropriate function
if(useSymlink) { if (useSymlink) {
(*cli_functions[cmdID]) (conn, argc, &argv[0]); (*cli_functions[cmdID]) (conn, argc, &argv[0]);
}else { }else {
(*cli_functions[cmdID]) (conn, argc-1, &argv[1]); (*cli_functions[cmdID]) (conn, argc-1, &argv[1]);
} }
// Disconnect from the backend // Disconnect from the backend
ucart_backendDisconnect(conn); if (!needCommandHelp) {
ucart_backendDisconnect(conn);
}
return 0; return 0;
} }
\ No newline at end of file
...@@ -13,12 +13,27 @@ ...@@ -13,12 +13,27 @@
int cli_monitor(struct backend_conn * conn, int argc, char **argv) { int cli_monitor(struct backend_conn * conn, int argc, char **argv) {
int c, result; int c, result;
int countFlag = 0; int countFlag = 0;
/* getopt_long stores the option index here. */
int option_index = 0;
int count, rate = 10; int count, rate = 10;
int forever = 0; 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) { if (c == -1)
switch(c) { break;
switch(c) {
case 'c' : case 'c' :
count = atoi(optarg); count = atoi(optarg);
countFlag = 1; countFlag = 1;
...@@ -34,6 +49,11 @@ int cli_monitor(struct backend_conn * conn, int argc, char **argv) { ...@@ -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) { if (forever) {
for (;;) { for (;;) {
struct timespec req; struct timespec req;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment