From a893c26129358a934767a92d487510c87c81dde8 Mon Sep 17 00:00:00 2001 From: Jens Axboe Date: Fri, 5 Dec 2014 09:35:40 -0700 Subject: [PATCH] parse: make suggestions for unknown options Signed-off-by: Jens Axboe --- init.c | 25 +++++++++++++++++++++++++ parse.c | 2 +- parse.h | 2 ++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/init.c b/init.c index 4f66759b..9fbc477f 100644 --- a/init.c +++ b/init.c @@ -1875,6 +1875,30 @@ static void parse_cmd_client(void *client, char *opt) fio_client_add_cmd_option(client, opt); } +static void show_closest_option(const char *name) +{ + int best_option, best_distance; + int i, distance; + + while (*name == '-') + name++; + + best_option = -1; + best_distance = INT_MAX; + i = 0; + while (l_opts[i].name) { + distance = string_distance(name, l_opts[i].name); + if (distance < best_distance) { + best_distance = distance; + best_option = i; + } + i++; + } + + if (best_option != -1) + log_err("Did you mean %s?\n", l_opts[best_option].name); +} + int parse_cmd_line(int argc, char *argv[], int client_type) { struct thread_data *td = NULL; @@ -2237,6 +2261,7 @@ int parse_cmd_line(int argc, char *argv[], int client_type) case '?': log_err("%s: unrecognized option '%s'\n", argv[0], argv[optind - 1]); + show_closest_option(argv[optind - 1]); default: do_exit++; exit_val = 1; diff --git a/parse.c b/parse.c index 42096471..141f4b28 100644 --- a/parse.c +++ b/parse.c @@ -1068,7 +1068,7 @@ int parse_option(char *opt, const char *input, * Option match, levenshtein distance. Handy for not quite remembering what * the option name is. */ -static int string_distance(const char *s1, const char *s2) +int string_distance(const char *s1, const char *s2) { unsigned int s1_len = strlen(s1); unsigned int s2_len = strlen(s2); diff --git a/parse.h b/parse.h index a9d726dd..15f2e06e 100644 --- a/parse.h +++ b/parse.h @@ -95,6 +95,8 @@ extern int check_str_bytes(const char *p, long long *val, void *data); extern int check_str_time(const char *p, long long *val, int); extern int str_to_float(const char *str, double *val, int is_time); +extern int string_distance(const char *s1, const char *s2); + /* * Handlers for the options */ -- 2.25.1