From 49aa0b4322c1bbc1524d2b29ee1f47c2cbb5ab2b Mon Sep 17 00:00:00 2001 From: Alexey Kopytov Date: Sat, 28 Jan 2017 16:20:29 +0300 Subject: [PATCH] Fixes #78: better error hints for missing help command Now if the help command is specified for a script, sysbench first check if the script impelements a custom help command, and calls it. If no custom command is defined, it checks whether the script declares any command line options, in which case it prints their description. If neither the help() function nor an option declaration is available, sysbench prints an error message. --- src/lua/internal/sysbench.cmdline.lua | 11 ++++++++ src/sysbench.c | 38 ++++++++++++++++++--------- src/sysbench.h | 3 +++ tests/t/cmdline.t | 16 +++++++++-- 4 files changed, 53 insertions(+), 15 deletions(-) diff --git a/src/lua/internal/sysbench.cmdline.lua b/src/lua/internal/sysbench.cmdline.lua index c9babe8..6889b64 100644 --- a/src/lua/internal/sysbench.cmdline.lua +++ b/src/lua/internal/sysbench.cmdline.lua @@ -167,3 +167,14 @@ function sysbench.cmdline.call_command(name) return rc and true or false end end + +ffi.cdef[[ +void sb_print_test_options(void); +]] + +-- ---------------------------------------------------------------------- +-- Print descriptions of command line options, if defined by option_defs +-- ---------------------------------------------------------------------- +function sysbench.cmdline.print_test_options() + ffi.C.sb_print_test_options() +end diff --git a/src/sysbench.c b/src/sysbench.c index 0e33f19..a4a49a4 100644 --- a/src/sysbench.c +++ b/src/sysbench.c @@ -1224,6 +1224,8 @@ int main(int argc, char *argv[]) return test != NULL ? EXIT_SUCCESS : EXIT_FAILURE; } + current_test = test; + /* Load and parse test-specific options */ if (parse_test_arguments(test, argc, argv)) return EXIT_FAILURE; @@ -1234,19 +1236,22 @@ int main(int argc, char *argv[]) } else if (!strcmp(sb_globals.cmdname, "help")) { - if (test == NULL) - print_help(); - else + if (test->builtin_cmds.help != NULL) { - if (test->args != NULL) - { - printf("%s options:\n", test->sname); - sb_print_options(test->args); - } - if (test->builtin_cmds.help != NULL) - test->builtin_cmds.help(); + test->builtin_cmds.help(); + return EXIT_SUCCESS; } - return EXIT_SUCCESS; + else if (test->args != NULL) + { + printf("%s options:\n", test->sname); + sb_print_test_options(); + return EXIT_SUCCESS; + } + + /* We don't know want to print as help text, let the user know */ + fprintf(stderr, "'%s' test does not implement the 'help' command.\n", + test->sname); + return EXIT_FAILURE; } else if (!strcmp(sb_globals.cmdname, "prepare")) { @@ -1254,7 +1259,7 @@ int main(int argc, char *argv[]) { fprintf(stderr, "'%s' test does not implement the 'prepare' command.\n", test->sname); - exit(1); + return EXIT_FAILURE; } return test->builtin_cmds.prepare(); @@ -1272,7 +1277,6 @@ int main(int argc, char *argv[]) } else if (!strcmp(sb_globals.cmdname, "run")) { - current_test = test; if (run_test(test)) return EXIT_FAILURE; } @@ -1294,3 +1298,11 @@ int main(int argc, char *argv[]) exit(0); } + +/* Print a description of available command line options for the current test */ + +void sb_print_test_options(void) +{ + if (current_test != NULL) + sb_print_options(current_test->args); +} diff --git a/src/sysbench.h b/src/sysbench.h index 6eb538e..6eac6cb 100644 --- a/src/sysbench.h +++ b/src/sysbench.h @@ -203,4 +203,7 @@ sb_event_t sb_next_event(sb_test_t *test, int thread_id); void sb_event_start(int thread_id); void sb_event_stop(int thread_id); +/* Print a description of available command line options for the current test */ +void sb_print_test_options(void); + #endif diff --git a/tests/t/cmdline.t b/tests/t/cmdline.t index f45dfc7..c933570 100644 --- a/tests/t/cmdline.t +++ b/tests/t/cmdline.t @@ -110,7 +110,9 @@ Command line options tests > } > > function help() - > local o = sysbench.opt + > print("Available options:") + > sysbench.cmdline.print_test_options() + > local o = sysbench.opt > print(o.str_opt1) > print(o.str_opt2) > print(o.str_opt3) @@ -133,7 +135,7 @@ Command line options tests $ sysbench cmdline.lua help sysbench * (glob) - cmdline.lua options: + Available options: --dash-opt=STRING dash-opt desc [dash-opt val] --str_opt1=STRING str_opt1 description --bool_opt3[=on|off] bool_opt3 description @@ -187,6 +189,16 @@ Command line options tests 'cmdline.lua' test does not implement the 'cleanup' command. [1] + $ cat >cmdline.lua < + > EOF + + $ sysbench cmdline.lua help + sysbench * (glob) + + 'cmdline.lua' test does not implement the 'help' command. + [1] + $ cat >cmdline.lua < sysbench.option_defs = { > {},