parse: make suggestions for unknown options
authorJens Axboe <axboe@fb.com>
Fri, 5 Dec 2014 16:35:40 +0000 (09:35 -0700)
committerJens Axboe <axboe@fb.com>
Fri, 5 Dec 2014 16:35:40 +0000 (09:35 -0700)
Signed-off-by: Jens Axboe <axboe@fb.com>
init.c
parse.c
parse.h

diff --git a/init.c b/init.c
index 4f66759b59b551ea0364231c45e7e61110b85ca0..9fbc477fd760651d117dc1bd37547bab98454ae9 100644 (file)
--- 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 42096471f021b804cbb8eccb9a781d25cada4334..141f4b282471614d0309befb87865d467f0bd047 100644 (file)
--- 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 a9d726dd94dadca481058432d8321ebb2f94b0ea..15f2e06e62926a9b2353ad14353827577318be72 100644 (file)
--- 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
  */