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.
This commit is contained in:
Alexey Kopytov
2017-01-28 16:20:29 +03:00
parent 49016568ba
commit 49aa0b4322
4 changed files with 53 additions and 15 deletions

View File

@ -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

View File

@ -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);
}

View File

@ -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

View File

@ -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
>
> EOF
$ sysbench cmdline.lua help
sysbench * (glob)
'cmdline.lua' test does not implement the 'help' command.
[1]
$ cat >cmdline.lua <<EOF
> sysbench.option_defs = {
> {},